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;
"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.
#include<iostream>
#include<conio.h>
usingnamespace 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;
}
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.
#include<iostream>
//#include<conio.h>
usingnamespace 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();
}