Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- #define ARR_MAX 50
- void inputData(ifstream& inFile, int& evens[], int& odds[], int& evenElements, int& oddElements);
- void bubbleSort(int arr[], int elements);
- int calcSum(int arr[], int elements);
- int calcMedian(int arr[], int elements);
- void outputArray(ofstream outFile, int arr[], int elements);
- void displayHighestValues(int arr[], int elements, int userValue);
- void displayLowestValues(int arr[], int elements, int userValue);
- void runtimeError(const string errorMessage);
- int main()
- {
- int evens[ARR_MAX], odds[ARR_MAX], evenElements = 0, oddElements = 0, userMenuSelection = 0;
- ifstream inFile;
- ofstream outFile;
- inFile.open ("...");
- if (!inFile)
- {
- runtimeError("Failed to open input file.");
- }
- inputData(inFile, evens[], odds[], evenElements, oddElements);
- bubbleSort(evens[], evenElements);
- bubbleSort(odds[], oddElements);
- inFile.close(); //here or at end of main()?
- 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";
- do
- {
- int evenSum, oddSum, evenMedian, oddMedian, userMenuSelection;
- bool sumCalculated = 0, medianCalculated = 0;
- if (userMenuSelection)
- {
- cout << "Select another option.\n";
- }
- else
- {
- cout << "Select an option by entering its corresponding number.\n";
- }
- cin >> userMenuSelection;
- switch(userMenuSelection)
- {
- case 1:
- if (!sumCalculated)
- {
- evenSum = calcSum(evens[], evenElements);
- oddSum = calcSum(odds[], oddElements)
- sumCalculated = 1;
- }
- cout << "# of Even entries: " << evenElements << endl
- << "# of Odd Entries: " << oddElements << endl
- << "Sum of Even List: " << evenSum << endl
- << "Sum of Odd List: " << oddSum;
- break;
- case 2:
- cout << "Average of Even List: " << (float) evenSum / evenElements << endl
- << "Average of Odd List: " << (float) oddSum / oddElements;
- break;
- case 3:
- if (!medianCalculated)
- {
- evenMedian = calcMedian(evens[], evenElements);
- oddMedian = calcMedian(odds[], oddElements)
- medianCalculated = 1;
- }
- cout << "Median of Even List: " << evenMedian << endl
- << "Median of Odd List: " << oddMedian;
- break;
- case 4:
- outFile.open ("...");
- outFile << "Even List:\n";
- outputArray(outFile, evens[], evenElements);
- outFile << "Odd List:\n";
- outputArray(outFile, odds[], oddElements);
- outFile.close();
- break;
- case 5:
- cout << "Enter a value for X: ";
- cin >> userValue;
- cout << "Highest " << userValue << " Even Values: "
- displayHighestValues(evens[], evenElements, userValue);
- cout << "Highest " << userValue << " Odd Values: "
- displayHighestValues(odds[], oddElements, userValue);
- break;
- case 6:
- cout << "Enter a value for X: ";
- cin >> userValue;
- cout << "Lowest " << userValue << " Even Values: "
- displayLowestValues(evens[], evenElements, userValue);
- cout << "Lowest " << userValue << " Odd Values: "
- displayLowestValues(odds[], oddElements, userValue);
- break;
- case 7:
- cout << "Exiting program.\n";
- break;
- default:
- cout << "Invalid menu option selected.\n";
- break;
- }
- } while (userChoice != 7);
- return 0;
- }
- void inputData(fstream& inFile, int& evens[], int& odds[], int& evenElements, int& oddElements)
- {
- int value;
- for (int i = 0; evenElements < ARR_MAX && oddElements < ARR_MAX && !feof(inFile); ++i)
- {
- inFile >> value;
- if (value % 2 == 0)
- {
- evens[evenElements++] = value;
- }
- else
- {
- odds[oddsElements++] = value;
- }
- }
- if (evenElements == 0 && oddElements == 0)
- {
- runtimeError("Input file is empty.");
- }
- if (!feof(inFile))
- {
- runtimeError("Excess entries detected.");
- }
- }
- void bubbleSort(int arr[], int elements)
- {
- int elmnt1, elmnt2;
- for (elmnt1 = 0; elmnt1 < elements - 1; ++elmnt1)
- {
- for (elmnt2 = 0; elmnt2 < elements - elmnt1 - 1; ++elmnt2)
- {
- if (arr[elmnt2] > arr[elmnt2 + 1])
- {
- int temp = arr[elmnt2];
- arr[elmnt2] = arr[elmnt2 + 1];
- arr[elmnt2 + 1] = temp;
- }
- }
- }
- }
- int calcSum(int arr[], int elements)
- {
- int sum = 0;
- for (int i = 0; i < elements; ++i)
- {
- sum += arr[i];
- }
- return sum;
- }
- int calcMedian(int arr[], int elements)
- {
- if (elements % 2 == 0)
- {
- return (float )(arr[elements / 2] + arr[(elements / 2) + 1]) / 2;
- }
- else
- {
- return arr[(elements / 2) + 1];
- }
- }
- void outputArray(ofstream outFile, int arr[], int elements)
- {
- for (int i = 0; i < elements; ++i)
- {
- outFile << arr[i] << ' ';
- }
- }
- void displayHighestValues(int arr[], int elements, int userValue);
- {
- for (int i = 0; i < userValue; ++i)
- {
- cout << arr[elements - 1 - i] << ' ';
- }
- }
- void displayLowestValues(int arr[], int elements, int userValue)
- {
- for (int i = 0; i < userValue; ++i)
- {
- cout << arr[i] << ' ';
- }
- }
- void runtimeError(const string errorMessage)
- {
- cout << "Error: " << errorMessage << " Exiting program.\n";
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement