Advertisement
Taco2k

Untitled

Oct 17th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int numValue = 0;
  9.     double temp = 0;
  10.     double sum = 0;
  11.     double average = 0;
  12.  
  13.     cout << fixed << setprecision(2);
  14.  
  15.     ifstream infile;
  16.     infile.open("input.txt");
  17.  
  18.     if (infile) {
  19.         infile >> numValue;
  20.  
  21.         if (numValue != 0) {
  22.  
  23.             for (int i = 0; i < numValue; i++) {
  24.                 infile >> temp;
  25.                 sum += temp;
  26.             }
  27.  
  28.             average = (sum / temp);
  29.             cout << "The average of these numbers are " << average << endl;
  30.         }
  31.         else {
  32.             cout << "Error! Cannot compute the average if there are no values." << endl;
  33.         }
  34.     }
  35.     else {
  36.         cout << "Error! Unable to open the file." << endl;
  37.     }
  38.  
  39.     infile.close();
  40.     cin.ignore();
  41.     cin.get();
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement