In: Physics
Write a Fortran program that is able to read in the data file.
The file has lines with the structure: 19990122 88888 30.5
Where:
i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day)
ii) the second is the five digit odometer reading of a car
iii) the third is the amount of fuel put into the car on that date to fill the tank up to full
The program must be able to determine the average miles per gallon consumed and the sigma of this distribution
(NOTE: I know that I can't attach the file itself, so there is no way for you to properly debug the code.That's fine. I just need help with the structure and setup)
Please look at the following step-wise solution. I have added program snippets in each steps.
THE PROGRAM IS WELL COMMENTED using ' ! '
THE HIGHLIGHTED LINES ARE THE PART OF THE CODE
1. Reading the data into three different array(one each for date, odometer reading and petrol refil amount)
Replace n by the number of lines in the data file
PROGRAM MILEAGE
INT date[n],odo[n],dist !! ARRAY for date and odomoter reading. put the value of n i,e the number of lines in data file, and distance travelled
FLOAT petrol[n], avg_miles[n-1], mean_dist !! ARRAY for petrol input and avg_miles calculation and for average
FLOAT total,mean,square,sigma
OPEN(1, FILE="DATA_FILE.dat , STATUS="unknown")
!! The do loop to read the data file
do i=1,n
read(1,*) date[i],odo[i],petrol[i]
enddo
2. NEXT WE NEED TO FIND THE DISTRIBUITION OF AVERAGE MILES PER GALLON
Now, lets say, the car was entirely filled on the first day. If we subtract the odometer reading in second row from first row to find the distance travelled and the amount of petrol refilled in second row is the amount of petrol used to travel the distance we have found
do i=2,n
dist=odo[i]-odo[i-1]
avg_miles[i-1]=dist/petrol[i]
total = total+avg_miles[i-1]
enddo
3. NEXT WE HAVE TO FIND THE SIGMA OF THE AVERAGE MILES PER GALLON DISTRIBUITION.
!! MEAN OF average miles per gallon
mean = total/(n-1).
!! SIGMA OF THE DISTRIBUITION
do i= 1,n-1
square=square+(avg_miles[i] - mean)**2
enddo
sigma = sqrt(square/n-1) !! Here while finding sigma we have to divide one entry less than the total number of entrries in avg_miles which is actually one less than the total number of rows in data file. eg- if data file has 10 entries than avg_miles array has 9 entries and we will divide square by 8 while finding sigma(sample standard deviation)
write(*,*) "The Sigma of the distribuition is"
write(*,*) sigma
END PROGRAM MILEAGE