Advertisement
COSCI539

CS216 Lab1 [7]

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