2 Dimensional Arrays HELP URGENT!!

See this program is used to enter 3 marks of 4 students and calculate their grade with their averages. Now the problem is as soon as it go to enter the 3rd mark of 3rd student console error pops out and the program crashes!! Try it out in your computer if my computer is the problem..

Note: Use Same Functions

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
#include<iostream>
int main()
{using namespace std;
int i,j,k,av,s=0,marks[4][3];
for(i=0;i<4;i++)
{for(j=0;j<3;j++)
{cout<<"Enter the mark "<<j+1<<" for student "<<i+1;
cout<<endl;
cin>>marks[i][j];
cout<<endl;
}
for(j=0;j<3;j++)
{
s=s+marks[i][j];
av=s/3;
}
s=0;
cout<<"Average of student = "<<av<<endl;
if(av>=75)
{cout<<" Grade = 'A'"<<endl<<endl;
}
else if(av>=60&&av<=74)
{cout<<" Grade = 'B'"<<endl<<endl;
}
else(cout<<"Grade = 'C'"<<endl<<endl);
}
}
	
	
Last edited on
Got something against whitespace? The point for formatting your code is so you can verify what you've written. The stuff you've written is impossible to decipher, for the sake of a few space characters you can make it easy to read and verify.
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
#include <iostream>

int main()
{
	using namespace std;

	int av,s=0,marks[4][3];

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << "Enter the mark " << j + 1 <<" for student " << i + 1;
			cout << endl;
			cin >> marks[i][j];
			cout << endl;
		}

		for (int j = 0; j < 3; j++)
		{
			s = s + marks[i][j];
			av = s/3;
		}

		s = 0;
		cout << "Average of student = " << av << endl;
		if (av >= 75)
		{
			cout << " Grade = 'A'" << endl << endl;
		}
		else if (av >= 60 && av <= 74)
		{
			cout << " Grade = 'B'" << endl << endl;
		}
		else
			cout << "Grade = 'C'" << endl << endl;
	}
}


Now that it's formated, it's clear that the code is ok.
Last edited on
The program did not crash for me. However, you probably want to move av = s/3; on line 15 outside of the j loop - you only need to calculate the average once after you've finished summing.
Last edited on
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
#include<iostream>
int main()
{
using namespace std;
int i,j,k,av,s=0,marks[4][3];

for(i=0;i<4;i++)
{
	for(j=0;j<3;j++)
	{	
		cout<<"Enter the mark "<<j+1<<" for student "<<i+1;
		cout<<endl;
		cin>>marks[i][j];
		cout<<endl;
	}

	for(j=0;j<3;j++)
	{
		s=s+marks[i][j];
		///av=s/3; 
	}
	av=s/3; ///
	
	s=0;
	cout<<"Average of student = "<< av << endl;
	if(av>=75)
	{
		cout<<" Grade = 'A'"<<endl<<endl;
	}
	else if(av>=60&&av<=74)
	{
		cout<<" Grade = 'B'"<<endl<<endl;
	}
	else(cout<<"Grade = 'C'"<<endl<<endl);
}

return 0;
}
Topic archived. No new replies allowed.