outputting multipule looping statements

I've been working on this program for school all day and after a lot of trial and error I have it working correctly except for the very last part. At the end I need to print out the lowest and highest of each quiz in this certain format. Now if it out of the loop it does not get the numbers but if it is in the loop it prints them out way to many. Any Idea's are always helpful

Also in the middle of the code I have the highestAverage figured out and working and partially printing, except it is printing out that student 5 has the highest when it should be student 3.

Let me know what you all think
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  #include<stdio.h>
#include<stdlib.h>
#define STUDENTS 5
#define QUIZZES 7
main()	{

	int quizScores[STUDENTS][QUIZZES] = {
		{ 90, 67, 45, 56, 77, 88, 45 },
		{ 65, 80, 87, 60, 55, 100, 78 },
		{ 90, 89, 88, 66, 100, 90, 99 },
		{ 54, 67, 80, 75, 45, 45, 87 },
		{ 78, 66, 48, 99, 61, 82, 95 }

	};
	int studentTotal = 0, quizTotal = 0, row, col, lowestQuiz[QUIZZES], highestQuiz[QUIZZES];
	double studentAverage[STUDENTS], quizAverage[QUIZZES], highestAverage;
	int i = 0;

	for (row = 0; row < STUDENTS; row++)	{
		studentTotal = 0;
		for (col = 0; col < QUIZZES; col++)	{
			studentTotal += quizScores[row][col];
		}
		studentAverage[row] = (double)studentTotal / QUIZZES;
		printf("Student %i had average %.2lf\n", row + 1, studentAverage[row]);

		if (row == 4)	{
			printf("\n");
		}
	}

	highestAverage = studentAverage[0];

	for (i = 1; i < STUDENTS; i++)	{
		if (highestAverage < studentAverage[i])	{
			highestAverage = studentAverage[i];
		}
	}

	printf("Student %d has the highest average with a score of %.2lf\n\n", i, highestAverage);

	for (col = 0; col < QUIZZES; col++)	{
		quizTotal = 0;
		for (row = 0; row < STUDENTS; row++)	{
			quizTotal += quizScores[row][col];
		}
		quizAverage[col] = (double)quizTotal / STUDENTS;
		printf("Quiz %i had average %.2lf\n", col + 1, quizAverage[col]);

		if (col == 6)	{
			printf("\n");
		}
	}

	printf("Quiz Grades Range\n");

	for (i = 0; i < QUIZZES; i++)	{
		for (col = i; col < QUIZZES; col++)	{
			lowestQuiz[i] = quizScores;
			for (row = 0; row < STUDENTS; row++) {
				if (lowestQuiz[i] > quizScores[row][col]) {
					lowestQuiz[i] = quizScores[row][col];
				}
			}
			i++;
		}
	}

	for (i = 0; i < QUIZZES; i++)	{
		for (col = i; col < QUIZZES; col++)	{
			for (row = 0; row < STUDENTS; row++) {
				if (highestQuiz[i] < quizScores[row][col]) {
					highestQuiz[i] = quizScores[row][col];
				}
			}
			i++;
		}
	}


	printf("Quiz 1:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);
	printf("Quiz 2:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);
	printf("Quiz 3:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);
	printf("Quiz 4:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);
	printf("Quiz 6:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);
	printf("Quiz 7:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);

	system("pause");
}
The average. You have:
1
2
3
4
for (i = 1; i < STUDENTS; i++) {
}
// the value of i after the loop is STUDENTS (i.e. 5)
printf("Student %d\n", i );  // prints 5 

You have to keep track of the winner and not just the max score.
1
2
3
4
5
int winner = 0;
for (i = 1; i < STUDENTS; i++) {
  // update winner if highestAverage changes
}
printf("Student %d\n", winner+1 );  // prints 5 

C++ has nifty standard library methods. For example:
1
2
int winner = std::distance( studentAverage, std::max_element( studentAverage, studentAverage+STUDENTS );
std::cout << "Student " << winner+1 << " has average " << highestAverage[winner] << '\n';

I presume that you are limited to C?


Now, the quiz stat question. You have triple loops and that makes no sense. I presume that you want to look at one quiz at a time and record both the lowest and highest score for it. Therefore:
Loop over the quizzes
  initialize the low and high of this quiz
  loop over the students
    update low if needed
    update high if needed

  print the result for this quiz OR

after the nested loop is complete
Loop over the quizzes
  print the result for this quiz

One of the problems with declaring all of the variables at the start is that it's easy to try to use them in contexts where they don't make sense, such as lines 81 to 86
 
    printf("Quiz 1:	%d - %d\n", lowestQuiz[i], highestQuiz[col]);

I'd recommend limiting the scope of variables as much as possible, by declaring them at the point where they are needed, so that they fall out of scope at the end of that block of code. For example,
1
2
    for (int qui=0; qui<QUIZZES; qui++)
         printf("Quiz %d:    %d - %d\n", qui+1, lowestQuiz[qui], highestQuiz[qui]); 

I used int qui and int stu as loop counters / array subscripts rather than i or row or col because it makes it easier to keep track of whether or not the right subscript is being used to access a particular array element.

Also, the code as it stands does not compile, there is a major error at line 59:
lowestQuiz[i] = quizScores; that is trying to assign the address of the array to one of the elements which doesn't make any sense, either to a human or to the compiler.
Last edited on
In for loops, you type the last condition like this:

for ( int qui = 0; qui < QUIZZES; ++qui)

You do the ++ or -- before the variable name.
Topic archived. No new replies allowed.