If you have downloaded this book’s source code from the companion Web site, you will find a file named Random.txt in the Chapter 05 folder. (The companion Web site is at www.pearsonhighered/gaddis.) This file contains a long list of random numbers. Copy the file to your hard drive and then write a program that opens the file, reads all the numbers from the file, and calculates the following: A) The number of numbers in the file B) The sum of all the numbers in the file (a running total) C) The average of all the numbers in the file The program should display the number of numbers found in the file, the sum of the numbers, and the average of the numbers. Prompts And Output Labels: Print each of the above quantities on a line by itself, preceding by the following (respective) labels: "Number of numbers: ", "Sum of the numbers: ", and "Average of the numbers: ".

Respuesta :

Answer:

Check the explanation

Explanation:

Here is the code for you:

#include <iostream>

#include <fstream>

using namespace std;

int main ()

{

int aNumber=0;

int numbers=0;

double sum=0.0;

double average=0.0;

ifstream randomFile;

randomFile.open("Random.txt");

if (randomFile.fail())

cout << "failed to read file.";

else

{

while (randomFile >> aNumber)

{

numbers++;

sum+=aNumber;

}

if (numbers>0)

average = sum/numbers;

else

average=0.0;

cout << "Number of numbers: " << numbers << "\n";

cout << "Sum of the numbers: " << sum << "\n";

cout << "Average of the numbers: " << average;

}

randomFile.close();

return 0;

}

In this exercise we have to use the computer language knowledge in C++ to write the code as:

the code is in the attached image.

In a more easy way we have that the code will be:

#include <iostream>

#include <fstream>

using namespace std;

int main ()

{

int aNumber=0;

int numbers=0;

double sum=0.0;

double average=0.0;

ifstream randomFile;

randomFile.open("Random.txt");

if (randomFile.fail())

cout << "failed to read file.";

else

{

while (randomFile >> aNumber)

{

numbers++;

sum+=aNumber;

}

if (numbers>0)

average = sum/numbers;

else

average=0.0;

cout << "Number of numbers: " << numbers << "\n";

cout << "Sum of the numbers: " << sum << "\n";

cout << "Average of the numbers: " << average;

}

randomFile.close();

return 0;

}

See more about C++ code at brainly.com/question/25870717

Ver imagen lhmarianateixeira