CJH44
CJH44

Reputation: 13

Read data from file into array

I am having trouble reading data from a file into an array, I am new to programing and fortran so any information would be a blessing.

this is a sample of the txt file the program is reading from

!60         
!USS Challenger    1.51 12712.2 1.040986E+11
!USS Cortez        9.14 97822.5 2.009091E+09
!USS Dauntless     5.76 27984.0 2.599167E+09
!USS Enterprise    2.48  9136.3 1.478474E+10
!USS Excalibur     3.83 32253.0 1.286400E+10

all together there is 60 ships. the variables are separated by spaces and are as follows

warp factor, distance in light years, actual velocity.

this is the current code I have used, it has given the error The module or main program array 'm' at (1) must have constant shape

PROGRAM engine_performance
    IMPLICIT NONE
    INTEGER :: i         ! loop index
    REAL,PARAMETER :: c = 2.997925*10**8 ! light years (m/s)
    REAL,PARAMETER :: n = 60 ! number of flights
    REAL :: warpFactor   ! warp factor
    REAL :: distanceLy   ! distance in light years
    REAL :: actualTT     ! actual time travel
    REAL :: velocity     ! velocity in m/s 
    REAL :: TheoTimeT    ! theoretical time travel 
    REAL :: average      ! average of engine performance
    REAL :: median       ! median of engine performance
    INTEGER, DIMENSION (3,n), ALLOCATABLE :: M



    OPEN(UNIT=10, FILE="TrekTimes.txt")

  DO i = 1,n 
      READ(*,100) warpFactor, distanceLY, actualTT
         100 FORMAT(T19,F4.2,1X,F7.1,&
                    1X,ES 12.6)

      WRITE(*,*) M 
  END DO
  CLOSE (10) 
END PROGRAM engine_performance

Upvotes: 1

Views: 1098

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78316

The first time I read your code I mistakenly read M as an array in which your program would store the numbers from the file of ships. On closer inspection I realise (a) that M is an array of integers and (b) the read statement later in the code reads each line of the input file but doesn't store warpFactor, distanceLY, actualTT anywhere.

Making a wild guess that M ought to be the representation of the numeric factors associated with each ship, here's how to fix your code. If the wild guess is wide of the mark, clarify what you are trying to do and any remaining problems with your code.

First, fix the declaration of M

REAL, DIMENSION (:,:), ALLOCATABLE :: M

The term (3,n) can't be used in the declaration of an allocatable array. Because n is previously declared to be a real constant it's not valid as the specification of an extent of an array. If it could be the declaration of the array would fix its dimensions at (3,60) which means that the array can't be allocatable.

So, also change the declaration of n to

INTEGER :: n

It's no longer a parameter, you're going to read its value from the first line of the file, which is why it's in the file in the first place.

Second, I think you have rows and columns switched in your array. The file has 60 rows (of ships), each of which has 3 columns of numeric data. So when it comes time to allocate M use the statement

ALLOCATE(M(n,3))

Of course, prior to that you'll have had to read n from the first line in the file, but that shouldn't be a serious problem.

Third, read the values into the array. Change

  READ(*,100) warpFactor, distanceLY, actualTT

to

  READ(*,100) M(i,:)

which will read the whole of row i.

Finally, those leading ! on each line of the file -- get rid of them (use an editor or re-create the file without them). They prevent the easy reading of values. With the ! present reading n requires a format specification, without it it's just read(10,*).

Oh, and absolutely finally: you should probably, after you've got this program working, direct your attention to the topic of defined types in your favourite Fortran tutorial, and learn how to declare type(starship) for added expressiveness and ease of programming.

Upvotes: 1

Related Questions