Anonymous Anonymous - 16 days ago
2203 0

No description

C++

ArrayCreateDisplayHighLowAverageTotalSequentialSearchBubbleSortBinarySearch

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

// All non-main functions
void populate(int, int[]);
void display(int, int[]);
void highLow(int, int[]);
void sequentialSearch(int, int[]);
void bubbleSort(int, int[]);
void displayTwo(int, int[]);
int binarySearch(int, int[], int);

// Main function
int main()
{
    // Defining the array and size of the array
    int arraySize = 25;
    int array[arraySize];

    // Defining values for menu use later and search values
    int userInputValue;
    char menuSelection;

    // Seeding the random number generator based on time
    srand(time(0));

    // Core program using do-while loop to allow running via prompt at end
    do
    {
        // Function calls in requested order
        populate(arraySize, array);

        display(arraySize, array);

        highLow(arraySize, array);

        sequentialSearch(arraySize, array);

        bubbleSort(arraySize, array);

        displayTwo(arraySize, array);

        // Prompting user for search value
        cout << endl;
        cout << "Enter a value to search for: ";
        cin >> userInputValue;
        cout << endl;

        // Accepting the input from binary search for position in array
        int foundValue = binarySearch(arraySize, array, userInputValue);

        // Determining whether requested value was found based on input and return from function
        if(foundValue >= 0)
        {
            cout << "The value's location in the array is at [" << foundValue + 1 << "]" << endl;
        }
        else
        {
            cout << "Your search request of " << userInputValue << "was not found.";
        }

        // Prompting user for program to run again or close
        cout << "Would you like to search another value? y/n: ";
        cin >> menuSelection;

    }while((menuSelection == 'y') || (menuSelection == 'Y'));

    return 0;
}

// Populate the array defined in main
void populate(int arraySize, int array[])
{
    for(int i = 0; i < arraySize; i++)
    {
        // Random numbers inputted to i-value of array
        array[i] = rand()%100 + 1;
    }
}

// Display the array contents to the user
void display(int arraySize, int array[])
{
    for(int i = 0; i < arraySize; i++)
    {
        // Displaying well formatted array contents
        cout << "Array [" << i + 1 << "]:       " << array[i] << endl;
    }
}

// Accepts array values and determines the highest, lowest, and average numbers of the array. It also displays the total.
void highLow(int arraySize, int array[])
{
    // All values used (aside from i etc)
    int high = array[0];
    int low = array[0];
    int total = 0;
    int average;

    // Nested loops to determine values and math along with it
    for(int i = 0; i < arraySize; i++)
    {
        if(array[i] > high)
        {
            high = array[i];
        }
        else if(low > array[i])
        {
            low = array[i];
        }
        total = total + array[i];
    }

    // Defining average based on final value of total
    average = total / arraySize;

    // Display the results to the user
    cout << endl;
    cout << "High: " << high << endl;
    cout << "Low: " << low << endl;
    cout << "Average: " << average << endl;
    cout << "Total: " << total << endl << endl;

}

// Searches one position at a time for the requested search value
void sequentialSearch(int arraySize, int array[])
{
    // Variables used
    int searchValue;
    int searchDone = 0;

    // Prompting user for search value
    cout << "Enter a number to search for: ";
    cin >> searchValue;
    cout << endl;

    // Searching array for value
    for(int i = 0; i < arraySize; i++)
    {
        if(searchValue == array[i])
        {
            cout << "Value found at Array [" << i + 1 << "]" << endl;
            searchDone = 1;
            cout << endl;
        }
    }

    // Checking for value change in search completion variable
    if(searchDone == 0)
    {
        cout << "Value not found";
        cout << endl;
    }
}

// Sorts the variables in the array very slowly via swapping
void bubbleSort(int arraySize, int array[])
{
    // Nested loops for search and eventual value swaps
    for(int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < arraySize; j++)
        {
            if(array[j] > array[i])
            {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}

// Display the sorted array back to the user
void displayTwo(int arraySize, int array[])
{
    for(int i = 0; i < arraySize; i++)
    {
        cout << "Array [" << i + 1 << "]:     " << array[i] << endl;
    }
}

// Accepts user's requested value from main, and searches the array by constantly redifing the middle, first, and last
int binarySearch(int arraySize, int array[], int searchValue)
{
    // All name-variables used in function
    int firstValue = 0;
    int lastValue = arraySize - 1;
    int middleValue;

    // Comparing values of first value of array to last to check sorting
    while(firstValue <= lastValue)
    {
        // Redefining middle value via first and last values
        middleValue = (firstValue + lastValue)/2;

        // Checking search value against middle value position as over, under, or equal to
        if(searchValue == array[middleValue])
        {
            return middleValue;
        }
        else if(searchValue < array[middleValue])
        {
            lastValue = middleValue - 1;
        }
        else
        {
            firstValue = middleValue + 1;
        }
    }
}