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
TP is commonly used to __________ and __________ files to a server.
Goshia [24]

Answer:

upload; download

Explanation:

This question is not complete: is not TP

Is FTP is an Internet protocol commonly used to upload and download files to a server, For example:

There are image banks where you can sell or share files like videos, music, and photographs, in this kind of website, you can transfer files for FTP, in FTP we must type a user name and password to send the files.

8 0
3 years ago
The indent buttons on the home tab allow you to increase or decrease paragraph indenting in increments of ____ inches.
TEA [102]
<span>0.5 inches.
You may also manually set the indentation. In order to do this, click the "Page Layout" tab and enter the desired values in the boxes under "Indent". The indentation of a text is the distance it has from the left margin (when writing from left to right).</span>
3 0
3 years ago
Which of the following are options when using the Select tool in Paint? (Select all that apply.)
meriva
I believe the answer is all of them except picture selection<span />
3 0
3 years ago
Match the definitions to their respective cli hot keys and shortcuts. (not all options are used.)
lord [1]
  • Tab ( Complete omitted commands and parameters )
  • Space bar ( Display the next screen )
  • Up arrow ( Scroll backward through previously entered commands )
  • ? ( Provides context-sensitive help )
  • Ctrl Shift 6 ( Cancel commands such as trace and ping).

What is CLI tools ?

A command line interface (CLI) is a text-based user interface (UI) used to run programs, manage computer files, and interact with a computer. A command line interface is also known as a command line user interface, console user interface, or character user interface.

Why do we use CLI tools ?

The CLI is a command line utility that accepts text input and executes operating system functions. In the 1960s,  this was the only way to interact with a computer, as only computer terminals were in use. In the 1970s and 1980s, command line input was widely used on Unix systems and personal computer systems such as MS-DOS and Apple DOS.

To know more about CLI Tools visit here:

brainly.com/question/13263568

#SPJ4

4 0
1 year ago
How to copy and paste things that won't let you?
VLD [36.1K]
You take the ctrl buutton and hold it down while awlleclting the other setion you wouo would lieke to highlight.
5 0
3 years ago
Other questions:
  • Lisa adds her co-worker Renald to a meeting and removes her secretary Olivia from the meeting. What will happen as a result?
    14·2 answers
  • Melissa and Sue want to distribute a document to be discussed at a meeting starting in about thirty minutes. People will be atte
    14·1 answer
  • To generate a report with exact results based on specific criteria it is best to base the report on a(n) ____________________ cr
    6·1 answer
  • Write a C function which mimics the behavior of the assembly language function below. Note that this time, the assembly language
    10·1 answer
  • A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never
    7·1 answer
  • What game is this? help mee?
    8·2 answers
  • Write a recursive function is_pow2(n) that returns True if the positive integer n is an integer power of 2, and False otherwise.
    9·1 answer
  • Select the correct answer. Nancy has made a small web page with the new features of HTML5. She has to show this web page in scho
    9·1 answer
  • When scriptwriters are writing scripts, why do they have to write them in accordance with industry standards?
    9·1 answer
  • Which of the mis systems uses both informal and formal information-gathering procedures?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!