please help me .....

hi,
why after debugging program
windows says:
"Run-Time Check Failure #2 - Stack around the variable 'SD' was corrupted."
"break or continue"


I put here my code, please check it as fast as you can.
thank you.
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
  // my second project
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#include <math.h>

using namespace std;
void main()
{
	int n[4];// number of class students
	float grade[4][30],// grades
		  sum=0,       // sum
		  avg[4],      // average
		  var[4],      // variance
		  SD[4],       // std deviation
		  min = 0,     // minimum 
		  max = 0;     // maximum
	cout<<"\n	In The Name Of Allah"
		<<"\n	________________________________________________________\n";
	cout<<"\n	Rahmat programs\n"
		<<"	________________________________________________________\n"
		<<"\n	in this program,you enter grade of 4 classes"
		<<"\n	and then program tells you with class is Best class  or...\n";

	cout<<"\n	CLASSES STATUS"
		<<"	________________________________________\n";
	for( int i = 1 ; i <= 4 ; i++ )
	{
		cout<<"\n	enter number of class "<<i<<" students ( <=30 ): ";
		cin>>n[i];
	}
	system("cls");
	///////////////////////////////////////////////////////////////// class 1
	cout<<"\n\n	Class 1 :"
		<<"\n	________________\n";
	for ( int i = 1 ; i <= n[1] ; i++ )
	{
		cout<<"\n	enter grade :  ";
		cin>>grade[1][i];
		sum += grade[1][i];
	}
	avg[1] = sum / n[1];
	sum = 0 ;
	for (int k =1 ;k <= n[1];k++)
	{
		sum += (( avg[1] - grade[1][k] ) * ( avg[1] - grade[1][k] ));
	}
	var[1]= sum / n[1];
	SD[1] = sqrt(var[1]);
	min = SD[1];
	cout<<"\n	average is : "<<avg[1]<<"\n	variance is : "<<var[1]<<"\n	std deviation is : "<<SD[1];
	_getch();system("cls");
	///////////////////////////////////////////////////////////////// class 2
	cout<<"\n\n	Class 2 :"
		<<"\n	________________\n";
	sum = 0 ;
	for ( int i = 1 ; i <= n[2] ; i++ )
	{
		cout<<"\n	enter grade :  ";
		cin>>grade[2][i];
		sum += grade[2][i];
	}
	avg[2] = sum / n[2];
	sum = 0 ;
	for ( int k = 1 ; k <= n[2] ; k++ )
	{
		sum += ((avg[2] - grade[2][k] )* (avg[2] - grade[2][k] ));
	}
	var[2]= sum / n[2];
	SD[2] = sqrt(var[2]);
	cout<<"\n	average is : "<<avg[2]<<"\n	variance is : "<<var[2]<<"\n	std deviation is : "<<SD[2];
	_getch();system("cls");
	///////////////////////////////////////////////////////////////// class 3
	cout<<"\n\n	Class 3 :"
		<<"\n	________________\n";
	sum = 0 ;
	for ( int i = 1 ; i <= n[3] ; i++ )
	{
		cout<<"\n	enter grade :  ";
		cin>>grade[3][i];
		sum += grade[3][i];
	}
	avg[3] = sum / n[3];
	sum = 0 ;
	for ( int k = 1 ; k <= n[3] ; k++ )
	{
		sum += (( avg[3] - grade[3][k] ) * ( avg[3] - grade[3][k] ));
	}
	var[3]= sum / n[3];
	SD[3] = sqrt (var[3] );
	cout<<"\n	average is : "<<avg[3]<<"\n	variance is : "<<var[3]<<"\n	std deviation is : "<<SD[3];
	_getch();system("cls");
	///////////////////////////////////////////////////////////////// class 4
	cout<<"\n\n	Class 4 :"
		<<"\n	________________\n";
	sum = 0 ;
	for ( int i = 1 ; i <= n[4] ; i++ )
	{
		cout<<"\n	enter grade :  ";
		cin>>grade [4][i];
		sum += grade[4][i];
	}
	avg[4] = sum / n[4];
	sum = 0 ;
	for (int k = 1 ; k <= n[4] ; k++)
	{
		sum += ((avg[4] - grade[4][k] )* (avg[4] - grade[4][k] ));
	}
	var[4]= sum/n[4];
	SD[4] = sqrt (var[4]);
	cout<<"\n	average is : "<<avg[4]<<"\n	variance is : "<<var[4]<<"\n	std deviation is : "<<SD[4];
	_getch();system("cls");
	///////////////////////////////////////////////////////////////// finding minimum average
	cout<<"\n	RESULTS_______________________________________\n\n";
	for ( int i = 1  ; i <= 4 ; i++ )// for max
	{
		if ( avg[i] > max )
			max = avg[i];
	}
	if ( max == avg[1] )
		cout<<"\n	Better Class is : Class 1  \n";
	else if ( max == avg[2] )
		cout<<"\n	Better Class is : Class 2  \n";
	else if ( max == avg[3] )
		cout<<"\n	Better Class is : Class 3  \n";
	else if ( max == avg[4] )
		cout<<"\n	Better Class is : Class 4  \n";
	///////////////////////////////////////////////////////////////// finding minimum maximum of e.a.m.
	for ( int i = 1  ; i <= 4 ; i++ )// for min
	{
		if ( SD[i] < min )
			min = SD [i];
	}
	if ( min == SD[1] )
		cout<<"\n	The most consistent Class is : Class 1  \n";
	else if ( min == SD[2] )
		cout<<"\n	The most consistent Class is : Class 2  \n";
	else if ( min == SD[3] )
		cout<<"\n	The most consistent Class is : Class 3  \n";
	else if ( min == SD[4] )
		cout<<"\n	The most consistent Class is : Class 4  \n";
	////////////////////////////////////////////////////////////////// finding Best class 
	if ( max == avg[1] && min == SD[1] )
		cout<<"\n	Best class   is : Class 1";
	else if ( max == avg[2] && min == SD[2] )
		cout<<"\n	Best class  is : Class 2";
	else if ( max == avg[3] && min == SD[3] )
		cout<<"\n	Best class  is : Class 3";
	else if ( max == avg[4] && min == SD[4] )
		cout<<"\n	Best class  is : Class 4";
	else 
		cout<<"\n	error !!! program can not find Best class ";
	////////////////////////////////////////////////////////////////// 

	_getch();
}// main

That error usually means you are stepping outside of array bounds. And you are... just about everywhere.

Remember that array indexes start at ZERO not at one. So if you have an array with 4 elements... elements [0], [1], [2], and [3] are valid. [4] is not valid and is out of bounds.

So yeah... indexes should be 0-3, not 1-4 as they are in your code.
Topic archived. No new replies allowed.