I am having a hard time understanding how to establish arrays for this problem. We're supposed to write a program that reads in numbers from 2 different files, and it averages one of the files while outputting both files for each month of the year. I have been reading the tutorial but the array doesn't make sense to me.
Code:#include <iostream> #include <fstream> using namespace std; const int Months = 12; void get_stats ( double Previous_data, double current_data ); void output_previous ( double Previous_data ); int main() { int months [12]; double Previous_data, current_data, average; cout << "We are going to get the data required for this lab. One moment please..." << endl; get_stats( Previous_data, current_data ); output_previous( Previous_data ); return 0; } void get_stats( double Previous_data, double current_data) { ifstream current; current.open ( "rainfall_current.dat" ); // If the file fails to open it will close the program and output the statement. if ( current.fail( ) ) { cout << "Please check if the file is saved properly. It could not open." << endl; } double temp, sum = 0; int i = 0; double average; // Loops the file until no more numbers can be read while (current >> temp) { sum += temp; ++i; } // Final average average = sum / i; // Outputs the average to the screen cout << "The average of the numbers is "; cout << average; cout << endl; } void output_previous( double Previous_data ) { ifstream previous; previous.open ( "rainfall_previous.dat" ); // If the file fails to open it will close the program and output the statement. if ( previous.fail( ) ) { cout << "Please check if the file is saved properly. It could not open." << endl; } }



LinkBack URL
About LinkBacks


