如何使用维度属性?

时间:2014-01-02 21:14:28

标签: fortran fortran90

我需要找到如何在此程序中使用dimension属性。我无法弄清楚这里的问题是用户如何指定行数? (换句话说,学生人数):

PROGRAM 
implicit none

integer::k,sn
real,dimension(**?**,4)::A
character(len=10),dimension(**?**)::B

open(10,file='students.txt',status='new')
write(*,*)'how many student are in the classroom?'
read(*,*)sn 
k=1 

do 
    write(*,*)k,'.','student name=';read(*,*)B(k)
    write(*,*)'1.Quiz';read(*,*)A(k,1)
    write(*,*)'2.Quiz';read(*,*)A(k,2)
    write(*,*)'Final Quiz';read(*,*)A(k,3)


    A(k,4)=(A(k,1)*30/100)+(A(k,2)*30/100)+(A(k,3)*40/100)

    write(10,9)B(k),'     ',A(k,1),'   ',A(k,2),'   ',A(k,3),'   ',A(k,4)

    k=k+1
    if(k>sn)exit

end do
9 format(1x,A10,A5,F5.1,A3,F5.1,A3,F5.1,A3,F5.1)  
end program 

2 个答案:

答案 0 :(得分:0)

基本上你有固定(静态)数组,例如使用dimension

real,dimension(4) :: X

X是一个长度为41 - 4)的数组。这相当于:

real :: X(4)

静态数组在整个范围内具有固定长度(例如,在整个程序中用于全局变量或整个函数/子程序)。

您需要的是allocatable在运行时allocate d的数组:

program test
  implicit none
  real, allocatable :: B(:) ! The shape is given by ":" - 1 dimension
  integer           :: stat

  ! allocate memory, four elements: 
  allocate( B(4), stat=stat )
  ! *Always* check the return value
  if ( stat /= 0 ) stop 'Cannot allocate memory'

  ! ... Do stuff

  ! Clean up
  deallocate( B )

  ! Allocate again using a different length:
  allocate( B(3), stat=stat )
  ! *Always* check the return value
  if ( stat /= 0 ) stop 'Cannot allocate memory'

  ! No need to deallocate at the end of the program! 

end program

答案 1 :(得分:0)

real,dimension(:,:),allocatable ::A

character(len=10),dimension(:),allocatable::B
.
.
.
 DEALLOCATE(A)
          DEALLOCATE(B)

这个有效!谢谢你们。