JohnHuan 于 2017.05.20 12:31 提问

#include
#include
#include
#include
int main()
{
int nclasses = 0;

int nstudents_max = 0;

printf("How many students are in the largest class? :");
scanf_s("%d", &nstudents_max);
printf("How many classes are there? :");
scanf_s("%d", &nclasses);

``````int **grades = (int**)malloc(nclasses * sizeof(int*));                //Stores the grades
int *students = (int*)malloc(nstudents_max * sizeof(int));            //Stores the number of students in each class

for (int nclass = 0; nclass < nclasses; ++nclass)
{
printf("Enter the grades for students in class %d.\n", nclass + 1);
students[nclass] = 0;                                                //Student count within a class
while (true)
{
printf("Enter the integer grade for student %d: ", students[nclass] + 1);
if (++students[nclass] == nstudents_max)                            //Increment and check student count
{
printf("Class %d complete", nclass + 1);
break;
}
printf("Any more students in class %d (Y or N): ", nclass + 1);
break;
}
}

printf("\n");
for (int nclass = 0; nclass < nclasses; ++nclass)
{
int class_tatal = 0;
printf("Student grades for class %d are:\n", nclass + 1);
for (int student = 0; student < students[nclass]; ++student)
{
if ((student + 1) % 6 == 0)
printf("\n");
}
printf("\nAverge grade for class %d is %.2lf\n", nclass + 1, (double)class_tatal / students[nclass]);
}
return 0;
``````

}

2个回答

qq_36742141   2017.05.20 13:31

JohnHuan   2017.05.21 10:06