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;
- #define ARR_MAX 15
- 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 DisplayHighestValues(int arr[], int arrSize, int userValue);
- void DisplayLowestValues(int arr[], int arrSize, int userValue);
- void RuntimeError(const string errorMessage);
- int main()
- {
- ofstream outFile;
- 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 << "[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.\n\n";
- do
- {
- static int evenSum = CalcArraySum(evens, evenCount), oddSum = CalcArraySum(odds, oddCount);
- int userOutputChoice; //for options 5 & 6
- float evenMedian = 0, oddMedian = 0;
- static bool sumCalculated = 0, medianCalculated = 0;
- cout << setprecision(2) << fixed; //where to put this?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- if (userMenuSelection)
- {
- cout << "\n\nSelect another option: ";
- }
- else
- {
- cout << "Select an option by entering its corresponding number: ";
- }
- cin >> userMenuSelection; //assuming only integer input
- switch (userMenuSelection)
- {
- case 1:
- cout << "# of Even Entires: " << evenCount
- << "\nSum of Even Entries: " << evenSum
- << "\n# of Odd Entires: " << oddCount
- << "\nSum of Odd Entries: " << oddSum;
- break;
- case 2:
- 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";
- }
- break;
- case 3:
- if (!medianCalculated)
- {
- evenMedian = CalcArrayMedian(evens, evenCount);
- oddMedian = CalcArrayMedian(odds, oddCount);
- medianCalculated = 1;
- }
- cout << "Median of Even Entries: ";
- if (evenCount)
- {
- cout << evenMedian;
- }
- else
- {
- cout << "N/A\n";
- }
- cout << "\nMedian of odd Entries: ";
- if (oddCount)
- {
- cout << oddMedian;
- }
- else
- {
- cout << "N/A\n";
- }
- break;
- case 4:
- 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";
- break;
- case 5:
- cout << "Enter a value for X: ";
- cin >> userOutputChoice;
- cout << "Highest " << userOutputChoice << " Even Entries: ";
- DisplayHighestValues(evens, evenCount, userOutputChoice);
- cout << "Highest " << userOutputChoice << " Odd Entries: ";
- DisplayHighestValues(odds, oddCount, userOutputChoice);
- break;
- case 6:
- cout << "Enter a value for X: ";
- cin >> userOutputChoice;
- cout << "Lowest " << userOutputChoice << " Even Entries: ";
- DisplayLowestValues(evens, evenCount, userOutputChoice);
- cout << "Lowest " << userOutputChoice << " Odd Entries: ";
- DisplayLowestValues(odds, oddCount, userOutputChoice);
- break;
- case 7:
- 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;
- }
- //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.\n\n";
- }
- 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."); //more than ARR_MAX even or odd entries are read
- }
- }
- //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; //return in if body, is this ok?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- 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)
- {
- outFile << "N/A";
- }
- }
- //Prints highest X values of array determined by user
- //pre: array is populated, array size is defined and user value is defined
- //post: highest X values of array are printed
- void DisplayHighestValues(int arr[], int arrSize, int userValue)
- {
- int i;
- for (i = 0; i < userValue && i < arrSize; ++i)
- {
- cout << arr[arrSize - 1 - i] << ' ';
- }
- if (!i)
- {
- cout << "N/A";
- }
- cout << endl;
- }
- //Prints lowest X values of array determined by user
- //pre: array is populated, array size is defined and user value is defined
- //post: lowest X values of array are printed
- void DisplayLowestValues(int arr[], int arrSize, int userValue)
- {
- int i;
- for (i = 0; i < userValue && i < arrSize; ++i)
- {
- cout << arr[i] << ' ';
- }
- if (!i)
- {
- 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);
- }
Add Comment
Please, Sign In to add comment