Answer to Question #304196 in C++ for Harry

Question #304196

This program will read integers from a file and find results from these integers.

Open the file, test to make sure the file opened. Use a loop to read the integers and process each integer as it is read. When End Of File is reached, close the file.

After all of the integers have been read and processed, print the results for the following output:

  • The integer count:
  • The sum of the integers:
  • The smallest integer:
  • The largest integer:
  • The average of the integers:

Use notepad, vim, or other simple text editor to create a file named file1.txt containing the following, with one integer per line.

Test the program two times.

Use the following data in the first test:

11
9
18
22
27
33
21
  

For the second test add an additional line containing the number 40.



1
Expert's answer
2022-03-02T01:16:31-0500
#include <iostream>

using namespace std;

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int count = 0;
  int sum = 0;
  int mn = INT_MIN;
  int mx = INT_MAX;
  while (cin >> x) {
    count++;
    sum += x;
    mn = min(mn, x);
    mx = max(mx, x);
  }
  cout << "Integer count: " << count << endl;
  cout << "Sum: " << sum << endl;
  cout << "Min: " << mn << endl;
  cout << "Max: " << mx << endl;
  cout << "Avarage: " << ((double) sum) / count << endl;
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog