Advertisement
COSCI539

CS216 Lab1 [8]

Feb 27th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.63 KB | None | 0 0
  1. //Lab 1 Wilson, Drayden T Th
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. const int ARR_MAX = 15;
  11.  
  12. enum MenuOption { INVALID = 0, TOTALS, AVERAGES, MEDIANS, TO_FILE, HIGHEST, LOWEST, EXIT };
  13.  
  14. void PrintTotals(int evens[], int odds[], int evenCount, int oddCount);
  15. void PrintAverages(int evens[], int odds[], int evenCount, int oddCount);
  16. void PrintMedians(int evens[], int odds[], int evenCount, int oddCount);
  17. void PrintToFile(int evens[], int odds[], int evenCount, int oddCount);
  18. void PrintHighestValues(int evens[], int odds[], int evenCount, int oddCount);
  19. void PrintLowestValues(int evens[], int odds[], int evenCount, int oddCount);
  20. void FillArrays(int evens[], int odds[], int& evenCount, int& oddCount);
  21. void BubbleSort(int arr[], int arrSize);
  22. int CalcArraySum(int arr[], int arrSize);
  23. float CalcArrayMedian(int arr[], int arrSize);
  24. void OutputArray(ofstream& outFile, int arr[], int arrSize);
  25. void DisplayHighestLowestValues(int arr[], int arrSize, int startIndex, int endIndex);
  26. void RuntimeError(const string errorMessage);
  27.  
  28. int main()
  29. {
  30.     int evens[ARR_MAX], odds[ARR_MAX], evenCount = 0, oddCount = 0, userMenuSelection = 0;
  31.  
  32.     cout << setprecision(2) << fixed;
  33.  
  34.     FillArrays(evens, odds, evenCount, oddCount);
  35.     BubbleSort(evens, evenCount);
  36.     BubbleSort(odds, oddCount);
  37.  
  38.     do
  39.     {
  40.         cout << "\n\n[1] Display the number of entries and totals of each list.\n"
  41.             << "[2] Display the averages of each list.\n"
  42.             << "[3] Display the medians of each list.\n"
  43.             << "[4] Output sorted lists to file.\n"
  44.             << "[5] Display the highest X values of each list.\n"
  45.             << "[6] Display the lowest X values of each list.\n"
  46.             << "[7] Exit.";
  47.  
  48.         if (userMenuSelection)
  49.         {
  50.             cout << "\n\nSelect another option: ";
  51.         }
  52.         else
  53.         {
  54.             cout << "\n\nSelect an option by entering its corresponding number: ";
  55.         }
  56.  
  57.         cin >> userMenuSelection;
  58.  
  59.         switch (userMenuSelection)
  60.         {
  61.         case TOTALS:
  62.                     PrintTotals(evens, odds, evenCount, oddCount);
  63.                     break;
  64.         case AVERAGES:
  65.                     PrintAverages(evens, odds, evenCount, oddCount);
  66.                     break;
  67.         case MEDIANS:
  68.                     PrintMedians(evens, odds, evenCount, oddCount);
  69.                     break;
  70.         case TO_FILE:
  71.                     PrintToFile(evens, odds, evenCount, oddCount);
  72.                     break;
  73.         case HIGHEST:
  74.                     PrintHighestValues(evens, odds, evenCount, oddCount);
  75.                     break;
  76.         case LOWEST:
  77.                     PrintLowestValues(evens, odds, evenCount, oddCount);
  78.                     break;
  79.         case EXIT:
  80.                     cout << "Exiting the program.\n\n";
  81.                     break;
  82.         default:
  83.                     cout << "Invalid menu option selected.\n";
  84.                     userMenuSelection = 1; //avoids repetion of initial instructions
  85.                     break;
  86.         }
  87.     } while (userMenuSelection != 7);
  88.  
  89.     system("pause");
  90.     return 0;
  91. }
  92.  
  93. //Menu Option 1 [TOTALS]
  94. void PrintTotals(int evens[], int odds[], int evenCount, int oddCount)
  95. {
  96.     cout << "# of Even Entires: " << evenCount
  97.         << "\nSum of Even Entries: " << CalcArraySum(evens, evenCount)
  98.         << "\n# of Odd Entires: " << oddCount
  99.         << "\nSum of Odd Entries: " << CalcArraySum(odds, oddCount);
  100. }
  101.  
  102. //Menu Option 2 [AVERAGES]
  103. void PrintAverages(int evens[], int odds[], int evenCount, int oddCount)
  104. {
  105.     cout << "Average of Even Entries: ";
  106.     if (evenCount)
  107.     {
  108.         cout << (float)CalcArraySum(evens, evenCount) / evenCount;
  109.     }
  110.     else
  111.     {
  112.         cout << "N/A\n";
  113.     }
  114.  
  115.     cout << "\nAverage of Odd Entries: ";
  116.     if (oddCount)
  117.     {
  118.         cout << (float)CalcArraySum(odds, oddCount) / oddCount;
  119.     }
  120.     else
  121.     {
  122.         cout << "N/A\n";
  123.     }
  124. }
  125.  
  126. //Menu Option 3 [MEDIANS]
  127. void PrintMedians(int evens[], int odds[], int evenCount, int oddCount)
  128. {
  129.     cout << "Median of Even Entries: ";
  130.     if (evenCount)
  131.     {
  132.         cout << CalcArrayMedian(evens, evenCount);
  133.     }
  134.     else
  135.     {
  136.         cout << "N/A\n";
  137.     }
  138.  
  139.     cout << "\nMedian of odd Entries: ";
  140.     if (oddCount)
  141.     {
  142.         cout << CalcArrayMedian(odds, oddCount);
  143.     }
  144.     else
  145.     {
  146.         cout << "N/A\n";
  147.     }
  148. }
  149.  
  150. //Menu Option 4 [TO_FILE]
  151. void PrintToFile(int evens[], int odds[], int evenCount, int oddCount)
  152. {
  153.     ofstream outFile;
  154.  
  155.     outFile.open("output.txt");
  156.  
  157.     outFile << "Even Array: ";
  158.     if (evenCount)
  159.     {
  160.         OutputArray(outFile, evens, evenCount);
  161.     }
  162.     else
  163.     {
  164.         outFile << "N/A\n";
  165.     }
  166.  
  167.     outFile << "\nOdd Array: ";
  168.     if (oddCount)
  169.     {
  170.         OutputArray(outFile, odds, oddCount);
  171.     }
  172.     else
  173.     {
  174.         outFile << "N/A\n";
  175.     }
  176.  
  177.     outFile.close();
  178.  
  179.     cout << "Print to file successful.\n";
  180. }
  181.  
  182. //Menu Option 5 [HIGHEST]
  183. void PrintHighestValues(int evens[], int odds[], int evenCount, int oddCount)
  184. {
  185.     int userValue;
  186.  
  187.     cout << "Enter a value for X: ";
  188.     cin >> userValue;
  189.  
  190.     if (userValue > evenCount)
  191.     {
  192.         printf("Value exceeds even list size. Can only print %d even values.\n", evenCount);
  193.     }
  194.  
  195.     if (userValue > oddCount)
  196.     {
  197.         printf("Value exceeds odd list size. Can only print %d odd values.\n", oddCount);
  198.     }
  199.  
  200.     cout << "Highest " << userValue << " Even Entries: ";
  201.     if (evenCount)
  202.     {
  203.         DisplayHighestLowestValues(evens, evenCount, ((userValue < evenCount) ? (evenCount - userValue) : 0), evenCount);
  204.     }
  205.     else
  206.     {
  207.         cout << "N/A\n";
  208.     }
  209.    
  210.     cout << "Highest " << userValue << " Odd Entries: ";
  211.     if (evenCount)
  212.     {
  213.         DisplayHighestLowestValues(odds, oddCount, ((userValue < oddCount) ? (oddCount - userValue) : 0), oddCount);
  214.     }
  215.     else
  216.     {
  217.         cout << "N/A\n";
  218.     }
  219. }
  220.  
  221. //Menu Option 6 [LOWEST]
  222. void PrintLowestValues(int evens[], int odds[], int evenCount, int oddCount)
  223. {
  224.     int userValue;
  225.  
  226.     cout << "Enter a value for X: ";
  227.     cin >> userValue;
  228.  
  229.     cout << "Lowest " << userValue << " Even Entries: ";
  230.     if (evenCount)
  231.     {
  232.         DisplayHighestLowestValues(evens, evenCount, 0, ((userValue < evenCount) ? userValue : evenCount));
  233.     }
  234.     else
  235.     {
  236.         cout << "N/A\n";
  237.     }
  238.     cout << "Lowest " << userValue << " Odd Entries: ";
  239.     if (oddCount)
  240.     {
  241.         DisplayHighestLowestValues(odds, oddCount, 0, ((userValue < oddCount) ? userValue : oddCount));
  242.     }
  243.     else
  244.     {
  245.         cout << "N/A\n";
  246.     }
  247. }
  248.  
  249. //Assigns both arrays and array size variables
  250. //pre: input file is open
  251. //post: both arrays are populated and size variables are defined
  252. void FillArrays(int evens[], int odds[], int& evenCount, int& oddCount)
  253. {
  254.     ifstream inFile;
  255.     int value;
  256.  
  257.     inFile.open("input.txt");
  258.  
  259.     if (!inFile)
  260.     {
  261.         RuntimeError("Failed to open input file.");
  262.     }
  263.     else
  264.     {
  265.         cout << "Input file read successfully.";
  266.     }
  267.  
  268.     for (int i = 0; inFile >> value && evenCount < ARR_MAX && oddCount < ARR_MAX; ++i)
  269.     {
  270.         if (value % 2 == 0)
  271.         {
  272.             evens[evenCount++] = value;
  273.         }
  274.         else
  275.         {
  276.             odds[oddCount++] = value;
  277.         }
  278.     }
  279.  
  280.     inFile.close();
  281.  
  282.     if (evenCount == 0 && oddCount == 0)
  283.     {
  284.         RuntimeError("Input file is empty.");
  285.     }
  286.     else if (!inFile.eof())
  287.     {
  288.         RuntimeError("Excess entries detected. A maximum of " + to_string(ARR_MAX) + " even or odd entries can be accepted");
  289.     }
  290. }
  291.  
  292. //Sorts array
  293. //pre: array is populated and array size is defined
  294. //post: array is sorted in ascneding order
  295. void BubbleSort(int arr[], int arrSize)
  296. {
  297.     int i, j;
  298.  
  299.     for (i = 0; i < arrSize - 1; ++i)
  300.     {
  301.         for (j = 0; j < arrSize - i - 1; ++j)
  302.         {
  303.             if (arr[j] > arr[j + 1])
  304.             {
  305.                 int temp = arr[j];
  306.                 arr[j] = arr[j + 1];
  307.                 arr[j + 1] = temp;
  308.             }
  309.         }
  310.     }
  311. }
  312.  
  313. //Calculates sum of all elements in array
  314. //pre: array is populated and array size is defined
  315. //post: returns sum of all array elements
  316. int CalcArraySum(int arr[], int arrSize)
  317. {
  318.     int sum = 0;
  319.  
  320.     for (int i = 0; i < arrSize; ++i)
  321.     {
  322.         sum += arr[i];
  323.     }
  324.     return sum;
  325. }
  326.  
  327. //Calculates median value of array elements
  328. //pre: array is populated and array size is defined
  329. //post: returns median of array elements
  330. float CalcArrayMedian(int arr[], int arrSize)
  331. {
  332.     if (arrSize % 2 == 0)
  333.     {
  334.         return (arr[(arrSize / 2) - 1] + arr[arrSize / 2]) / 2.0;
  335.     }
  336.     else
  337.     {
  338.         return arr[arrSize / 2];
  339.     }
  340. }
  341.  
  342. //Prints array to output file
  343. //pre: output file is open, array is populated and array size is defined
  344. //post: array is printed to file
  345. void OutputArray(ofstream& outFile, int arr[], int arrSize)
  346. {
  347.     int i;
  348.  
  349.     for (i = 0; i < arrSize; ++i)
  350.     {
  351.         outFile << arr[i] << ' ';
  352.     }
  353. }
  354.  
  355. //Prints highest or lowest X values of array
  356. //pre: array is populated, array size is defined; highest: start = size - X, end = size; lowest: start = 0, end = X
  357. //post: values are printed, prints "N/A" if array is empty
  358. void DisplayHighestLowestValues(int arr[], int arrSize, int startIndex, int endIndex)
  359. {
  360.     for (int current = startIndex; current < endIndex; ++current)
  361.     {
  362.         cout << arr[current] << ' ';
  363.     }
  364.  
  365.     cout << endl;
  366. }
  367.  
  368. //Outputs a unique error message and ends the program
  369. //pre: N/A
  370. //post: error message has been output and program has ended
  371. void RuntimeError(const string errorMessage)
  372. {
  373.     cout << "Error: " << errorMessage << " Exiting program.\n\n";
  374.     system("pause");
  375.     exit(1);
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement