Program just stops

closed account (z8q4izwU)
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







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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#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;		 
			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.
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;
}
closed account (z8q4izwU)
Thanks for the help!
Topic archived. No new replies allowed.