| |
3.3 READING FROM AND WRITING TO FILES |
| A convenient way to solve the problem is to read from files and also write the voluminous data to files rather than to the screen. This is done as follows |
| program averagetemp |
| |
DIMENSION tempval (365, 24) |
| |
open (unit = 11, file = 'input.dat') |
| |
open (unit = 12 ,file = 'output') |
| |
read (11, *) ( ( tempval (i, j), j = 1,24), i= 365) |
| c |
calculate the average temp each day & write to file output |
| c |
this is for a non-leap year. For a leap year, we have 366 |
| c |
days |
| |
How will you write a more general program valid for both types of years? |
| |
do 100 i = 1, 365 |
| |
|
xx= 0.0 |
| |
do 90 j = 1,24 |
| |
|
xx = xx + tempval (i,j) |
| 90 |
continue |
| |
avtemp = xx /24.0 |
| 100 |
write (12, *) 'day no =', i, 'average temp = ', avtemp |
| |
close (12) |
| |
close (11) |
| |
end |
|
|
|