Answer:
See explaination for code
Step-by-step explanation:
Program code:
#include<stdio.h>
#include<stdlib.h>
//definition of the function readFile()
//reads the data from the input file
void readFile(FILE *input, char firstName[], char lastName[], int scores[])
{
int i;
//Initializes the quiz scores to zero
for (i = 0; i < 10; i++)
scores[i] = 0;
//Read the first name then last name
fscanf(input, "%s %s", firstName, lastName);
//Read the 10 Quiz scores
for (i = 0; i < 10; i++)
fscanf(input, "%d", &scores[i]);
}
//definition of the function writeFile()
//write the data into the output file
void writeFile(FILE *output, char firstName[], char lastName[], int scores[])
{
int c, sum = 0;
float average;
int len1 = strlen(lastName);
int j = 0;
char name[20];
//assign the lastName to name array
for (j = 0; j < len1; ++j)
{
name[j] = lastName[j];
}
name[j] = ',';
j++;
//append the space after the comma in name array.
name[j] = ' ';
j++;
int k = 0;
//find the length of the firstName
len1 = strlen(firstName);
//append the firstName after the space in name array.
for (k = 0; k < len1; ++k, j++)
{
name[j] = firstName[k];
}
//assign the last character of name is null
name[j] = '\0';
//Writes Last name then first name with specified size and alignment
fprintf(output, "%-20s", name);
//Writes the 10 quiz scores with specified size
//and alignment and calculates the sum
for (c = 0; c < 10; c++)
{
fprintf(output, "%4d", scores[c]);
sum += scores[c];
}
//Calculates the average
average = (float)sum / 10;
//Writes the average
fprintf(output, "%10.2f", average);
//Writes the next line
fprintf(output, "\\");
}
//definition of the function copyIntoInputFile()
//copies the data from input file to output file
void copyIntoInputFile(FILE *from, FILE *to)
{
char data[130];
while (fgets(data, 130, from))
{
fprintf(to, "%s", data);
}
}
int main()
{
//open the input file in read mode
FILE *input = fopen("quiz.txt", "r");
//open the output file in write mode
FILE *output = fopen("average.txt", "w+");
//declare the variables
char scoresHeading[][4] = { "1", "2" , "3", "4", "5", "6", "7","8","9","10" };
char firstName[20], lastName[20];
int quiz[10], average, c = 0;
//Checks whether the file can able to open or not
if (input == NULL)
{
printf("Unable to open the file.\\");
return 0;
}
else
{
//print the heading the output file
fprintf(output, "%-20s", "Name");
for (int i = 0; i < 10; ++i)
{
//print the grades a right justified column that is 4 characters wide.
fprintf(output, "%4s", scoresHeading[i]);
}
//print average right justified column that is 10 characters wide
fprintf(output, "%10s\\", "Average");
//read the file
while (!feof(input))
{
//call the method readFile to read the file
readFile(input, firstName, lastName, quiz);
//call the method writeFile to write
writeFile(output, firstName, lastName, quiz);
}
}
//Close both the files
fclose(input);
fclose(output);
//open the files
input = fopen("average.txt", "r");
output = fopen("quiz.txt", "w+");
//call the function copyIntoInputFile
copyIntoInputFile(input, output);
return 0;
}