I have a program that reads a file and inputs the information into an array. Then from that array I am to seperate the array according to a 10 point grade scale. This is the portion of the code I have so far regarding the sorting. Also, the array reads until a negative number. 50 is not the actual amount of elements, but rather the max amount. I've looked into case statements, but honestly do not know how to go about this grouping. Any suggestions?
int studID[50]; // declare program variables and arrays
float testScore[50];
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
for (count = 0; count < 50; count++)
{
if (studID[count] > 0 && testScore[count] >= 90.00)
count = a;
else if (studID[count] > 0 && testScore[count] >= 80.00)
count = b;
else if (studID[count] > 0 && testScore[count] >= 70.00)
count = c;
else if (studID[count] > 0 && testScore[count] >= 60.00)
count = d;
else count = f;
}
cout << "Students recieving a grade of A are: " << studID[a] << endl;
No a switch/case would help in this case since it deals only with single values and not ranges like in your case
You're almost done. Just change a little bit:
int studID[50]; // declare program variables and arrays
float testScore[50];
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
// Before this you need to fill 'studID' and 'testScore'for (count = 0; count < 50; count++)
{
// Do not modify count since that's your index (it'd better to name that differently)
// assuming that 'studID[count]' contains the number of students
if (studID[count] > 0 && testScore[count] >= 90.00)
a += studID[count];
elseif (studID[count] > 0 && testScore[count] >= 80.00)
b += studID[count];
elseif (studID[count] > 0 && testScore[count] >= 70.00)
c += studID[count];
elseif (studID[count] > 0 && testScore[count] >= 60.00)
d += studID[count];
else f += studID[count];
}
cout << "Students recieving a grade of A are: " << a << endl;
On the cout statement it only lists the totals of the student IDs that recieve that letter grade, rather than a list. How can I make a list when I don't even know how many elements will be included within the grouping?
Try a linked list (std::list). It's a handy way of adding an unknown amount of Elements to a list. It requires no 'premature' space allocation (thus no need to guess the amount, or allocate extra "safety" space).