How do I find the average of the values that are within the array? I was planning on using interger division since I'm using interger values.
Code:
#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);
void pause(void);
// Variables
char data_filename[] = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";
//******************************************************
// main
//******************************************************
int main(void)
{
int record_count = count_file_values(data_filename);
int areas[record_count];
load_array_from_file(data_filename, areas, record_count);
cout << "The areas are: \n";
display_array(areas, record_count);
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 " << input_filename << "\n\n";
pause();
exit(EXIT_FAILURE);
}
while (inData >> next_value)
{
number_of_values++;
}
// Close the files
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; // 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 << "\nInterger: " << setw(2) << i;
cout << " Decimal: " << setw(2) << i+1;
cout << " value is: " << setw(3) << things[i];
}
cout << "\n************************************";
return;
}