Advertisement
COSCI539

CS216 Lab1 [3]

Feb 20th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. #define ARR_MAX 50
  8.  
  9. void inputData(ifstream& inFile, int& evens[], int& odds[], int& evenElements, int& oddElements);
  10. void bubbleSort(int arr[], int elements);
  11. int calcSum(int arr[], int elements);
  12. int calcMedian(int arr[], int elements);
  13. void outputArray(ofstream outFile, int arr[], int elements);
  14. void displayHighestValues(int arr[], int elements, int userValue);
  15. void displayLowestValues(int arr[], int elements, int userValue);
  16. void runtimeError(const string errorMessage);
  17.  
  18. int main()
  19. {
  20.     int evens[ARR_MAX], odds[ARR_MAX], evenElements = 0, oddElements = 0, userMenuSelection = 0;
  21.     ifstream inFile;
  22.     ofstream outFile;
  23.    
  24.     inFile.open ("...");
  25.    
  26.     if (!inFile)
  27.     {
  28.         runtimeError("Failed to open input file.");
  29.     }
  30.    
  31.     inputData(inFile, evens[], odds[], evenElements, oddElements);
  32.     bubbleSort(evens[], evenElements);
  33.     bubbleSort(odds[], oddElements);
  34.    
  35.     inFile.close(); //here or at end of main()?
  36.  
  37.     cout << "[1] Display the number of entries and totals of each list.\n"
  38.          << "[2] Display the averages of each list.\n"
  39.          << "[3] Display the medians of each list.\n"
  40.          << "[4] Output sorted lists to file.\n"
  41.          << "[5] Display the highest X values of each list.\n"
  42.          << "[6] Display the lowest X values of each list.\n"
  43.          << "[7] Exit.\n";
  44.    
  45.     do
  46.     {
  47.         int evenSum, oddSum, evenMedian, oddMedian, userMenuSelection;
  48.         bool sumCalculated = 0, medianCalculated = 0;
  49.        
  50.         if (userMenuSelection)
  51.         {
  52.             cout << "Select another option.\n";
  53.         }
  54.         else
  55.         {
  56.             cout << "Select an option by entering its corresponding number.\n";
  57.         }
  58.        
  59.         cin >> userMenuSelection;
  60.    
  61.         switch(userMenuSelection)
  62.         {
  63.             case 1:
  64.                 if (!sumCalculated)
  65.                 {
  66.                     evenSum = calcSum(evens[], evenElements);
  67.                     oddSum = calcSum(odds[], oddElements)
  68.                     sumCalculated = 1;
  69.                 }
  70.                
  71.                 cout << "# of Even entries: " << evenElements << endl
  72.                      << "# of Odd Entries: " << oddElements << endl
  73.                      << "Sum of Even List: " << evenSum << endl
  74.                      << "Sum of Odd List: " << oddSum;
  75.                 break;
  76.             case 2:
  77.                 cout << "Average of Even List: " << (float) evenSum / evenElements << endl
  78.                      << "Average of Odd List: " << (float) oddSum / oddElements;
  79.                 break;
  80.             case 3:
  81.                 if (!medianCalculated)
  82.                 {
  83.                     evenMedian = calcMedian(evens[], evenElements);
  84.                     oddMedian = calcMedian(odds[], oddElements)
  85.                     medianCalculated = 1;
  86.                 }
  87.            
  88.                 cout << "Median of Even List: " << evenMedian << endl
  89.                      << "Median of Odd List: " << oddMedian;
  90.                 break;
  91.             case 4:
  92.                 outFile.open ("...");
  93.                
  94.                 outFile << "Even List:\n";
  95.                 outputArray(outFile, evens[], evenElements);
  96.                 outFile << "Odd List:\n";
  97.                 outputArray(outFile, odds[], oddElements);
  98.                
  99.                 outFile.close();
  100.                 break;
  101.             case 5:
  102.                 cout << "Enter a value for X: ";
  103.                 cin >> userValue;
  104.            
  105.                 cout << "Highest " << userValue << " Even Values: "
  106.                 displayHighestValues(evens[], evenElements, userValue);
  107.                 cout << "Highest " << userValue << " Odd Values: "
  108.                 displayHighestValues(odds[], oddElements, userValue);
  109.                 break;
  110.             case 6:
  111.                 cout << "Enter a value for X: ";
  112.                 cin >> userValue;
  113.            
  114.                 cout << "Lowest " << userValue << " Even Values: "
  115.                 displayLowestValues(evens[], evenElements, userValue);
  116.                 cout << "Lowest " << userValue << " Odd Values: "
  117.                 displayLowestValues(odds[], oddElements, userValue);
  118.                 break;
  119.             case 7:
  120.                 cout << "Exiting program.\n";
  121.                 break;
  122.             default:
  123.                 cout << "Invalid menu option selected.\n";
  124.                 break;
  125.         }
  126.     } while (userChoice != 7);
  127.  
  128.     return 0;
  129. }
  130.  
  131. void inputData(fstream& inFile, int& evens[], int& odds[], int& evenElements, int& oddElements)
  132. {
  133.     int value;
  134.    
  135.     for (int i = 0; evenElements < ARR_MAX && oddElements < ARR_MAX && !feof(inFile); ++i)
  136.     {
  137.         inFile >> value;
  138.        
  139.         if (value % 2 == 0)
  140.         {
  141.             evens[evenElements++] = value;
  142.         }
  143.         else
  144.         {
  145.             odds[oddsElements++] = value;
  146.         }
  147.     }
  148.    
  149.     if (evenElements == 0 && oddElements == 0)
  150.     {
  151.         runtimeError("Input file is empty.");
  152.     }
  153.     if (!feof(inFile))
  154.     {
  155.         runtimeError("Excess entries detected.");
  156.     }
  157. }
  158.  
  159. void bubbleSort(int arr[], int elements)
  160. {
  161.     int elmnt1, elmnt2;
  162.    
  163.     for (elmnt1 = 0; elmnt1 < elements - 1; ++elmnt1)      
  164.     {
  165.         for (elmnt2 = 0; elmnt2 < elements - elmnt1 - 1; ++elmnt2)
  166.         {
  167.             if (arr[elmnt2] > arr[elmnt2 + 1])
  168.             {
  169.                 int temp = arr[elmnt2];
  170.                 arr[elmnt2] = arr[elmnt2 + 1];
  171.                 arr[elmnt2 + 1] = temp;
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177. int calcSum(int arr[], int elements)
  178. {
  179.     int sum = 0;
  180.    
  181.     for (int i = 0; i < elements; ++i)
  182.     {
  183.         sum += arr[i];
  184.     }
  185.    
  186.     return sum;
  187. }
  188.  
  189. int calcMedian(int arr[], int elements)
  190. {
  191.     if (elements % 2 == 0)
  192.     {
  193.         return (float )(arr[elements / 2] + arr[(elements / 2) + 1]) / 2;
  194.     }
  195.     else
  196.     {
  197.         return arr[(elements / 2) + 1];
  198.     }
  199. }
  200.  
  201. void outputArray(ofstream outFile, int arr[], int elements)
  202. {
  203.     for (int i = 0; i < elements; ++i)
  204.     {
  205.         outFile << arr[i] << ' ';
  206.     }
  207. }
  208.  
  209. void displayHighestValues(int arr[], int elements, int userValue);
  210. {
  211.     for (int i = 0; i < userValue; ++i)
  212.     {
  213.         cout << arr[elements - 1 - i] << ' ';
  214.     }
  215. }
  216.  
  217. void displayLowestValues(int arr[], int elements, int userValue)
  218. {
  219.     for (int i = 0; i < userValue; ++i)
  220.     {
  221.         cout << arr[i] << ' ';
  222.     }
  223. }
  224.  
  225. void runtimeError(const string errorMessage)
  226. {
  227.     cout << "Error: " << errorMessage << " Exiting program.\n";
  228.     exit(1);
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement