Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A text-file scores.txt contains the numeric grades of a certain section. The num

ID: 3576656 • Letter: A

Question

A text-file scores.txt contains the numeric grades of a certain section. The number of grades in the file is not known; but it cannot be more than 30. Write a C program that creates a double 1D array of size 30, it then passes this array as an output parameter to a function getGradeResults that uses an EOF sentinel-controlled loop to read the grades from the text-file into the passed array. The function also returns the count of the grades read and their average.

The main function then displays the returned average, the grades in the returned array, and the count of the number of grades above the average.

Sample scores.txt

50.0         60.0

40.0         30.5        70.0       80.0

65.5

25.0         55.0

20.0

Sample program runs:

  

50.0         60.0

40.0         30.5        70.0       80.0

65.5

25.0         55.0

20.0

Average 49.6 The grades are 50.0 60.0 40.0 30.5 70.0 80. 0 65.5 25.0 55.0 20.0 Number of grades above the average 6

Explanation / Answer

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

void getGradeResults(FILE *f1, double arr[], int *count_grades, double *average)
{
*count_grades = 0;
double sum = 0.0;


while(fscanf(f1, "%lf", &arr[*count_grades]) != EOF) //read file upto end and store the values into array
{
sum += arr[*count_grades];
*count_grades = *count_grades + 1;
}
//calculating average
*average = sum / (*count_grades);
}

int main()
{
double arr[30];
int i,count_grades,count_grades_above_average=0;
double average;
FILE *f1;
f1 = fopen("scores.txt","r");

getGradeResults(f1,arr,&count_grades,&average); //passing address of grade count and average

printf("Average = %.2f ",average);

printf(" The grades are:");
for(i = 0; i < count_grades; i++)
{
printf("%.1f ",arr[i]);

if(arr[i] > average)
count_grades_above_average++;
}
printf(" Number of grades above the average = %d ", count_grades_above_average);
return 0;
}

output is below

Average = 49.60

The grades are:50.0 60.0 40.0 30.5 70.0 80.0 65.5 25.0 55.0 20.0
Number of grades above the average = 6

Process returned 0 (0x0) execution time : 0.053 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote