この fortran コードは、単純な行列テーブルからデータを txt ファイルにコピーし、各行と列の以前の成績に基づいて成績と平均を計算することになっています。なんらかの理由で、成績や平均を印刷することができません。空白または 0 や星が付いて表示されます。txt ファイルからマトリックス テーブルを出力した後、実行時にプログラムがクラッシュします。エラーはアクセス違反であると表示されます。あまり役に立ちません。これまでの私のコードは次のとおりです。
program calculate1
real,dimension(:,:),allocatable :: a
character(200)::line
print *,'How many rows are there?'
read *,n !amount of rows
print *,'How many columns are there?'
read *,m !amount of columns
allocate (a(n+1,m+1))
open(1, file='in1.txt')
call print_out (a,n+1,m+1)
Close(1);
contains
subroutine print_out(b,n,m)
real,dimension(:,:):: b
character(200)::line(n)
character(1)::g
write(*,'(10x)',advance='no')
do j=2,m-1
write(*,100,advance='no'),'hw',j-1
enddo
100 format(a2,i2,3x)
print '(a6,a6)','exam ','grade '
do i=1,n-1 !makes rows from data
read(1,'(a)') line(i) !read from in1.txt
write(*,'(a)') line(i) !write to screen
enddo
do i=1,n !makes (m+1)th column
b(i,m+1)= sum(b(i,1:m-1))+3*b(i,m)
enddo
do j=1,m+1 !makes (n+1)th row
b(n+1,j)= sum(b(1:n,j))/n
enddo
101 format(a8,4x,20f8.2) ! format for average print
print 101, 'averages', b(n+1,j)
endsubroutine
endprogram
in1.txt ファイルの内容は次のとおりです。
jackson 4. 4. 4.2 8.
johnson 4. 2. 2.11 3.
sugimoto 1. 0. 1.5 0.
wong 3.5 3. 3. 2.