In: Computer Science
CODE -
#include<stdio.h>
void calculateAverages(float grades[][2], float averages[], int numStudents)
{
for(int i=0; i<numStudents; i++)
{
averages[i] = (grades[i][0] + grades[i][1]) / 2; // Calculating average grades for different students using for loop
}
}
int main()
{
int numStudents;
printf("How many students are there? ");
scanf("%d", &numStudents); // Taking no. of students as input from the user
// Creating arrays
float grades[100][2];
float averages[100];
// Loop to take grades as input from the user
for(int i=0; i<numStudents; i++)
{
printf("Please enter grade #1 for student #%d: ", i+1);
scanf("%f", &grades[i][0]);
printf("Please enter grade #2 for student #%d: ", i+1);
scanf("%f", &grades[i][1]);
}
calculateAverages(grades, averages, numStudents); // Calling function to calculate average grades
for(int i=0; i<numStudents; i++)
printf("The average grade for student #%d is %.2f\n", i+1, averages[i]); // Displaying average grades
return 0;
}
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.