Hello,
I created the following code:
Code:
// Headers and Other Technical Items
#include <iostream>
#include <fstream> // For file I/O
#include <iomanip> // For setw()
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);
// Variables
char data_filename[] = "C:\Cpp_Source_Code_Files\Chapter_19\Demo_Farm_Acres_Input";
//******************************************************
// 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);
pause();
return 0;
}
//******************************************************
// count_file_values
//******************************************************
int count_file_values(char input_filename[])
{
fstream inData; // device token for the input stream
double next_value;
int number_of_values = 0;
inData.open(input_filename, ios::in); //Open input file
if (!inData)
{
cout << "\n\nError opening input data file: " << input_filename << "\n\n";
pause();
exit(EXIT_FAILURE);
}
// Get data values and count them
while (inData >> next_value)
{
number_of_values++;
}
// Close the files
inData.close(); //Close input file
return number_of_values;
}
//******************************************************
// load_array_from_file
//******************************************************
void load_array_from_file(char input_filename[], int things[], int array_size)
{
fstream inData; // device token for the input stream
inData.open(input_filename, ios::in); //Open input file
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++) // Load the array
{
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; // Accumulator
for (int i = 0; i < array_size; i++)
{
total += things[i];
}
return total;
}
//******************************************************
// pause
//******************************************************
void pause(void)
{
cout << "\n\n";
system("PAUSE");
cout << "\n\n";
return;
}
Now I have to add a function that returns an integer value after being passed an array and the number of array elements.elements. The value it should return is the average of the values that are within the array ( integer division ).
The average would be the total divided by the number of elements in the array and it will truncate to a whole number ( that's ok). Lets say it would be average_array. Just supposed to display the average within the function main following the display of the sum.
I am really having difficulties with that. I don't understand how I would do it. I hope somebody can help me