Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. /*Andrew Gonzalez
  2.  * Lab 4
  3.  * This program computes the average of data sets and to total average for all the inputs.*/
  4.  
  5.  #include <iostream>
  6.  using namespace std;
  7.  
  8. int main()
  9. {
  10.     /*Variable declarations*/
  11.     int flag = 1;
  12.     int size, repititions;
  13.     double highestAvg = 0;
  14.     double lowestAvg = 0;
  15.     double number, runningTotal, average, finalAverage;
  16.    
  17.     /*while true loop keeps running thorugh the process as long as there are more averages to compute*/
  18.     while(true){
  19.        
  20.         /*do while loop to take in user input for size of each data set*/
  21.         do{
  22.             cout << "Please enter the size of the data set (1-10 elements or 0 to quit): ";
  23.             cin >> size;
  24.             if(size < 0 || size > 10)
  25.                 cout << endl;
  26.         }while(size < 0 || size > 10);
  27.        
  28.         /*If statement to break out of the while true when the condition is met*/
  29.         if(size == 0) break;
  30.        
  31.         /*cin in the data for the current set*/
  32.         cout << "Please enter your data: ";
  33.         for(int i = 0; i < size; i++){
  34.             cin >> number;
  35.             runningTotal += number;
  36.         }
  37.        
  38.         /*compute the average for current data set*/
  39.         average = runningTotal / size;
  40.        
  41.         /*if the average is a higher or lower than the current max or min average, replace with the new extreme
  42.          * if it is the first time iterating through the program, the flag will place the current value into the the low and high*/
  43.         if(flag == 1) {
  44.             lowestAvg = average;
  45.             highestAvg = average;
  46.             flag = 2;
  47.         }
  48.         if(average < lowestAvg)
  49.             lowestAvg = average;
  50.         if(average > highestAvg)
  51.             highestAvg = average;
  52.        
  53.         /*adds the average into the total in preparation for the final
  54.          * also adds one to the number of times the while loop has occurred*/
  55.         finalAverage += average;
  56.         repititions++;
  57.        
  58.         /*Setting precision to 3 decimal places*/
  59.         cout.setf(ios::fixed);
  60.         cout.precision(3);
  61.    
  62.         /*output the average for that set of current data.
  63.          * reset the average and the runningTotal variables for the next iteration*/
  64.         cout << "\naverage = " << average << "\n\n";
  65.         average = 0;
  66.         runningTotal = 0;
  67.     }
  68.    
  69.     /*compute the final average without the highest and lowest averages*/
  70.     finalAverage = finalAverage - highestAvg - lowestAvg;
  71.     repititions = repititions - 2;
  72.     finalAverage = finalAverage / repititions;
  73.    
  74.     /*output the final average*/
  75.     cout << "\nfinal average = " << finalAverage;
  76.    
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement