Confused with arrays

Howdie,
I've been stumbling over this for a couple days and could use a little help. I only need pseudocode but trying to write it out regular has gotten me farther, even if that isn't very far. I need to total up the grades 35 students for 30 classrooms and output the total for each classroom. Using this I can get the total from one classroom.
1
2
3
4
5
6
for(id = 1; id <= 10; id++)
	{
		cout << "Enter score\n";
		cin >> score;
		sum = sum + score;
	}

The pseudocode will read from a file but because for this I have to enter by hand I shortened up the numbers.
What is tripping me up is how to get sum into classroom[0] and then increment it so the next time through the loop it goes into classroom[1] and so on.
Maybe I'm going about it all wrong and I'm most likely over thinking it but if someone could straighten me out I would appreciate it.
Thanks
A good start is to start the for loop variable (id) at 0 (array indices start at 0 and go until n-1 where n is the size). The conditional part of the for loop (the second statement) should be the variable is less than the array size. This prevents an out of range error. Once your for loop is set up, you can simple change it to this:
1
2
3
4
5
for (...) {
   cout << "Enter score\n";
   cin >> myGradeArray[id];
   sum += myGradeArray[id];
}


The cin statement allows you to enter the grade directly into the array. Each time the for loop loops, the index increases, preventing it from overwriting the last value. The sum += statement says take the value of sum, assign it to itself and add the value just entered. This will also prevent you from having to use a second variable (score).
Sorry,
Bad explanation on my part. The for loop is only to get the total for the classroom. What I need in the array, for the output, is the totals for each classroom, something like this:

_room 1 room2
score 87 98

Unless I'm missing something, what you said and what I had is only good for the first classroom. I need a loop outside this one to fill the array and that's where I'm lost. Something like:
1
2
3
4
int classroom [num];

	while (num < 34)
		for loop;

if that makes better sense.

I'm known for being thick headed so it's likely I'm over looking the obvious.

Sorry about the bad formatting also, it's only intended as a rough draft or outline but I should have known better than to post it that way.



Alright, than you would do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ...
// Loop for the classes
for (int i = 0; i < /* class array size */; i ++) {
   // Set the element equal to 0
   classArray[i] = 0;
   // Get the scores for class i
   for (int j = 0; j < /* number of scores to enter */; j ++) {
      cout << "Enter score #" << j + 1 << " for class #" << i + 1 << ": ";
      cin >> score;

      // Increment the element
      classArray[i] += score;
   }
   // classArray[i] now contains the sum of the grades
}


Anything else you're having issues with, let me know.
Last edited on
Thanks, but still having some problems with it.

It wont accept variable g unless I declare it outside of main(). Not sure why that is but after I enter the scores for the first classroom it jumps to the output and counts the first classroom wrong. The others come out as 0 like they should though.

If I comment out line 11, I don't have to declare g separately and it loops through all the inputs like it should. However, it outputs garbage. I get the same results if I get rid on the grade[] array, which I don't need, and just use sum also.

I can't figure out if it's something wrong with the input or if it's my output statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const int score = 5;
	const int total = 8;

	int room[score];
	int grade[total];
	int sum = 0;
	

	for (int g = 0; g < score; g++)

		room[g] = 0;
		
		for (int a = 0; a < total; a++)
		{
			cout << "Type score # " << a + 1 << "for class # " << g + 1 << ".\n";
			cin >> grade[a];
			sum += grade[a];

			room[g] += sum;
		}

		for (int g = 0; g< score; g++)
			cout << "\n\nscore for class # " <<g + 1 << "  " << room[g] << '\n'; 
Maybe you forgot this in the code you posted... maybe not. But, your missing curly braces for your first loop to wrap around everything you want to be looped.

The reason why it works when you comment out line 11 is because your first loop will only loop through the next single command which will then become your loop of a -> total.

If your first loop is going to be containing the other for loops I'd suggest changing the variable name of the third one from g to c (or which ever)

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int g = 0; g < score; g++)
{
	room[g] = 0;
		
	for (int a = 0; a < total; a++)
	{
		cout << "Type score # " << a + 1 << "for class # " << g + 1 << ".\n";
		cin >> grade[a];
		sum += grade[a];

		room[g] += sum;
	}

	for (int c = 0; c< score; c++)
		cout << "\n\nscore for class # " <<c + 1 << "  " << room[c] << '\n'; 
}


Regards,
Anthony
Good catch Anthony, I thought I had tried adding the braces and it didn't make a difference, just tried it again though and it fixed having to declare g separately and the looping. The third for is not part of that loop though and is only for final output.

However, it's still not counting right. If I give everyone in the first class a score of 1 the total ends up being 36 and if I do the same for the last class the total is around 500, they should both be 8. Before, when it only ran the first loop it output 36 also and 0 for the other classes so it's counting something, I just don't know what it's counting.
Topic archived. No new replies allowed.