Ok, I have a program I'm trying to get to work, but I'm struggling to get right. I suspect its when it reads the data from the .txt file. I have to get the average of column 1 and the average of column 2, and get the highest of column 1 and the lowest of column 2.
Here's the code I have so far...
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int getData();
int averageHigh();
int averageLow();
int indexHighTemp();
int indexLowTemp();
int i, j;
int hsum = 0, lsum = 0;
int avgh, avgl;
int high = 0, low = 0;
int ind;
int const row = 2, col = 12, num = 12;
int temp[row][col];
int main()
{
cout << "Processing Data" <<endl;
cout << endl;
getData();
averageHigh();
averageLow();
indexHighTemp();
indexLowTemp();
cout << " " <<endl;
system("PAUSE");
return 0;
}
int getData()
{
ifstream inFile;
inFile.open ("c:\\Ch9_Ex9Data.txt");
if (!inFile)
{
cout << "Unable to open input file!"<<endl;
return 0;
}
while( inFile >> temp[i][0] >> temp[i][1])
{
++i;
}
}
int averageHigh()
{
for(int i = 0; i < row; i++)
{
for(int j=0; j < col; j++)
{
hsum = (hsum + (temp[0][i]));
}
}
avgh = hsum / 12;
cout << "Average high temperature: ";
cout << avgh <<endl;
return 0;
}
int averageLow()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
lsum = (lsum + (temp[1][i]));
}
}
avgl = lsum / 12;
cout << "Average low temperature: ";
cout << avgl << endl;
return 0;
}
int indexHighTemp()
{
ind = 0;
for(int i=0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if (high <= (temp[0][i]))
{
high = (temp[0][i]);
ind = i;
}
}
}
cout << "Highest temperature: ";
cout << (temp[0][ind]) << endl;
return 0;
}
int indexLowTemp()
{
ind = 0;
for(int i=0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if (low >= (temp[1][i]))
{
low = (temp[1][i]);
ind = i;
}
}
}
cout << "Lowest temperature: ";
cout << (temp[1][ind]) << endl;
return 0;
}
And here's the .txt file data
34 -12
30 -21
32 -18
40 -5
52 10
75 32
85 49
92 53
88 31
65 9
42 -17
39 -19
The lowest number is right, but the highest is not. Plus my averages are way off. Again, I think its my getData() function.
Help! Thanks in advance.