1. Question?

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];
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;
}

//******************************************************
//******************************************************

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;
}```

2. No need to read the file once first to find the number of lines. Just use a std::vector and let it grow.

In this case, you don't even need that. Just keep 2 variables, count and sum. At the end, avg = sum / count.

3. I understood the avg = sum part, but what did you mean before that?

4. Originally Posted by Guru_
I understood the avg = sum part, but what did you mean before that?
You are reading the file twice, once to find out how many lines it has, and once to actually read the values from the file. If you use std::vector, you can just use "push_back" to automatically grow the vector [which is a C++ version of array] to the size required.

But also, as stated, you do not need more than two variables to get an average, just sum up all the numbers and count them in one loop, and once the loop is finished, print out the sum / count as average.

--
Mats

Popular pages Recent additions