1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
leva [86]
3 years ago
15

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.
Computers and Technology
1 answer:
musickatia [10]3 years ago
7 0

Answer:

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;

}

You might be interested in
Your program is going to compare the distinct salaries of two individuals for the last 5 years. If the salary for the two indivi
kirza4 [7]

In python:

i = 1

lst1 = ([])

lst2 = ([])

while i <= 5:

   person1 = int(input("Enter the salary individual 1 got in year {}".format(i)))

   person2 = int(input("Enter the salary individual 1 got in year {}".format(i)))

   lst1.append(person1)

   lst2.append(person2)

   i += 1

if sum(lst1) > sum(lst2):

   print("Individual 1 has the highest salary")

else:

   print("Individual 2 has the highest salary")

This works correctly if the two individuals do not end up with the same salary overall.

4 0
3 years ago
Use your editor to open the cc_data.js file and study the data stored in the staff object to become familiar with its contents a
bezimeni [28]

Answer:Use your editor to open the tny_july_txt.html and tny_timer_txt.js files from the ... 3 Take some time to study the content and structure of the file, paying close ... the nextJuly4() function, insert a function named showClock() that has no parameters. ... Declare a variable named thisDay that stores a Date object containing the ...

Explanation:

6 0
3 years ago
All presentations should have this. A. a clear central message. B. a design template. C. at least seven slides. D. special effec
Oksi-84 [34.3K]

Answer:

A. a clear central message.

When creating a presentation on basically anything, there should always be a clear central message. The purpose of most presentations are to give information on what the presenter would like to convey to the audience members. Without a clear central message, the audience members would be lost and the presentations purpose will not be fulfilled.

4 0
3 years ago
Read 2 more answers
Please tell fast plzzzzzz. ​
shepuryov [24]

Answer:

true....................

6 0
3 years ago
Read 2 more answers
Polls shows that most Americans....
Len [333]
I dont know whats the contexts behind this uestion but i think the answer is B
6 0
3 years ago
Other questions:
  • Why does the PC send out a broadcast ARP prior
    14·1 answer
  • He ____________ may include a high-level WBS in a graphic chart format or as an indentured list of the work elements and associa
    14·1 answer
  • I’m which part of a profession email should you try to be brief, but highly descriptive
    7·1 answer
  • American Standard Code for Information Interchange (ASCII) would be classified at which layer of the OSI Model?
    13·1 answer
  • Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scor
    5·1 answer
  • Karen wants to create a program that will allow the user to input their age. Which of these lines of code should be used?
    11·1 answer
  • Task 1: Alex has created the following code using Scratch and expected it to move backwards and forwards across the screen. Howe
    13·1 answer
  • Please help!! i need this asap &lt;3 <br> (the boxes have the same answer choices in both)
    7·2 answers
  • What is Colby Knox known for?
    15·2 answers
  • Who does online school on FLVS???????????????????
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!