Caculate avarage from array values

i wrote following code to calculate average of the values entered to the array.After displaying the output following error was displayed.

"Run-Time Check Failure #2 - Stack around the variable 'marks' was corrupted.
A buffer overrun has occurred in q 3 410005111.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program. "

1
2
3
4
     //Average mark of student 1
		cout<<"Avarage mark of the student 1 :"<<(marks[0][0]+marks[1][0]+marks[2][0]+marks[3][0]);
		cout<<endl;
	
How have you declared marks?
"Run-Time Check Failure #2 - Stack around the variable 'marks' was corrupted., the buffer over run would mean that while writing to another variable the code has written outside of where it should, the only way for us to help is for you to post more of your code.
ok, Here is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<conio.h>

using namespace std;

void main(){

	//Declaring array marks
	int marks[3][9];
	int p=0,y=0,t=0,o=0; //Declaring intigers and assign values
	int b=0;
	


}
Last edited on
You've initialised marks as:
 
	int marks[3][9];


This means it's a 3 x 9 array. Valid indices for the first dimension are 0 - 2, and for the second dimension are 0 - 8.

At various points in your code, you're attempting to set values using an index of 3 for the first dimension, and 9 for the second.


I commented various things throughout the code. And I would highly suggest not repeating similar code. Those ten different students are all easily combined together with an additional for loop. I also added two #define s. They simplify remembering a number and make it easy to change later.
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
#include<iostream>
//#include<conio.h>

using namespace std;

#define NUM_STUDENTS 10
#define NUM_SUBJECTS 4

//use int main, not void main
int main(){

	//Declaring array marks
	int marks[NUM_SUBJECTS][NUM_STUDENTS]; //this array needs to be 4*10 instead of 3*9

        //these single letter variables are all unused now
	//int p=0,y=0,t=0,o=0; //Declaring intigers and assign values
	//int b=0;
	
	
	cout<<"Enter zero if student didnot sit a paper"<<endl; //print instruction for user
	
    for(int j = 0; j < NUM_STUDENTS; ++j)
    {
        cout<<"Enter the marks of student " << j+1 << ":"<<endl; //Taking user input for student j+1
        
        for(int i=0;i<NUM_SUBJECTS;i++){
            cout<<"subject "<<i+1<<": ";
            int temp;
            cin>> marks[i][j];
        }
    }
    
	cout<<endl;

	//Displaying values entered to the array
	for(int j=0;j<NUM_SUBJECTS;j++)
		for(int k=0;k<NUM_STUDENTS;k++){
			cout<<"Array["<<j<<"]["<<k<<"] :";
			cout<<marks[j][k]<<endl;
		}


   //Counting number of student in each subject
   int subject_counts[NUM_SUBJECTS]; //for all 4 subjects
    
    for(int s = 0; s < NUM_SUBJECTS; ++s)
    {
        subject_counts[s] = 0;
		for(int n=0;n<NUM_STUDENTS;n++){
			if(marks[s][n] > 0)
				subject_counts[s]++;
        }
		cout<<"Number of student sat for the subject " << s+1 << " :"<<subject_counts[s]<<endl;
    }

    //Average mark of student 1
		cout<<"Avarage mark of the student 1 :"<<(double(marks[0][0]+marks[1][0]+marks[2][0]+marks[3][0]) / 4.0);
		cout<<endl;
	

	//_getch(); //I personally do not endorse any usage of conio.h
    cin.get();


}
Last edited on
Topic archived. No new replies allowed.