2

私が取り組んでいるプロジェクトでは、かなりの量の配列を一連のサブルーチンと関数に渡す必要があるため、モジュールを選択します。サブルーチンにどのように割り当て可能な配列を使用するのか

これらの配列はすべて割り当て可能ですが、サブルーチンで使用する必要がある配列を明示する必要がある場合を除き、大きな問題はありません。 実行するコードを管理する方法は、完全に過剰なものです。

方法、それが働いている:

Program Principal 
Use Info 
open(unit=1,file="dadose6h.dat") 
call get_data 
call sortsupply 
call sortcost 
call ratecapacity 
end Program 

Module Info 
integer i,j 
integer, dimension (:), allocatable :: supply 
integer, dimension (:), allocatable :: cost 
integer, dimension (:,:), allocatable :: capacity 
integer, dimension (:,:), allocatable :: demand 
End module info 


subroutine get_data 
use info 
read(1,*) j,i 
allocate (supply (j)) 
allocate (cost (j)) 
allocate (capacity (j,i)) 
allocate (demand (j,i)) 
read(1,*) supply 
read(1,*) cost 
read(1,*) capacity 
read(1,*) demand 
end subroutine 

Subroutine SortCost 
use info 
integer u 
!u=cost(1) 
!... 
print*, cost 
End subroutine 

Subroutine Sortsupply 
use info 
integer u 
!u=supply(1) 
!... 
print*, supply 
End subroutine 

Subroutine ratecapacity 
use info 
integer u 
!u=capacity(1,1)  
!... 
print *, j,i 
print*, capacity 
End subroutine 

の例では、上記のソートされている配列のほか、対等ありsortcostとsortsupply 2つのサブルーチンを持っています。私は本当にこの2つのタスクを行うためのユニークなサブルーチンを求めていました。私は正しい方法を宣言できません。

方法、それは次のようになります。

Program Principal 
Use Info 
open(unit=1,file="dadose6h.dat") 
call get_data 
call sort(supply) 
call sort(cost) 
call rate(capacity) 
end Program 

Module Info 
integer i,j 
integer, dimension (:), allocatable :: supply 
integer, dimension (:), allocatable :: cost 
integer, dimension (:,:), allocatable :: capacity 
integer, dimension (:,:), allocatable :: demand 
End module info 


subroutine get_data 
use info 
read(1,*) j,i 
allocate (supply (j)) 
allocate (cost (j)) 
allocate (capacity (j,i)) 
allocate (demand (j,i)) 
read(1,*) supply 
read(1,*) cost 
read(1,*) capacity 
read(1,*) demand 
end subroutine 

Subroutine Sort(X) 
!use info 
!i dunno how the declaration should be 
integer u 
!u=X(1) 
!... 
print*, "Sort:",X 
End subroutine 

Subroutine rate(X) 
!use info 
!i dunno how the declaration should be neither 
integer u 
!u=X(1,1) 
!... 
print*, "rate:", X 
End subroutine 

私はいくつかの宣言が、どれも仕事をしようとしなかった、これらの人はFORTRAN - allocatable array in subroutineは、How to pass allocatable arrays to subroutines in Fortranは、最初に答えが意図に障害を置くと同様の問題を抱えているが、私既に試した(inout、out、in)、まだ動作しません。 2番目は明示的なインタフェースについての答えを持っていますが、私は割り当てステータスについて何もしません。

注:私はこの質問をするのは間違いです。割り当て可能な配列をルーチンに渡すことについての問題ではありません。私はすでにモジュールのInfoでこれを行うことができます。しかし、どのサブルーチンを表示する必要がありますか? 。

私は本当にこのサブルーチンのこの2つの宣言を書くことができたことを感謝します。ソート(一次元配列)とレート(二次元)または少なくとも教えてください。 dadose6h.datファイルの

内容:

4 
7 
1000 2000 2000 2000 
100 100 100 100 
10 10 74 19 
60 1 25 20 
90 50 7 2 
11 31 51 96 
15 10 94 36 
52 89 47 13 
30 35 4 12 
100 150 50 200 
100 100 200 75 
100 100 200 250 
100 100 150 250 
150 100 200 250 
200 100 200 250 
200 150 200 250 
+0

注意を喚起するには、Fortranのすべての質問にタグfortranを使用してください。 Fortran 90は古いバージョンです。あなたの質問がその古いバージョンに固有のものであれば、他のタグの代わりにfortran90というタグを追加することができます。 –

+0

質問を書くときはise capital Iを入力し、カンマと完全停止の後にスペースを入れてください。それを編集するには多くの作業が必要です。 –

+0

私は先生が正しいかもしれないと心配し、あなたはコードの間違ったデザインを主張しています。 –

答えて

1

あなたのデザインはまだ私には非常に紛らわしいです。私はちょうどこれを行うでしょう:

Module Info 
integer i,j 
integer, dimension (:), allocatable :: supply 
integer, dimension (:), allocatable :: cost 
integer, dimension (:,:), allocatable :: capacity 
integer, dimension (:,:), allocatable :: demand 
End module info 


Module Procedures 

contains 

    Subroutine Sort(X) 
    integer :: X(:) 

    integer u 
    !u=X(1) 
    !... 
    print*, "Sort:",X 
    End subroutine 

End module Procedures 


Program Principal 
Use Info 
Use Procedures 

open(unit=1,file="dadose6h.dat") 
call get_data 
call sort(supply) 
call sort(cost) 
call rate(capacity) 
end Program 

これは非常に小さな変更ですので、私はあなたの説明を理解できない可能性があります。ソート手順でモジュールにデータを必要とする理由はありません。

+0

この例では、モジュールデータを使用する理由はありませんが、元の問題がありました。ソートルーチンの例にその理由があるようにいくつかの変更を加えました(print *、i)。答えをありがとう、それは多くの助けた。 –

関連する問題