Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fortran continuation

Status
Not open for further replies.

WSUPhys

Technical User
Feb 6, 2013
20
US
I have attached my source code. I am currently working on a program that will take data from a text array file and calculate the total average mpg,the standard deviation, the yearly mpg and the average mpg by season(Summer and Winter). When I try to calculate the mpg per season, my code does not cooperate. I am having two problems, the first is that I was earlier getting a result of 0 and now after changing my code I get an error that tells me that the "&" is an invalid token but I am merely using it to continue the line. Any help would be appreciated.
Program Fuel Consumption
Implicit none

Real Gal(290),MPG(290),TotalGal,Variance,StdDev,Totalsummer
Real MPGAvg,MPG93,MPG94,MPG95,MPG96,MPG97,MPG98,Totalwinter,TotalMonths
Real Summer, Winter
Integer Yr(290),Mon(290),Day(290),Mi(290),h,k,l,m,n,p,q,s,t,u,y
Integer z,OtherMonths
Winter=0
Summer=0
Totalsummer=0
Totalwinter=0
Open (unit=2, file = "car.dat")
! This format reads in the file breaking it into a five column array
10 Format(I4,I2,I2,1x,I5,1x,F4.1)
! This loop reads into the array
Do 100 k=1,290
Read (2,10) Yr(k),Mon(k),Day(k),Mi(k),Gal(k)
100 continue
Do 102 m=1,290
mpg(m)=(Mi(m)-Mi(m-1)/Gal(m))
TotalGal=Gal(m)+TotalGal
102 Continue
! This calculates the average mpg
MPGAvg=(Mi(290)-Mi(1))/TotalGal
Do 103 n=1,290
mpg(n)=(Mi(n)-Mi(n-1))/Gal(n)
! This calculates the variance and standard deviation
Variance=Variance+((mpg(n)-MPGAvg)**2)
103 continue
StdDev=(Variance/290)**.5
! The following calculates the average mpg per season
If(Mon(k).eq. 1 .or. Mon(k).eq. 2 .or. Mon(k) .eq. 11&
&.or. Mon(k).eq. 12) then
Winter=Winter+1
Totalwinter=Totalwinter+mpg(n)
Else If (Mon(k).eq. 6 .or.Mon(k).eq. 7.or.Mon(k).eq. 8&
&.or.Mon(k).eq. 9)then
Summer=Summer+1
Totalsummer=Totalsummer+mpg(m)
Else
OtherMonths=OtherMonths+1
TotalMonths=TotalMonths+mpg(m)
End If
Totalwinter=Totalwinter/Winter
Totalsummer=TotalSummer/Summer
Write(6,*)"Total Winter is", Totalwinter
Write(6,*) "This is the average fuel consumption"
Write(6,*) MPGAvg
Write (6,*)"This is the sigma distribution"
Write(6,*)StdDev
! The following code will calculate the average mpg for each year
Do 104 p=2,63
Gal(p)=Gal(p)+Gal(p-1)
104 continue
MPG93=(Mi(p)-Mi(p-1))/Gal(p)
Write(6,*)"This is the average MPG for 1993"
Write(6,*)MPG93

Do 105 q=65,117
Gal(q)=Gal(q)+Gal(q-1)
105 continue
MPG94=(Mi(q)-Mi(q-1))/Gal(q)
Write(6,*)"This is the average MPG for 1994"
Write(6,*)MPG94

Do 106 s=119,188
Gal(s)=Gal(s)+Gal(s-1)
106 continue
MPG95=(Mi(s)-Mi(s-1))/Gal(s)
Write(6,*)"This is the average MPG for 1995"
Write(6,*)MPG95

Do 107 t=190,245
Gal(t)=Gal(t)+Gal(t-1)
107 continue
MPG96=(Mi(t)-Mi(t-1))/Gal(t)
Write(6,*)"This is the average MPG for 1996"
Write(6,*)MPG96

Do 108 u=247,284
Gal(u)=Gal(u)+Gal(u-1)
108 continue
MPG97=(Mi(u)-Mi(u-1))/Gal(u)
Write(6,*)"This is the average MPG for 1997"
Write(6,*)MPG97

Do 109 y=286,290
Gal(y)=Gal(y)+Gal(y-1)
109 continue
MPG98=(Mi(290)-Mi(285))/Gal(y-1)
Write(6,*)"This is the average MPG for 1998"
Write(6,*)MPG98
! The following prints values for the seasons
Write(6,*)"The average mpg during winter was"
Write(6,*) Totalwinter
Write(6,*)"The average mpg during summer was"
Write(6,*)Totalsummer



End
 
Your source compiles fine with gfortran, but when I try to run it I get an error
Code:
At line 15 of file WSUPhys.f95 (unit = 2, file = 'car.dat')
Fortran runtime error: End of file
Line 15 is
Code:
Read (2,10) Yr(k),Mon(k),Day(k),Mi(k),Gal(k)

I thing that your file car.dat contains lines like this
Code:
19990122 88888 30.5
...
 
IMO your format what you use for reading is bad.
I personally would rather read simply the 3 columns of a file into 3 variables and extract from the first column (which represents date in specific format YYYMMDD) the values for year month and day.
 
I changed you code:
first Ideclared this variable
Code:
character(8)  :: date_char
and then added to the loop body this
Code:
Do 100 k=1,290 
   !Read (2,10) Yr(k),Mon(k),Day(k),Mi(k),Gal(k)
   Read (2,*) Date(k),Mi(k),Gal(k)
   write(*,*) 'Date =', Date(k)
   ! convert integer to string
   write (date_char, '(I8)') date(k)
   write(*,*) 'numerical date = ', Date(k)
   write(*,*) 'character date = ', Date_char
   ! extract from string  year, month and day
   read(date_char(1:4),'(I4)') Yr(k)
   read(date_char(5:6),'(I2)') Mon(k)  
   read(date_char(7:8),'(I2)') Day(k)
   write(*,*) 'year =', Yr(k), ', month =', Mon(k), ', day = ', Day(k)
 100 continue
Then I got this result
Code:
$ gfortran WSUPhys.f95 -o WSUPhys

$ WSUPhys
 Date =    19930114
 numerical date =     19930114
 character date = 19930114
 year =        1993 , month =           1 , day =           14
 Date =    19930118
 numerical date =     19930118
 character date = 19930118
 year =        1993 , month =           1 , day =           18
...

...

...
 Date =    19980305
 numerical date =     19980305
 character date = 19980305
 year =        1998 , month =           3 , day =            5
 Date =    19980306
 numerical date =     19980306
 character date = 19980306
 year =        1998 , month =           3 , day =            6
 Total Winter is             NaN
 This is the average fuel consumption
   14.857347    
 This is the sigma distribution
   3.6001668    
 This is the average MPG for 1993
   16.832298    
 This is the average MPG for 1994
   16.121212    
 This is the average MPG for 1995
   15.193370    
 This is the average MPG for 1996
   15.000000    
 This is the average MPG for 1997
   13.010203    
 This is the average MPG for 1998
   10.549110    
 The average mpg during winter was
             NaN
 The average mpg during summer was
             NaN
 
thank you, I think the problem with the continuation is from trying to run the program as an f77, I will switch it to f95. Thank you for the suggestion on the dates.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top