grouping from an array

Feb 16, 2012 at 4:49am
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;
Feb 16, 2012 at 8:45am
Please use code tags: [code]Your code[/code]

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:
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
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];
else if (studID[count] > 0 && testScore[count] >= 80.00)
b += studID[count];
else if (studID[count] > 0 && testScore[count] >= 70.00)
c += studID[count];
else if (studID[count] > 0 && testScore[count] >= 60.00)
d += studID[count];
else f += studID[count];
}

cout << "Students recieving a grade of A are: " << a << endl;
Feb 16, 2012 at 5:10pm
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?
Feb 16, 2012 at 5:21pm
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).
Topic archived. No new replies allowed.