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];
}
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;
}
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?
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)
1 2 3 4 5 6 7 8 9
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';
}