Reputation: 1
I'm writing a program for school in FORTRAN. We have to write a program where the user enters a number of grades. With that number of grades, I have to make the program prompt the user that many times to enter the grades. I think I would use a dimensional variable, but I don't know how.
So far I have this, with obvious compiling errors:
INTEGER :: NumGrades
REAL :: GradeAverage
INTEGER :: N
WRITE (*,*) 'Enter Number of grades: '
READ (*,*) NumGrades
N = NumGrades
REAL, Dimension(N) :: Grade
WRITE (*,*) 'Enter the individual grades: '
READ (*,*) Grade
Any help would be greatly appreciated!
Upvotes: 0
Views: 99
Reputation: 145
Assuming your assignment isn't overdue, you could use allocation. It essentially lets you give an array size after initializing your variables.
INTEGER :: NumGrades
REAL :: GradeAverage !Not exactly sure what this is used for in this snippet
REAL, DIMENSION(:), ALLOCATABLE :: Grade
INTEGER :: i !Used for loop counters
WRITE (*,*) 'Enter Number of grades: '
READ (*,*) NumGrades
allocate(Grade(NumGrades)) !size(Grade) == NumGrades or whatever you inputted
WRITE (*,*) 'Enter the individual grades: '
!DO i = 1, NumGrades
READ(*, *) Grade(i)
!END DO
GradeAverage = sum(Grade) / size(Grade) !Just thought I'd throw this in
The dimension(:) lets the computer know that there is no defined size yet.
Alternatively, you can set the array size to a max integer value if you don't care about memory constraints.
Hope you got this for your assignment!
Edit - Oh yeah, don't forget to deallocate(Grade).
Upvotes: 1