COSCI539

CS216 Lab1 [5]

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