C++ Run Time Check Failure

closed account (36q54iN6)
I dont know why the compiler sent me the Run-Time Check Failure in Option 2, 5, 6. Why the compiler do that ?

I already try to solve it but I cant :(
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
#include <cmath>
#include <iomanip>
using namespace std;

const int rows = 2;
const int columns = 2;

//prototipos:
int getTotal (int[][columns]);
//getAverage
double getAverage (int);
//getRowTotal
int getRowTotal(int[][columns],int);
//getColumnTotal
int getColumnTotal(int[][columns],int);
//getAverage
double getAverage(int);
//getHighestInRow
int getHighestInRow(int[][columns],int);
//getLowestInRow
int getLowestInRow(int[][columns],int);

int main ()
{
	int a,b,value,userChoice;

	int num[rows][columns] = {15,30,45,60};
	cout<< "Bi-Dimensional Array Numbers:\n"<<endl;

	for(a=0;a<rows;a++)
	{
		for(b=0;b<columns;b++)
			cout<<num[a][b]<<"\t";
		cout<<endl;
	}

	//userMenu
	for(;;)
	{
		cout<<"\n"<<"Press '1' for Array Total."<<endl;
		cout<<"Press '2' for Array Average."<<endl;
		cout<<"Press '3' for Row Total."<<endl;
		cout<<"Press '4' for Column Total."<<endl;
		cout<<"Press '5' to find Highest Value in a Row."<<endl;
		cout<<"Press '6' to find Lowest Value in a Row."<<endl;
		cout<<"Select Your Choice: ";
		cin>>userChoice;

		//array Total
		if(userChoice == 1)
		{
			cout<<"\n"<<"Array Total is: "<<getTotal(num)<<endl;
		}

		//arrayAverage
		else if(userChoice == 2)
		{
			cout<<"\n"<<"Array Average is: "<<getAverage(value)<<endl;
		}

		//rowTotal
		else if(userChoice == 3)
		{
			cout<<"\n Row Total \n"<<endl;
				cout<<"Please Enter a Row Number: ";
				cin>>a;
				if(a<0||a>rows)
				{
					cout<<"Invalid Input!!"<<rows<<" Please make another selection:\n";
				}
					
				else 
					cout<<"The Value of Row "<<a<<" is: "<<getRowTotal(num,a)<<endl;
		}
	
		//columnTotal
		else if(userChoice == 4)
		{
			cout<<"\n Column Total \n"<<endl;
			cout<<"Please Enter a Column Number: ";
				cin>>a;
				if(a<0||a>columns)
				{
					cout<<"Invalid Input!!"<<columns<<" Please make another selection:\n";
				}

				else
					cout<<"The Value of Column "<<a<<" is: "<<getColumnTotal(num,a)<<endl;
		}

		//High Value in a Row
		else if(userChoice == 5)
		{
			cout<<"Please Enter a Row Number: ";
				cin>>a;
				if(a<0||a>rows)
				{
					cout<<"Invalid Input!!"<<rows<<" Please Make Another Selection:\n";
				}
				
				else
					cout<<"Highest Value in a Row "<<a<<" is: "<<getHighestInRow(num,a)<<endl;
		}

		//Lowest Value in a Row
		else if(userChoice == 6)
		{
			cout<<"Please Enter a Row Number: ";
				cin>>a;
				if(a<0||a>rows)
				{
					cout<<"Invalid Input!!"<<rows<<" Please Make Another Selection:\n";
				}

				else
					cout<<"Lowest Value in a Row "<<a<<" is: "<<getLowestInRow(num,a)<<endl;
		}

		system("pause");
		return 0;
	}
}

//definicion total
int getTotal(int row[][columns])
{
	int a,b,value=0;

	for(a=0;a<rows;a++)
		for(b=0;b<columns;b++)
	 value+=row[a][b];
	return value;
}

//definicion average
double getAverage(int totalValues)
{
	double value;
	value=totalValues/(rows*columns);
	return value;
}

//definicion row total
int getRowTotal(int num[][columns],int n)
{
	int a,value=0;

	for(a=0; a<columns;a++)
		value+=num[n][a];
	return value;
}

//definicion suma columnas
int getColumnTotal(int num[][columns],int n)
{
	//variables
	int a,value=0;

	for(a=0; a<rows;a++)
		value+=num[a][n];
	return value;
}

//definicion valor mas grande en fila
int getHighestInRow(int num[][columns],int n)
{
	int a,value=0;
	value=num[n][0];

	for(a=1;1<columns;a++)
		if(num[n][a]>value)
			value=num[n][a];
	return value;
}

//definicion valor menor en fila
int getLowestInRow(int num[][columns],int n)
{
	int a,value=0;
	value=num[n][0];

	for(a=1;1<columns;a++)
		if(num[n][a]<value)
			value=num[n][a];
	return value;
}
line 59: value is uninitialized.

BTW, getAverage doesn't do what the function name says. It simply returns the number of elements in the array.

line 171, 183: termination condition should be a<columns, not 1<columns
This will cause you to subscript outside the array.

'5' and '6' have array overruns, you are lucky to get a run time error and not just a program crash.

Note that columns is meaningless and ignored in the following context:
int getLowestInRow(int num[][columns],int n)
it will be changed to:
int getLowestInRow(int num[][],int n)
Topic archived. No new replies allowed.