| forsakensun12 (18) | |||
|
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? Heres my input data that i have in notepad 38 27 42 29 50 35 61 45 71 54 79 64 84 69 83 68 75 61 64 50 54 42 32 32
| |||
|
|
|||
| Chervil (806) | |
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.
| |
|
|
|
| shahbazsaleem (14) | |
|
This is working code if you have any problem to understand it tell me #include<iostream> #include<fstream> #include<iomanip> const int NO_OF_MONTHS = 12; const int rows = 12; //The rows and collums are constant const int cols = 2; using namespace 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; double 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]) { double 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 = listTemp[0][0]; //finds highest in the "high" column for(int x = 0; x < rows; x++) { if(listTemp[x][0] > highestIndex) highestIndex = listTemp[x][0]; } cout << "The index high temperature is " << highestIndex <<endl<<endl; } void indexLowTemp ( int listTemp[rows][cols]) { int lowestIndex = listTemp[0][1]; //finds the lowest in the low column for(int x = 0; x < rows; x++) { if(lowestIndex > listTemp[x][1]) lowestIndex = listTemp[x][1]; } cout << "The index low temperature is " << lowestIndex << endl<<endl; } | |
|
|
|
| forsakensun12 (18) | |
|
Thanks for the help! | |
|
|
|