/* * ======================================================================= * prog_rainfall.c : The purposes of this C program are: * * (a) To read daily rainfalls for one week, and compute the maximum * daily rainfall and the average daily rainfall. * (b) Demonstrates use of one-dimensional arrays, and output to a file. * * Copyright (C) 1998 by Mark Austin and David Chancogne. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written by : Mark Austin * ======================================================================= */ #include #include #define max(a, b) ((a) > (b) ? (a) : (b)) void fileOut( float faRainfall[], float fAv ); enum { MaxDays = 7 }; int main( void ) { float faRainfall[ MaxDays ]; /* store the daily rainfall */ float fSum = 0.0; /* store sum of daily rainfalls */ float fMax = 0.0; /* store maximum rainfall */ float fAv = 0.0; /* store average rainfall */ int iDay; /* day counter */ /* [a] : Initialize the rainfall for each day to zero */ for( iDay = 1; iDay <= MaxDays; iDay = iDay + 1 ) faRainfall[ iDay-1 ] = 0.0; /* [b] : Prompt the user for the rainfall for each day: */ for( iDay = 1; iDay <= MaxDays; iDay = iDay + 1 ) { printf( "Enter the rainfall (in inches) for day %d -->", iDay ); scanf( "%f%*c", &faRainfall[ iDay-1 ] ); } /* [c] : Print the maximum and average rainfall for the week: */ for( iDay = 1; iDay <= MaxDays; iDay = iDay + 1 ) { fMax = max( fMax, faRainfall[ iDay-1 ] ); fSum += faRainfall[ iDay-1 ]; } fAv = fSum/MaxDays; printf("\nThe maximum amount of rain this week was %f inches\n", fMax ); printf( "The average daily rainfall this week was %f inches\n", fAv ); /* [d] : Write details of rainfall to output file */ fileOut( faRainfall, fAv); } /* * ==================================================================== * fileOut() : Write rainfall data to a file called RAIN.TXT * * Input : float faRainfall[] -- array of daily rainfall measurements. * float fAv -- average daily rainfalls for week. * Output : void * ==================================================================== */ void fileOut( float faRainfall[], float fAv ) { FILE *FpRain; /* typedef struct, defined in stdio.h */ int iDay; /* day counter */ FpRain = fopen("RAIN.TXT", "w"); if(FpRain == NULL) { printf("ERROR >> File RAIN.TXT could not be opened\n"); exit(1); } for ( iDay = 1; iDay <= MaxDays; iDay = iDay + 1 ) fprintf( FpRain, "%f %f \n", faRainfall[ iDay-1 ], fAv ); fclose( FpRain ); }