View Single Post
Old Mar 26th, 2004, 10:11 PM   #13 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,696
Send a message via AIM to Ninety-Nine SE-L
hey again, I need help once more, this is a much easier program involving arrays, I can't get it to pop out of the second for loop, someone run it and tell me what's wrong. It compiles just fine.

Instructions:
enter in how many classes there are (lets say 3),
enter in how many students for class #1,
enter in all the grades for class #1,
averages all the grades for class #1, then finds how many are above average,
enter in how many students for class #2,
enter in all the grades for class #2,
averages all the grades for class #2, then finds how many are above average,
enter in how many students for class #3,
enter in all the grades for class #3,
averages all the grades for class #3, then finds how many are above average,

then it displays in this format:
Class 1 had 12 students, an average of 65 and 5 grades above average.
Class 2 had 6 students, an average of 71 and 3 grades above average.
Class 3...

Here's what I wrote:
Code:
/*
  March 26, 2004
  Program 4
*/

/* Program is supposed to read in grades for a given number of classes
   and output the averages for each class*/

#include <stdio.h>
#include <stdlib.h>

int main (void) {
	
	int classes, students[50], grades[50], averages[50], above[50];
	int i, j, k, x, y, z;
	
	printf("How many classes?\n");
	scanf("%d", &classes);
	printf("\n");
	
	for (i = 1; i <= classes; ++i){
		printf("How many students in class %d?\n", i);
		scanf("%d", &students[i]);
		printf("\n");
		
		printf("Input grades\n");
		for (j = 1; j <= students[i]; ++j){
			scanf("%d", &grades[j]);
			printf ("\n");
			x += grades[j];
		}

		y = x / students[i]; /*find average*/
		averages[i] = y; /*read average into array*/
		k = 0;
		for (j = 1; j <= students[i]; ++j){
			while (grades[j] > averages[i]){
				k += 1;
			}
			averages[i] = k;
		}
		printf("\n");
	}
	
	for (j = 1; j <= classes; ++j){
		printf("Class %d had %d students, an average of %d and %d grades above average.\n", j, students[j], averages[j], above[j]);
	}
	return 0;
}
Thanks for any help.
Ninety-Nine SE-L is offline   Reply With Quote