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, TO_FILE, HIGHEST, LOWEST, EXIT };
- void PrintTotals(int evens[], int odds[], int evenCount, int oddCount);
- void PrintAverages(int evens[], int odds[], int evenCount, int oddCount);
- void PrintMedians(int evens[], int odds[], int evenCount, int oddCount);
- 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 DisplayHighestLowestValues(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;
- cout << setprecision(2) << fixed;
- FillArrays(evens, odds, evenCount, oddCount);
- BubbleSort(evens, evenCount);
- BubbleSort(odds, oddCount);
- 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(evens, odds, evenCount, oddCount);
- break;
- case AVERAGES:
- PrintAverages(evens, odds, evenCount, oddCount);
- break;
- case MEDIANS:
- PrintMedians(evens, odds, evenCount, oddCount);
- break;
- case TO_FILE:
- 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
- break;
- }
- } while (userMenuSelection != 7);
- system("pause");
- return 0;
- }
- //Menu Option 1 [TOTALS]
- void PrintTotals(int evens[], int odds[], int evenCount, int oddCount)
- {
- cout << "# of Even Entires: " << evenCount
- << "\nSum of Even Entries: " << CalcArraySum(evens, evenCount)
- << "\n# of Odd Entires: " << oddCount
- << "\nSum of Odd Entries: " << CalcArraySum(odds, oddCount);
- }
- //Menu Option 2 [AVERAGES]
- void PrintAverages(int evens[], int odds[], int evenCount, int oddCount)
- {
- cout << "Average of Even Entries: ";
- if (evenCount)
- {
- cout << (float)CalcArraySum(evens, evenCount) / evenCount;
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "\nAverage of Odd Entries: ";
- if (oddCount)
- {
- cout << (float)CalcArraySum(odds, oddCount) / oddCount;
- }
- else
- {
- cout << "N/A\n";
- }
- }
- //Menu Option 3 [MEDIANS]
- void PrintMedians(int evens[], int odds[], int evenCount, int oddCount)
- {
- cout << "Median of Even Entries: ";
- if (evenCount)
- {
- cout << CalcArrayMedian(evens, evenCount);
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "\nMedian of odd Entries: ";
- if (oddCount)
- {
- cout << CalcArrayMedian(odds, oddCount);
- }
- else
- {
- cout << "N/A\n";
- }
- }
- //Menu Option 4 [TO_FILE]
- void PrintToFile(int evens[], int odds[], int evenCount, int oddCount)
- {
- ofstream outFile;
- outFile.open("output.txt");
- outFile << "Even Array: ";
- if (evenCount)
- {
- OutputArray(outFile, evens, evenCount);
- }
- else
- {
- outFile << "N/A\n";
- }
- outFile << "\nOdd Array: ";
- if (oddCount)
- {
- OutputArray(outFile, odds, oddCount);
- }
- else
- {
- outFile << "N/A\n";
- }
- 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: ";
- if (evenCount)
- {
- DisplayHighestLowestValues(evens, evenCount, ((userValue < evenCount) ? (evenCount - userValue) : 0), evenCount);
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "Highest " << userValue << " Odd Entries: ";
- if (evenCount)
- {
- DisplayHighestLowestValues(odds, oddCount, ((userValue < oddCount) ? (oddCount - userValue) : 0), oddCount);
- }
- else
- {
- cout << "N/A\n";
- }
- }
- //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: ";
- if (evenCount)
- {
- DisplayHighestLowestValues(evens, evenCount, 0, ((userValue < evenCount) ? userValue : evenCount));
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "Lowest " << userValue << " Odd Entries: ";
- if (oddCount)
- {
- DisplayHighestLowestValues(odds, oddCount, 0, ((userValue < oddCount) ? userValue : oddCount));
- }
- else
- {
- cout << "N/A\n";
- }
- }
- //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("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.");
- }
- 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] << ' ';
- }
- }
- //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 DisplayHighestLowestValues(int arr[], int arrSize, int startIndex, int endIndex)
- {
- for (int current = startIndex; current < endIndex; ++current)
- {
- cout << arr[current] << ' ';
- }
- 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