Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab 1 Wilson, Drayden T Th
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- using namespace std;
- const int ARR_MAX = 15;
- enum MenuOption { Invalid = 0, Totals, Averages, Medians, ToFile, Highest, Lowest, Exit };
- void PrintTotals(int evenCount, int oddCount, int evenSum, int oddSum);
- void PrintAverages(int evenCount, int oddCount, int evenSum, int oddSum);
- void PrintMedians(int evens[], int odds[], int evenCount, int oddCount, float evenMedian, float oddMedian);
- void PrintToFile(int evens[], int odds[], int evenCount, int oddCount);
- void PrintHighestValues(int evens[], int odds[], int evenCount, int oddCount);
- void PrintLowestValues(int evens[], int odds[], int evenCount, int oddCount);
- void FillArrays(int evens[], int odds[], int& evenCount, int& oddCount);
- void BubbleSort(int arr[], int arrSize);
- int CalcArraySum(int arr[], int arrSize);
- float CalcArrayMedian(int arr[], int arrSize);
- void OutputArray(ofstream& outFile, int arr[], int arrSize);
- void DisplaySomeValues(int arr[], int arrSize, int startIndex, int endIndex);
- void RuntimeError(const string errorMessage);
- int main()
- {
- int evens[ARR_MAX], odds[ARR_MAX], evenCount = 0, oddCount = 0, userMenuSelection = 0;
- FillArrays(evens, odds, evenCount, oddCount);
- BubbleSort(evens, evenCount);
- BubbleSort(odds, oddCount);
- cout << setprecision(2) << fixed;
- int evenSum = CalcArraySum(evens, evenCount), oddSum = CalcArraySum(odds, oddCount);
- //summate here or in file with check?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- float evenMedian = 0, oddMedian = 0; //calc median here or in file?~~~~~~~~~~~~~~~~~~~~~
- do
- {
- cout << "\n\n[1] Display the number of entries and totals of each list.\n"
- << "[2] Display the averages of each list.\n"
- << "[3] Display the medians of each list.\n"
- << "[4] Output sorted lists to file.\n"
- << "[5] Display the highest X values of each list.\n"
- << "[6] Display the lowest X values of each list.\n"
- << "[7] Exit.";
- if (userMenuSelection)
- {
- cout << "\n\nSelect another option: ";
- }
- else
- {
- cout << "\n\nSelect an option by entering its corresponding number: ";
- }
- cin >> userMenuSelection;
- switch (userMenuSelection)
- {
- case Totals:
- PrintTotals(evenCount, oddCount, evenSum, oddSum);
- break;
- case Averages:
- PrintAverages(evenCount, oddCount, evenSum, oddSum);
- break;
- case Medians:
- PrintMedians(evens, odds, evenCount, oddCount, evenMedian, oddMedian);
- break;
- case ToFile:
- PrintToFile(evens, odds, evenCount, oddCount);
- break;
- case Highest:
- PrintHighestValues(evens, odds, evenCount, oddCount);
- break;
- case Lowest:
- PrintLowestValues(evens, odds, evenCount, oddCount);
- break;
- case Exit:
- cout << "Exiting the program.\n\n";
- break;
- default:
- cout << "Invalid menu option selected.\n";
- userMenuSelection = 1; //avoids repetion of initial instructions if 0
- break;
- }
- } while (userMenuSelection != 7);
- system("pause");
- return 0;
- }
- //Menu Option 1 [Totals]
- void PrintTotals(int evenCount, int oddCount, int evenSum, int oddSum)
- {
- cout << "# of Even Entires: " << evenCount
- << "\nSum of Even Entries: " << evenSum
- << "\n# of Odd Entires: " << oddCount
- << "\nSum of Odd Entries: " << oddSum;
- }
- //Menu Option 2 [Averages]
- void PrintAverages(int evenCount, int oddCount, int evenSum, int oddSum)
- {
- cout << "Average of Even Entries: ";
- if (evenCount)
- {
- cout << (float)evenSum / evenCount;
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "\nAverage of Odd Entries: ";
- if (oddCount)
- {
- cout << (float)oddSum / oddCount;
- }
- else
- {
- cout << "N/A\n";
- }
- }
- //Menu Option 3 [Medians]
- void PrintMedians(int evens[], int odds[], int evenCount, int oddCount, float evenMedian, float oddMedian)
- {
- cout << "Median of Even Entries: ";
- if (evenCount)
- { evenMedian = CalcArrayMedian(evens, evenCount);
- cout << evenMedian;
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "\nMedian of odd Entries: ";
- if (oddCount)
- { oddMedian = CalcArrayMedian(odds, oddCount);
- cout << oddMedian;
- }
- else
- {
- cout << "N/A\n";
- }
- }
- //Menu Option 4 [ToFile]
- void PrintToFile(int evens[], int odds[], int evenCount, int oddCount)
- {
- ofstream outFile;
- outFile.open("output.txt");
- outFile << "Even Array: ";
- OutputArray(outFile, evens, evenCount);
- outFile << "\nOdd Array: ";
- OutputArray(outFile, odds, oddCount);
- outFile.close();
- cout << "Print to file successful.\n";
- }
- //Menu Option 5 [Highest]
- void PrintHighestValues(int evens[], int odds[], int evenCount, int oddCount)
- {
- int userValue;
- cout << "Enter a value for X: ";
- cin >> userValue;
- if (userValue > evenCount)
- {
- printf("Value exceeds even list size. Can only print %d even values.\n", evenCount);
- }
- if (userValue > oddCount)
- {
- printf("Value exceeds odd list size. Can only print %d odd values.\n", oddCount);
- }
- cout << "Highest " << userValue << " Even Entries: ";
- DisplaySomeValues(evens, evenCount, ((userValue < evenCount) ? (evenCount - userValue) : 0), evenCount);
- cout << "Highest " << userValue << " Odd Entries: ";
- DisplaySomeValues(odds, oddCount, ((userValue < oddCount) ? (oddCount - userValue) : 0), oddCount);
- }
- //Menu Option 6 [Lowest]
- void PrintLowestValues(int evens[], int odds[], int evenCount, int oddCount)
- {
- int userValue;
- cout << "Enter a value for X: ";
- cin >> userValue;
- cout << "Lowest " << userValue << " Even Entries: ";
- DisplaySomeValues(evens, evenCount, 0, ((userValue < evenCount) ? userValue : evenCount));
- cout << "Lowest " << userValue << " Odd Entries: ";
- DisplaySomeValues(odds, oddCount, 0, ((userValue < oddCount) ? userValue : oddCount));
- }
- //Assigns both arrays and array size variables
- //pre: input file is open
- //post: both arrays are populated and size variables are defined
- void FillArrays(int evens[], int odds[], int& evenCount, int& oddCount)
- {
- ifstream inFile;
- int value;
- //inFile.open("216_l1_in.txt");
- inFile.open("input.txt");
- if (!inFile)
- {
- RuntimeError("Failed to open input file.");
- }
- else
- {
- cout << "Input file read successfully.";
- }
- for (int i = 0; inFile >> value && evenCount < ARR_MAX && oddCount < ARR_MAX; ++i)
- {
- if (value % 2 == 0)
- {
- evens[evenCount++] = value;
- }
- else
- {
- odds[oddCount++] = value;
- }
- }
- inFile.close();
- if (evenCount == 0 && oddCount == 0)
- {
- RuntimeError("Input file is empty."); //no even or odd entries are read
- }
- else if (!inFile.eof())
- {
- RuntimeError("Excess entries detected. A maximum of " + to_string(ARR_MAX) + " even or odd entries can be accepted");
- }
- }
- //Sorts array
- //pre: array is populated and array size is defined
- //post: array is sorted in ascneding order
- void BubbleSort(int arr[], int arrSize)
- {
- int i, j;
- for (i = 0; i < arrSize - 1; ++i)
- {
- for (j = 0; j < arrSize - i - 1; ++j)
- {
- if (arr[j] > arr[j + 1])
- {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- //Calculates sum of all elements in array
- //pre: array is populated and array size is defined
- //post: returns sum of all array elements
- int CalcArraySum(int arr[], int arrSize)
- {
- int sum = 0;
- for (int i = 0; i < arrSize; ++i)
- {
- sum += arr[i];
- }
- return sum;
- }
- //Calculates median value of array elements
- //pre: array is populated and array size is defined
- //post: returns median of array elements
- float CalcArrayMedian(int arr[], int arrSize)
- {
- if (arrSize % 2 == 0)
- {
- return (arr[(arrSize / 2) - 1] + arr[arrSize / 2]) / 2.0;
- }
- else
- {
- return arr[arrSize / 2];
- }
- }
- //Prints array to output file
- //pre: output file is open, array is populated and array size is defined
- //post: array is printed to file
- void OutputArray(ofstream& outFile, int arr[], int arrSize)
- {
- int i;
- for (i = 0; i < arrSize; ++i)
- {
- outFile << arr[i] << ' ';
- }
- if (!i) //if array is empty
- {
- outFile << "N/A";
- }
- }
- //Prints highest or lowest X values of array
- //pre: array is populated, array size is defined; highest: start = size - X, end = size; lowest: start = 0, end = X
- //post: values are printed, prints "N/A" if array is empty
- void DisplaySomeValues(int arr[], int arrSize, int startIndex, int endIndex)
- {
- for (int current = startIndex; current < endIndex; ++current)
- {
- cout << arr[current] << ' ';
- }
- if (!arrSize)
- {
- cout << "N/A";
- }
- cout << endl;
- }
- //Outputs a unique error message and ends the program
- //pre: N/A
- //post: error message has been output and program has ended
- void RuntimeError(const string errorMessage)
- {
- cout << "Error: " << errorMessage << " Exiting program.\n\n";
- system("pause");
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement