Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, here\'s a code lab question. Making the program simple would be awesome!

ID: 3666496 • Letter: H

Question

Hello, here's a code lab question. Making the program simple would be awesome! Thank You!

5.24: Using Files—Numeric Processing
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.com/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 the numbers: ", and "Average of the numbers: ".

Instructor Notes:

Here are all of the necessary output strings to assist you with this assignment.

Explanation / Answer

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 << " ";
cout << "Sum of the numbers: " << sum << " ";
cout << "Average of the numbers: " << average;

}

randomFile.close();
return 0;
}

If you need any further refinements, just get back to me.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote