Have been working on a progressing assignment and have already created the total array count, but Im having a hard time getting my head around how I can use that to find average of the array as well.
Any help would be appreciated. I was thinking I could somehow use total and array_size from the array_sum function but I've tried all different kinds of ways and none of them are working.
Everything in the program works properly, minus the an average function im trying to produce.
#include <iostream> #include <fstream> #include <iomanip> using namespace std; // Function Prototypes int count_file_values(char input_filename[]); void load_array_from_file(char input_filename[], int things[], int array_size); void display_array(int things[], int array_size); int sum_array(int things[], int array_size); void pause(void); char data_filename[] = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt"; //****************************************************** // main //****************************************************** int main(void) { int record_count = count_file_values(data_filename); int acres[record_count]; load_array_from_file(data_filename, acres, record_count); cout << "The individual farm acerages are: \n"; display_array(acres, record_count); cout << "\n\nThe total acerage is: " << sum_array(acres, record_count); cout << "\n\nThe average acerage is: " << (total / array_size); pause(); return 0; } //****************************************************** // count_file_values //****************************************************** int count_file_values(char input_filename[]) { fstream inData; double next_value; int number_of_values = 0; inData.open(input_filename, ios::in); if (!inData) { cout << "\n\nError opening input data file: " << input_filename << "\n\n"; pause(); exit(EXIT_FAILURE); } while (inData >> next_value) { number_of_values++; } inData.close(); return number_of_values; } //****************************************************** // load_array_from_file //****************************************************** void load_array_from_file(char input_filename[], int things[], int array_size) { fstream inData; inData.open(input_filename, ios::in); if (!inData) { cout << "\n\nError opening input data file: " << input_filename << "\n\n"; pause(); exit(EXIT_FAILURE); } else { for (int i = 0; i < array_size; i++) { inData >> things[i]; } inData.close(); } return; } //****************************************************** // display_array //****************************************************** void display_array(int things[], int array_size) { cout << "\n************************************"; for (int i = 0; i < array_size; i++) { cout << "\nOffset: " << setw(2) << i; cout << " Memeber: " << setw(2) << i+1; cout << " value is: " << setw(3) << things[i]; } cout << "\n************************************"; return; } //****************************************************** // sum_array //****************************************************** int sum_array(int things[], int array_size) { int total = 0; for (int i = 0; i < array_size; i++) { total += things[i]; } return total; } //****************************************************** // average_array //****************************************************** ????? //****************************************************** // pause //****************************************************** void pause(void) { cout << "\n\n"; system("PAUSE"); cout << "\n\n"; return; }
This post has been edited by texagg: 19 April 2010 - 04:13 PM