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

This problem is a modification of Problem 24 on page 299 of the Gaddis text with

ID: 3566746 • Letter: T

Question

This problem is a modification of Problem 24 on page 299 of the Gaddis text with two small additions. (A scan of this problem can be found on the last page of the assignment.)

In this assignment, your program will read in a list of random numbers from an input file, random.txt, and calculate the following statistics on those numbers:

The number of numbers in the file.

The sum of the numbers in the file.

The average of the numbers in the file.

The largest number in the file.

The smallest number in the file.

And, for 5 extra bonus points:

The second largest number in the file.

The second smallest number in the file.

Your program should then print out a report, similar to the following:

Number of numbers: 235 Sum of numbers: 5346 Average of numbers: 22.8 Largest number: 515 Smallest number: 3 Second largest number: 512 Second smallest number: 7

Explanation / Answer

#include<iostream>
#include<fstream>
#include<math.h>
#include<stdlib.h>
#include<limits.h>

using namespace std;

int main(){
ifstream infile;
infile.open("random.txt");
int count = 0, sum = 0;
int largest = INT_MIN;
int smallest = INT_MAX;
int num;

while(!infile.eof()){
count++;
infile >> num;
sum += num;
if(num > largest){
largest = num;
}
if(num < smallest){
smallest = num;
}
}

cout << "Number of numbers: " << count << endl;
cout << "Sum of numbers: " << sum << endl;
cout << "Average of numbers: " << (double)sum/count <<endl;
cout << "Largest number: " <<largest <<endl;
cout << "Smallest number: " << smallest << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote