In: Computer Science
Write a C program that calculates the average grade for a specified number of students from each student's test1 and test2 grades. The program must first ask the user how many students there are. Then, for each student, the program will ask the user for the test1 grade (grade #1) and test2 grade (grade #2). The program should be able to handle up to 100 students, each with 2 grades (test1 and test2). Use a two-dimensional float array to store the grades. Then, using a loop, prompt the user for the test1 and test2 grade of each student. Create a second single-dimensional float array to hold the average grade for each student (for up to 100 students). To calculate the average grade, pass both the two-dimensional array and the single-dimensional array to a function named void calculateAverages(float grades[][2], float averages[], int numStudents). The third parameter of the function indicates the actual number of students. The function will then use a loop to calculate the average grade for each student. Remember, since averages is an array parameter, any changes to averages is changing the original array. When the function returns to main, the program (in main) should display the average grade for each student (using a loop).
#include<stdio.h>
void calculateAverages(float grades[][2], float averages[], int numStudents)
{
int i;
//CALCULATING THE AVERAGE GRADE OF EACH STUDENT
for(i = 0; i < numStudents; i++)
{
averages[i] = (float)((grades[i][0] + grades[i][1])/2);
}
}
int main()
{
int numStudents,i;
float grades[100][2],averages[100];
printf("Enter the number of students: ");
scanf("%d", &numStudents); //INPUT NUMBER OF STUDENTS FROM USER
//TAKING INPUT FROM USER
for(i = 0; i < numStudents; i++)
{
printf("\nStudent %d: \n", i+1);
printf("Enter grade for test 1: ");
scanf("%f", &grades[i][0]);
printf("Enter grade for test 2: ");
scanf("%f", &grades[i][1]);
}
calculateAverages(grades, averages, numStudents);
//PRINTING AVERAGE GRADES OF ALL STUDENTS
printf("\n");
for(i = 0; i < numStudents; i++)
{
printf("Average grade for Student %d: %0.2f\n", i+1, averages[i]);
}
}
IF YOU LIKED THE ANSWER, PLEASE UPVOTE