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:
Read my data from file.
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;
}