Advertisement
Porr011

Lesson 10 Activity 1

Feb 25th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <iomanip>
  7. #include <cmath>
  8. using namespace std;
  9.  
  10. string getFileInfo();
  11. double calculateMean (string filename);
  12. int calculatorMin(string filename);
  13.  
  14. //***********************************
  15. // Int main                         *
  16. // Where we tie in all the funtions *
  17. //***********************************
  18. int main ()
  19. {
  20. string filename = getFileInfo(); // getting file name
  21. double mean = calculateMean( filename); // avg
  22. int min = calculatorMin(filename); // min
  23. return 0;
  24. }
  25.  
  26. //***************************************************
  27. //Function getFileInfo                              *
  28. //Function for getting the text file name from user *
  29. //***************************************************
  30. string getFileInfo() {
  31.     string filename;
  32.     ifstream inputFile;
  33.     // asking user for name
  34.     cout << "Please enter a file name to read from: ";
  35.     cin >> filename;
  36.    
  37.    inputFile.open(filename);
  38.    
  39.    if (inputFile)
  40.    {
  41.        inputFile.close();
  42.    }
  43.    // if it doesnt open wrong ask for it again
  44.    else
  45.    {
  46.     cout << "Error: Please enter a file name to read from: ";
  47.     cin >> filename;
  48.    }
  49.     //if it opens then name is good and close file then return name
  50.     //if it didn't open, ask for a new name.
  51.     return filename;
  52. }
  53.  
  54. //************************************************************
  55. //Function calculateMean                                     *
  56. //funtion for the calculating of the mean of all the numbers *
  57. //************************************************************
  58. double calculateMean(string filename)
  59. {
  60.     ifstream infile;
  61.     float num=0;
  62.     float total = 0.0;
  63.     int count = 0;
  64.     //varibles for finding median
  65.     infile.open(filename);
  66.     // While infile successfully extracted numbers from the stream
  67.     while(infile >> num) {
  68.         total += num;
  69.         ++count;
  70.     }
  71.     // don't need the file anymore, close it
  72.     infile.close();
  73.     //output mean
  74.        cout << "The mean was " << total/count << endl;
  75.     // give the average
  76.    return total/count;
  77.  
  78. }
  79. //******************************************
  80. // Function calculatorMin                  *
  81. //funtion for calculating the minumum value*
  82. //******************************************
  83. int calculatorMin(string filename)
  84. {
  85.    
  86.     ifstream infile;
  87.     double min = 32767; // since all the numbers are positive
  88.     float num;
  89.     // varibles
  90.     infile.open(filename);
  91.    
  92.     while (infile >> num)
  93.     {
  94.         {
  95.         if (num < min)
  96.             min = num;
  97.         } // algorithm for median
  98.     }
  99.    
  100.     infile.close();
  101.    
  102.     cout << "The minumum number is " << min << endl;
  103.     // out put minumum number
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement