The attached file contains a long list of random numbers Ran

The attached file contains a long list of random numbers (Random.txt). Copy the file to your hard drive. Then write a program that asks user to enter the file name to open and validates that the file is successfully open. Once the user opens the file (Random.txt) successfully, your program should calculate the following:(The output should be in the following format) The number of numbers m the file: The even numbers in the file: The odd numbers in the file: The sum of all the numbers in the file: The average of all the numbers in the file:

Solution

//C++ code to read file and produce the desired output

#include <iostream>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <vector>
using namespace std;


int main()
{  
   vector<int> odd;
   vector<int> even;
   int number;
   double size;
   double sum=0;
   double average=0;
   ifstream inputFile;
   string name;

   while(true)
   {
       cout << \"Enter input file name: \";
       cin >> name;

       inputFile.open(name.c_str());
       if(inputFile.is_open())
           break;
       else
       cout << \"Error opening file\ \";
   }
  
while(inputFile>> number)
{
sum=sum+number;
size++;

if(number%2 == 0)
   even.push_back(number);
else
   odd.push_back(number);
}

// clase file
inputFile.close();
  
average=sum/size;
  
cout << \"Number of numbers in file: \" << size << endl;
cout << \"The Odd numbers in file: \";
for (int i = 0; i < odd.size(); ++i)
{
   cout << odd[i] << \" \";
}
cout << endl;
cout << \"The Even numbers in file: \";
for (int i = 0; i < even.size(); ++i)
{
   cout << even[i] << \" \";
}
cout << endl;
cout << \"The sum of all the numbers in file: \" << sum << endl;
cout << \"The average of all numbers in file: \" << average << endl;
return 0;
}

/*

random.txt
34
55
45
23
23
78
66
56
55
45

output:
Enter input file name: random.txt
Number of numbers in file: 10
The Odd numbers in file: 55 45 23 23 55 45
The Even numbers in file: 34 78 66 56
The sum of all the numbers in file: 480
The average of all numbers in file: 48


*/

 The attached file contains a long list of random numbers (Random.txt). Copy the file to your hard drive. Then write a program that asks user to enter the file
 The attached file contains a long list of random numbers (Random.txt). Copy the file to your hard drive. Then write a program that asks user to enter the file

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site