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

The attached file contains a long list of random numbers (Random.txt). 1. Copy t

ID: 3606533 • Letter: T

Question

The attached file contains a long list of random numbers (Random.txt).

1. Copy the file to your hard drive.

2. Then write a program using C++ that asks the user to enter the file name to open and validates that the file is successfully open.

3. Once the user opens the file (Random.txt) successfully, your program should calculate the following:(The output should be in the following format)

A) The number of numbers in the file:

B) The even numbers in the file:

C) The odd numbers in the file:

D) The sum of all the numbers in the file:

E) The average of all the numbers in the file:

Explanation / Answer

Code:

#include<iostream>
#include<fstream>
using namespace std;
int main() {
int n=0, even=0, odd=0, sum=0,number;
float avg;
ifstream myReadFile;
string filename;
cout << "Enter the filename : ";
cin >> filename;
myReadFile.open(filename);
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
number = atoi(output);
if(number % 2 == 0){
even++;
}else{
odd++;
}
sum += number;
n++;
}
}
avg = sum/n;
cout << "The number of numbers in the file:" << n << endl;
cout << "The even numbers in the file: " << even << endl;
cout << "The odd numbers in the file: " << odd << endl;
cout << "The sum of all the numbers in the file: " << sum << endl;
cout << "The average of all the numbers in the file: " << avg << endl;
myReadFile.close();
return 0;
}

Output:

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