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

1) Create an external file that has 10 lines with three float values on each lin

ID: 3711680 • Letter: 1

Question

1) Create an external file that has 10 lines with three float values on each line (representing 3 quiz grades).

2) Read the three values in that are on a line in from the external data file and store them in a 3 element float array. Pass the array to a function (using pointers) and find the average of the three values.

3) Assign a letter grade based on the calculated average:

A for a grade >=90 and <=100

B for a grade >=80 and <90

C for a grade >=70 and <80

D for a grade >=60 and <70

F for a grade >=0 and <60

4) Print out the 3 values that were read in from the input file, the calculated average, and an assigned letter grade to a second (newly created) data file.

This is an example of opening a file and reading information into an array.

#include <stdio.h>
main()
{
FILE *fp;
int i, count, numbers[25];
fp = fopen("C:\Users\Owner\Desktop\grades.txt", "r");
count = 0;
while(!feof(fp))
{
fscanf(fp,"%d", &numbers[count]);
count ++;
}
for(i=0; i<count; i++)
{
printf("This was in the file: %i ", numbers[i]);
}
fclose (fp);
}

C PROGRAMMING

Explanation / Answer

In the following C program, we open a file named "quizgrades.txt" in read mode that contains quiz marks of 10 students.

We read these values in the grade[] array using fscanf() in a while loop that runs 10 times for 10 records.

Then we get the average by calling the average function and according to that average, assign the lettergrade to the student.

Then, open another file called "result.txt" in append mode and write the three values in that file using fprintf().

CODE:

#include <stdio.h>

float average(float * grade)
{
   float avg, sum=0;
   int i;
   for(i=0; i<3; i++)
       sum += grade[i];
   avg = sum/3;
   return avg;      //returning average
  
}

int main()
{
FILE *fp, *fq;
int i, count;
float grade[3];
float avg;
char lettergrade;
fp = fopen("quizgrades.txt", "r");   //opening file to read scores

count = 0;
while(count<10)   //for 10 records
{
fscanf(fp,"%f %f %f", &grade[0], &grade[1], &grade[2]);       //reading 3 quiz grades
count ++;   //next student
printf(" %0.2f %0.2f %0.2f ", grade[0], grade[1], grade[2]);
avg = average(grade);   //call averagew function

//assigning grades to student
if(avg >=90 && avg <=100)
   lettergrade ='A';
else if(avg >=80 && avg <90)
   lettergrade ='B';
else if(avg >=70 && avg <80)
   lettergrade ='C';
else if(avg >=60 && avg <70)
   lettergrade ='D';
else
   lettergrade ='F';
  
fq = fopen("result.txt", "a");   //opening file to write in appened mode
//wrinting values to file:
fprintf(fq,"%0.2f %0.2f %0.2f %0.2f %c ", grade[0], grade[1], grade[2],avg,lettergrade);
}

fclose (fp);

fclose(fq);
}

FILES: graderesults:

77.8 80.0 65.5
66.5 70.5 88.0
81.4 56.5 50.8
55.0 87.0 90.0
76.0 80.5 81.0
86.0 55.8 69.5
73.0 74.6 82.5
67.6 80.0 55.5
90.5 87.8 92.0
78.8 82.1 76.5

results.txt:

77.80 80.00 65.50   74.43   C
66.50 70.50 88.00   75.00   C
81.40 56.50 50.80   62.90   D
55.00 87.00 90.00   77.33   C
76.00 80.50 81.00   79.17   C
86.00 55.80 69.50   70.43   C
73.00 74.60 82.50   76.70   C
67.60 80.00 55.50   67.70   D
90.50 87.80 92.00   90.10   A
78.80 82.10 76.50   79.13   C