82.8k views
3 votes
Suppose that a text file Exercise13_3.txt contains an unspecified number of scores.Write a c++ program that reads the scores from the file and display their total and average.Scores are seperated by blanks.

Instead of displaying the results on the screen, send the results to an output named using your last name.

In addition to finding the average and total, find the number of grades.

Input

Example: Contents of Exercise13_3.txt:

70 88 91 95

66 82 71 99 84 57

80 100 93 73 82

Output

Contents of YourLastName.txt:

Your first and last name

Number of grades = 16

Total of all grades = 1231

Average grade = 82.1

User Royson
by
4.7k points

2 Answers

3 votes

Final answer:

This detailed answer provides a C++ program that reads scores from a file, calculates their total, average, and count, and writes the output to another file named with the user's last name.

Step-by-step explanation:

The following C++ program demonstrates how to read scores from a text file, calculate the total, average, and number of scores, and write the output to a file named with the user's last name.

#include
#include
#include
using namespace std;

int main() {
ifstream inputFile("Exercise13_3.txt");
ofstream outputFile("YourLastName.txt");
int score;
int total = 0;
int count = 0;

while (inputFile >> score) {
total += score;
count++;
}

inputFile.close();

if(count > 0) {
outputFile << "Your first and last name\\";
outputFile << "Number of grades = " << count << "\\";
outputFile << "Total of all grades = " << total << "\\";
outputFile << "Average grade = " << fixed << setprecision(1) << (total / static_cast(count)) << "\\";
}
outputFile.close();

return 0;
}

This program uses streams from the fstream library to read from and write to files, and includes components like while loops and basic file I/O operations. Modify "YourLastName.txt" with the actual last name and replace "Your first and last name" with the user's actual name.

User Sachi Cortes
by
4.4k points
3 votes

Answer:

#include <iostream>

#include <cmath>

#include <math.h>

#include <fstream>

#include <string>

#include <numeric>

using namespace std;

int main()

{

ifstream infile;

float num;

float total;

float x;

float aver;

x=0; total=0;

infile.open("Exercise13_3.txt");

while(!infile.eof())

{

infile>>num;

total=total+num;

x++;

}

aver=(total-num)/(x-1);

cout << "Number of grades = " <<x<< "\\";

cout<< "Total of all grades = " <<total<<'\\';

cout<< "Average grade = "<<aver<<'\\';

cout<< ""<<'\\';

infile.close();

getchar();

return 0;

}

User Dan Polites
by
4.5k points