Mar 18, 2011 at 9:28pm UTC
I'm doing a program that outputs average high/low temps and the highest/lowest temp.
They all work except for lowest temp which always comes out 0. I don't know what to do. Any help would be appreciated. The problem function is indexLowTemp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include<iostream>
#include<iomanip>
int listTemp[12][2];
using namespace std;
void getData(int listTemp[12][2]);
void averageHigh(int listTemp[12][2]);
void averageLow(int listTemp[12][2]);
void indexHighTemp(int listTemp[12][2]);
void indexLowTemp(int listTemp[12][2]);
int HIGH = 0;
int LOW = 1;
int main()
{
double average;
int listTemp[12][2];
getData(listTemp);
averageHigh(listTemp);
averageLow(listTemp);
indexHighTemp(listTemp);
indexLowTemp(listTemp);
system("pause" );
return 0;
}
void getData(int listTemp[12][2])
{
int x;
cout << "Enter the HIGH monthly temperatures:" << endl;
for (x = 0; x < 12; x++)
{
cout << "Month " << (x + 1) << ": " ;
cin >> listTemp[x][HIGH];
}
cout << endl << "Enter the LOW monthly temperatures:" << endl;
for (x = 0; x < 12; x++)
{
cout << "Month " << (x + 1) << ": " ;
cin >> listTemp[x][LOW];
}
}
void averageHigh (int listTemp[12][2])
{
int x;
int Sum = 0;
double Average;
for (x = 0; x < 12; x++)
{
Sum = listTemp[x][0] + Sum;
}
Average = Sum/x;
cout << "Average high for the year: " << Average << endl<<endl;
}
void averageLow (int listTemp[12][2])
{
int Sum = 0;
int x;
double Average;
for (x = 0; x < 12; x++)
{
Sum = listTemp [x][1] + Sum;
}
Average = Sum/12;
cout << "Average low for the year: " << Average << endl<<endl;
}
void indexHighTemp (int listTemp[12][2])
{
int highestIndex = 0;
int x;
for (x = 0; x < 12; x++)
{
if (listTemp[0][x] > highestIndex)
highestIndex = listTemp[0][x];
}
cout << "The index high temperature is " << highestIndex <<endl<<endl;
}
void indexLowTemp (int listTemp[12][2])
{
int lowestIndex = 0;
int x;
for (x = 0; x < 12; x++)
{
if (listTemp[0][x] < lowestIndex)
lowestIndex = listTemp[0][x];
}
cout << "The index low temperature is " << lowestIndex << endl<<endl;
}
Edit- I changed some things around
Last edited on Mar 18, 2011 at 10:58pm UTC
Mar 18, 2011 at 9:46pm UTC
In the protoype of indexLowTemp, you declare the argument indexLowTemp[cols][cols]
whereas you actual function is indexLowTemp[rows][cols]
.
Last edited on Mar 18, 2011 at 9:46pm UTC
Mar 18, 2011 at 9:48pm UTC
Yeah, I caught that after I posted. It still didn't work though.
Mar 18, 2011 at 9:49pm UTC
Did you just fix it or did I read it wrong?
Mar 18, 2011 at 11:00pm UTC
int lowestIndex = 0;
If all the temps are > 0, you won't ever modify it. Just set it to the be the first temperature in the row.