Im having problems with this program. It finds the average high/low, and the index high/low temperatures. But for the index high it just stops at one number even though the numbers after it is higher. Everything is right besides the index high. It gives me the index high as 79 but it should be 84 but cant find out why. Any thoughts?
#include<iostream>
#include<fstream>
#include<iomanip>
constint NO_OF_MONTHS = 12;
constint rows = 12; //The rows and collums are constant
constint cols = 2;
usingnamespace std;
void getData( int listTemp[rows][cols]);
void averageHigh( int listTemp[rows][cols]);
void averageLow(int listTemp[rows][cols]);
void indexHighTemp(int listTemp[rows][cols]);
void indexLowTemp( int listTemp[cols][cols]);
ifstream infile;
int main()
{
double average;
int listTemp[rows][cols];
infile.open("data.txt"); //open infile
if (!infile)
{
cout << "Error opening data.txt " << endl;
return 1;
}
cout<<fixed<<showpoint; //result will show in decimal
cout<<setprecision(2);
// calls functions
getData(listTemp);
averageHigh(listTemp);
averageLow(listTemp);
indexHighTemp(listTemp);
indexLowTemp(listTemp);
// closes file
infile.close();
system("pause");
return 0;
}
void getData(int listTemp[rows][cols])
{ //reads data in both arrays
int x;
int j;
for (x=0; x < rows; x++)
{
for( j=0; j<cols; j++)
{
infile >> listTemp[x][j];
}
}
}
void averageHigh ( int listTemp[rows][cols])
{
int x=1;
int Sum = 0; //read first column and find the average
double Average;
for (x=0; x < rows; x++)
{
Sum = listTemp[x][0] + Sum;
// Average = Sum/x;
}
Average = Sum/x;
cout << "Average high for the year: " << Average << endl<<endl;
}
void averageLow ( int listTemp[rows][cols])
{
int Sum = 0; //read the second column and find the
double Average;
//average of the lows
for (int x=0; x < rows; x++)
{
Sum = listTemp [x][1] + Sum;
}
// Average = Sum/12;
Average = Sum/12;
cout << "Average low for the year: " << Average << endl<<endl;
}
void indexHighTemp ( int listTemp[rows][cols])
{
int highestIndex = 0;
//finds highest in the "high" column
for(int x = 0; x < rows; x++)
{
if(listTemp[0][x] > highestIndex)
highestIndex = listTemp[0][x];
}
cout << "The index high temperature is " << highestIndex <<endl<<endl;
}
void indexLowTemp ( int listTemp[rows][cols])
{
int lowestIndex = 0;
//finds the lowest in the low column
for(int x = 0; x < rows; x++)
{
if(lowestIndex > listTemp[0][x])
lowestIndex = listTemp[0][x];
}
cout << "The index low temperature is " << lowestIndex << endl<<endl;
}
In function indexHighTemp you have the rows and columns swapped in the array subscripts.
The same in indexLowTemp, in addition you need to set the initial value of lowestIndex to a figure bigger than any of the data, such as 999. Otherwise the initial value of 0 is the figure which is output.