117k views
1 vote
Write a program that opens a text le called quiz.txt for reading. This le contains a single line with a student's rst name then one space, then the student's last name, then one space, then some number of quiz scores that, if they exists, are separated by one space. The student will have between zero and ten scores (inclusive), and each score is an integer between 0 and 100 (inclusive). The le may or may not end with a new line character. Your program will read the data from this le and write its output to a second le. The data in the output le (named average.txt) will contain the student's quiz scores followed by the average of the student's quiz scores. The output le must be formatted as described below. 1. Each quiz score should be listed in a right-justied column that is 4 characters wide. Note that if a student has fewer than 10 scores (they have missed one or more of the quizzes), your program will need to display the missing score(s) using 0 for each one. 2. The average should appear in its own column that is 10 characters wide. Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10. 3. The average should be computed with an accuracy of two decimal places.

User Darshanie
by
4.8k points

1 Answer

4 votes

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;

}

User Tom Withers
by
4.1k points