Hello again.
I am hitting a brick wall. I am to read data from a file, store the data in three arrays respectively, and then calculate and display the average of the numbers in column 2 and 3.

These are the things I know:
Use a for loop to store the data in three arrays.
Use another for loop to access the data in one array.
Use another for loop to access the data in another array.

What I don't know:
How to set up the for loop to calculate the data in the arrays.

My example text file is:

1 12.5 6.5
2 25 16.5
3 32.1 15.4
4 40 10
5 11.6 7.65

``````#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream inputFile;
inputFile.open("C:\\Users\\Bob&Towey\\Documents\\assignment6example.txt");

//Declare three arrays.
int id[5]; //First column in data file
double hours[5]; // Second column in data file
double rate[5]; // Third column in data file
double sum=0;
double averagehours, averagerate;

//Read the data from file and store them in three arrays respectively.
for(int i=0; i<5; i++) {
inputFile >> id[i] >> hours[i] >> rate[i];

//Calculate and display the average hours and the average rate
for(){
sum = sum + hours[i];
averagehours = sum / 5;

cout << "Average of hours:" << averagehours;
}

for(){
sum = sum + rate[i];
averagerate = sum / 5;

cout << "The average rate is:" << averagerate;
}
}

return 0;
}``````

I have played around with the code. I tried to declare an int j and k for each of the for loops however when I run the program it displays a ridiculous amount of information none of which is correct.

Here is my updated code that I used for the for loops.

``````//Calculate and display the average hours and the average rate
for(int j=0; j < 5; j++){
sum = sum + hours[j];
averagehours = sum / 5;

cout << "Average of hours:" << averagehours;
}

for(int k = 0; k<5; k++){
sum = sum + rate[k];
averagerate = sum / 5;

cout << "The average rate is:" << averagerate;
}``````

It looks like you're trying to read in the data and calculate the means at the same time (which is possible, but not in the way that you're doing it). To me, your current code looks like this:

``````int main()
{
/* Open file and declare some arrays */

for ( int i = 0; i < 5; ++i ){
/* read a line of the file into the arrays */

/* loop over the first array and calculate a mean  */
/* loop over the second array and calculate a mean */
}

return 0;
}``````

What you want is something that looks more like:

``````int main()
{
/* Open file and declare some arrays */

for ( int i = 0; i < 5; ++i ){
/* read a line of the file into the arrays */
}

/* loop over the first array and calculate a mean  */
/* loop over the second array and calculate a mean */

return 0;
}``````

Spot the difference? The mean calculation is now outside the loop that reads in the data.

Also, You should look at how you're calculating the mean, your current method isn't very efficient (how often should you be dividing the sum by 5?)

As a final point, I'd say you should make a function to calculate the mean of an array, something like:

``double ArrayMean( double *array, unsigned arraySize );``

That way you won't have to duplicate the code every time you want to calculate a mean.

I hope that's some help to you :o)

Thank you very much Ravenous. I definitely see the difference. I am trying to do every thing within one loop when I need to be splitting the tasks up into separate loops. I will post my updated code later today. Thank you again for being so thorough and clear. You helped me out a bunch. ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.