### Fatal error in a 2D array

Hello everyone!
I'm pretty new with c++ in general (first college class) and I just joined the forum today. I am having a problem with looping the total results of my 2D array and I keep getting a fatal error... Any help would be greatly appreciated! Thank-you!

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main ()
{
int i,k,length2=5;
int s2avg,s3avg,s4avg,s5avg,s6avg,s2tot,s3tot,s4tot,s5tot,s6tot;
char ch,ch2,ch3,ch4,ch5;
int scores[5][5]={{70,78,90,82,68},{46,59,61,70,49},{90,88,79,69,92},{89,87,92,93,83},{35,33,44,61,40}};
string s1=" & Name *********************************************************************";
string s2=" 1. Stella ";
string s3=" 2. Charles ";
string s4=" 3. Mike ";
string s5=" 4. Nichole ";
string s6=" 5. Ashden ";
cout<<" Student No. Exam-1 Exam2 Exam-3 Exam-4 Exam-5 Total score Average"<<endl;
cout<<endl;
cout<<s1<<endl;
cout<<endl;

for(k=0; k < length2;k++)
{
s2tot=scores[0][k];
}
for(k=0; k < length2;k++)
{
s3tot=scores[1][k];
}
for(k=0; k < length2;k++)
{
s4tot=scores[2][k];
}
for(k=0; k < length2;k++)
{
s5tot=scores[3][k];
}
for(k=0; k < length2;k++)
{
s6tot=scores[4][k];
}

s2avg=s2tot/5;
s3avg=s3tot/5;
s4avg=s4tot/5;
s5avg=s5tot/5;
s6avg=s6tot/5;

if(s2avg>=90)
{
ch=65;
}
else if(s2avg>=80)
{
ch=66;
}
else if(s2avg>=70)
{
ch=67;
}
else if(s2avg>=60)
{
ch=68;
}
else if(s2avg<=59)
{
ch=70;
}
if(s3avg>=90)
{
ch2=65;
}
else if(s3avg>=80)
{
ch2=66;
}
else if(s3avg>=70)
{
ch2=67;
}
else if(s3avg>=60)
{
ch2=68;
}
else if(s3avg<=59)
{
ch2=70;
}
if(s4avg>=90)
{
ch3=65;
}
else if(s4avg>=80)
{
ch3=66;
}
else if(s4avg>=70)
{
ch3=67;
}
else if(s4avg>=60)
{
ch3=68;
}
else if(s4avg<=59)
{
ch3=70;
}
if(s5avg>=90)
{
ch4=65;
}
else if(s5avg>=80)
{
ch4=66;
}
else if(s5avg>=70)
{
ch4=67;
}
else if(s5avg>=60)
{
ch4=68;
}
else if(s5avg<=59)
{
ch4=70;
}
if(s6avg>=90)
{
ch5=65;
}
else if(s6avg>=80)
{
ch5=66;
}
else if(s6avg>=70)
{
ch5=67;
}
else if(s6avg>=60)
{
ch5=68;
}
else if(s6avg<=59)
{
ch5=70;
}

string scores2[5][1]={{s2},{s3},{s4},{s5},{s6}};
cout<<scores2[0][0]<<scores[0][0]<<" "<<scores[0][1]<<" "<<scores[0][2]<<" "<<scores[0][3]<<" "<<scores[0][4]<<" "<<s2tot<<" "<<s2avg<<" "<<ch<<endl;
cout<<endl;
cout<<scores2[0][1]<<scores[1][0]<<" "<<scores[1][1]<<" "<<scores[1][2]<<" "<<scores[1][3]<<" "<<scores[1][4]<<" "<<s3tot<<" "<<s3avg<<" "<<ch2<<endl;
cout<<endl;
cout<<scores2[0][2]<<scores[2][0]<<" "<<scores[2][1]<<" "<<scores[2][2]<<" "<<scores[2][3]<<" "<<scores[2][4]<<" "<<s4tot<<" "<<s4avg<<" "<<ch3<<endl;
cout<<endl;
cout<<scores2[0][3]<<scores[3][0]<<" "<<scores[3][1]<<" "<<scores[3][2]<<" "<<scores[3][3]<<" "<<scores[3][4]<<" "<<s5tot<<" "<<s5avg<<" "<<ch4<<endl;
cout<<endl;
cout<<scores2[0][4]<<scores[4][0]<<" "<<scores[4][1]<<" "<<scores[4][2]<<" "<<scores[4][3]<<" "<<scores[4][4]<<" "<<s6tot<<" "<<s6avg<<" "<<ch5<<endl;
cout<<endl;

int scores3[5]={s2avg,s3avg,s4avg,s5avg,s6avg};
int max= scores3[0];
int length=5;
for(i = 1; i<length; i++)
{
if(scores3[i] > max)
max = scores3[i];
}
cout<<"The highest average student score was an "<<max<<"%"<<endl;
cout<<endl;

int min= scores3[0];
for(i = 1; i<length; i++)
{
if(scores3[i] < min)
min= scores3[i];
}
cout<<"The lowest average student score was an "<<min<<"%"<<endl;
cout<<endl;
return 0;
}
Last edited on
Variables s2tot, s3tot, s4tot, s5tot, s6tot were not initialized. So the following statements have undefined behavior.

s2avg=s2tot/5;
s3avg=s3tot/5;
s4avg=s4tot/5;
s5avg=s5tot/5;
s6avg=s6tot/5;
Wow... I forgot to set them equal to zero. Thank-you so much that did it! One other question. Is there any better way to do all of the if and else statements for my char in less space?
 Is there any better way to do all of the if and else statements for my char in less space?

Of course :)

The main thing is that you don't do things that you haven't learned in class though.

 ``12345`` ``````s2avg=s2tot/5; s3avg=s3tot/5; s4avg=s4tot/5; s5avg=s5tot/5; s6avg=s6tot/5;``````

Totals and averages can be an array, then you can loop and make one if/else statement.

Have you learned `switch`. Looks cleaner than if/else (it's also better)
 ``123456789`` ``````switch(grade / 10) { case 10: case 9: letter = 'A'; break; case 8: letter = 'B'; break; case 7: letter = 'C'; break; case 6: letter = 'D'; break; default: letter = 'F'; }``````
Last edited on
Sweet! I added those in to my program. Now it looks pretty solid! Thanks so much for the help! :)
Topic archived. No new replies allowed.