//RainStatsV2ReadFile.cpp //program uses parallel arrays and functions to find the annual total //average rainfall, and month of highest rainfall for various locations //All data is read from a file //program then displays information nicely formatted to a text file //updated 11/08/22 //If you wish you can add overloaded functions for outputArrays() and outputStatistics() //that output to screen as well. #include #include #include #include using namespace std; //findTotalandAverage() computes the annual rain total and the monthly average void findTotalandAverage(const float [], float &total, float &average); //findGreatestRainLoc() finds the month with the greatest rainfall and return its location index in array int findGreatestRainLoc(const float []); //outputArrays() outputs the contents of months and rainfall parallel arrays for location to screen void outputArrays( string location, string months[12], float rainfall[12]); //outputStatistics() outputs the annual total, average and the month and amount of greatest rainfall to screen void outputStatistics( string months[], float rainfall[], float total, float average, int locGreatest); //outputArrays() outputs the contents of months and rainfall parallel arrays for location to file void outputArrays( ofstream &, string location, string months[12], float rainfall[12]); //outputStatistics() outputs the annual total, average and the month and amount of greatest rainfall to file void outputStatistics( ofstream &, string months[], float rainfall[], float total, float average, int locGreatest); int main() { //declare and initialize an array of strings holding the month name abbreviations string months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; ifstream fin; //my file variables ofstream fout; fin.open("READRAINFALL.TXT"); if (!fin.is_open()) { cout << "Error opening file. Program terminated!\n"; return 1; } fout.open("RAINSTATS.TXT"); if (!fout.is_open()) { cout << "Error opening file. Program terminated!\n"; return 1; } float monthlyRain[12]; float total, avg; int locGreatest; string location; //priming input for the outer loop getline(fin,location); //get the first location while (!fin.eof() ) //while we have not reached end of file { //Input the twelve monthly rainfall amounts for that location (could be an input fcn) for (int k=0; k < 12; k++) { fin >> monthlyRain[k]; } //Process statistics for this location findTotalandAverage(monthlyRain, total, avg); locGreatest = findGreatestRainLoc(monthlyRain); outputArrays(location, months, monthlyRain); outputStatistics(months, monthlyRain, total, avg, locGreatest); outputArrays(fout, location, months, monthlyRain); outputStatistics(fout, months, monthlyRain, total, avg, locGreatest); fin.ignore(); //MUST clear the last newline from input buffer getline(fin, location); //get next location from file } //end while not end of file loop fin.close(); return 0; } //Find annual total and average per month void findTotalandAverage(const float monthlyRain[12], float &total, float &average) { total = 0; for (int j= 0; j < 12; j++) total += monthlyRain[j]; average = total/12; } //find the month with the greatest rainfall and return its location in array int findGreatestRainLoc(const float rainfall[12]) { float maxRain = 0; int maxRainIndex =0; int k = 0; for (k = 0; k < 12; k++) { if (rainfall[k] > maxRain) { maxRain = rainfall[k]; maxRainIndex = k; } } return maxRainIndex; //returns index in array where greatest rainfall is found } //output the contents of months and rainfall parallel arrays to a file for location void outputArrays( ofstream &fout, string location, string months[12], float rainfall[12]) { fout << "Statistics for " << location << endl; fout << "Month" << setw(22) << "Rainfall inches" << endl; //heading for table fout << setprecision(2) << fixed << showpoint; for (int j= 0; j < 12; j++) { fout << months[j] << setw(18) << rainfall[j] << '\"' << endl; } } //output the annual total, average and the month and amount of greatest rainfall to file void outputStatistics( ofstream &fout, string months[], float rainfall[], float total, float average, int locGreatest) { fout << "Total for the year " << total << " inches" << endl; fout << "Average rainfall per month " << average << " inches" << endl; fout << "Month with the greatest rainfall was " << months[locGreatest] << " with rainfall of " << rainfall[locGreatest] << " inches" << endl << endl; } //output the contents of months and rainfall parallel arrays to a screen for location void outputArrays( string location, string months[12], float rainfall[12]) { cout << "Statistics for " << location << endl; cout << "Month" << setw(22) << "Rainfall inches" << endl; //heading for table cout << setprecision(2) << fixed << showpoint; for (int j= 0; j < 12; j++) { cout << months[j] << setw(18) << rainfall[j] << '\"' << endl; } } //output the annual total, average and the month and amount of greatest rainfall to screen void outputStatistics( string months[], float rainfall[], float total, float average, int locGreatest) { cout << "Total for the year " << total << " inches" << endl; cout << "Average rainfall per month " << average << " inches" << endl; cout << "Month with the greatest rainfall was " << months[locGreatest] << " with rainfall of " << rainfall[locGreatest] << " inches" << endl << endl; }