![st3ph](/avatars/47359.jpg)
st3ph
14.04.2020 •
Computers and Technology
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.
Solved
Show answers
More tips
- S Science and Technology Discovering the Anatomy of an LCD TV Screen...
- F Family and Home How to Raise a Genius? Discover the Secrets Here...
- F Food and Cooking How to Properly Cook Buckwheat?...
- H Health and Medicine How to Tan in a Tanning Bed? Tips and Recommendations...
- S Style and Beauty How to Sew a Balloon Skirt: Detailed Tutorial and Tips on Choosing the Right Fabric...
- P Philosophy Personal attitude towards Confession: how to prepare and undergo the procedure correctly?...
- F Food and Cooking How to Make Cottage Cheese at Home: Simple and Quick Recipe with Step By Step Instructions...
- H Health and Medicine Which Water Are You Drinking? Is it Worth Buying Bottled Water?...
- H Health and Medicine What Makes a Man a Man?...
- H Health and Medicine What to Take with You to the Maternity Hospital?...
Answers on questions: Computers and Technology
- E English 1. A play that imitates elements of another story or genre is called a: theater script parody tragedy...
- M Mathematics Cross multiplication is helpful in: A. Quadratic equations B. Solving proportions C. Linear equations D. Word problems...
- A Advanced Placement (AP) Why do researchers study the brains of nonhuman animals?...
- P Physics Light travels at 3.0 × 108 m/s in a vacuum and slows to 2.0 × 108 m/s in glass. what is the index of refraction of glass?...
Ответ:
See explaination for code
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, "\n");
}
//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.\n");
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\n", "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;
}
Ответ: