post #1 of 1
Thread Starter 

Well, yet again some more of my entry level C++ programs from school.

This one is a menu based program that creates a dynamic array, processes that information through a bunch of professor determined parameters, and then spits the information back out

Here are the instructions:

 

Warning: Spoiler! (Click to show)

Purpose: Demonstrate the ability to use pointers to handle arrays and variable locations, and accomplish dynamic memory allocation.

Write a C++ program to do the following:

In the main function display a menu to get user’s option for:

1. Create and initialize a dynamic array
2. Display statistics on the array
3. Search for an element
4. Quit

The user should choose 1 first to proceed with the rest of the options.

When option 1 is chosen, ask the user for an integer value for size from inside the main function. This value will be the size of the array that you will create dynamically. Then call a function with the following prototype and description:

int *createArray(int &size) : Dynamically allocate and initialize with random numbers an array of size size. The parameter to this function is a reference variable which will store the size of the array. The function will return a pointer to the dynamically allocated array. In the main function, display contents of the array.

When option 2 is chosen, call a function with the following prototype and description:
void findStats(int *arr, int size, int &min, int &max, double &average) : This function finds the statistics about the array by searching the minimum and maximum elements and by calculating the average of elements (The average is calculated by adding each element value in the array then dividing this sum by the size of the array).
The first parameter to the function is a pointer to the dynamically created array, the second parameter is the size of the array that is passed by value, the third parameter is the minimum value that is passed by reference, the fourth parameter is the maximum value that is passed by reference, and the last parameter is the average value that is passed by reference. The findStats function returns a void, i.e. nothing. Still, since min, max, and average are passed by reference, you should be able to display these variables’ calculated values from inside the caller (main) function. Display the value of each statistic properly in the caller function.

When option 3 is chosen, get an integer value for an element from inside the main function. Then call a function with the following prototype and description:
int *searchElement(int *arr, int size, int *element): Search for the element stored in the variable element inside the array. The first parameter to the function is a pointer to the array. The second parameter is the size of the array. The third parameter is a pointer to the element variable for which you’re going to search. You may either use linear or binary search here, whichever is most convenient for you, but if you use a binary search, you must sort the array. The function returns a pointer to the element found in the array, or a 0 if the element is not found to the main function. Please display properly where the element is found (if found), or display not found (if the element is not found) in the main function.

When option 4 is chosen, quit the program.

 

 

Simple enough... just not getting the correct results from function findStats and searchElement. I am not sure exactly what I am doing wrong. Any help is appreciated and as always +rep.

(findStats returns whatever value is in element 0 of the array for min, max, and average. searchElement does not find the requested integer in the array)
 

Code:
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int *arr;


// Prototypes
int *createArray(int &size); 
void findStats (int *arr, int size, int &min, int &max, double &average);
int *searchElement(int *arr, int size, int *element);



int main()
{
        int                     userOption,
                                ix = 0,
                                size,
                                min = 0,
                                max = 0,
                                userElement;
        double           average = 0;

        // Menu loop (exit if 4).
        do
        {
                // Display menu and request input.
                cout << "1. Create and initialize a dynamic array" << endl;
                cout << "2. Display statistics on the array" << endl;
                cout << "3. Search for an element" << endl;
                cout << "4. Quit" << endl << endl;
                cout << "Please select option (1-4)" << endl;

                cin >> userOption;

                // Validate user input.
                if ( userOption != 1 && userOption != 4 && ix == 0 )
                { 
                cout << endl << "Error; initial choice must be 1." << endl << endl;
                }

                ix++;

                // User selection: 1
                if ( userOption == 1 )
                {
                        // Request size input, call createArray, return pointer to array.       
                        cout << "Please enter an integer value for the size of the array." << endl;
                        cin >> size;
                        int arr = *createArray(size);

                        // Display contents of array.
                        for (int ia = 0; ia < size; ia++)

                                cout << "Element " << ia << ":" << arr + ia << endl;
                                cout << endl << endl;
                }

                // If user selects 2 call findStats and display.
                else if ( userOption ==2 )
                {
                        findStats(arr, size, min, max, average);

                        // Display stats.
                        cout << endl << "Statistics:" << endl;
                        cout << "Min: " << min << endl;
                        cout << "Max: " << max << endl;
                        cout << "Average: " << average << endl << endl;

                }

                // If user selects 3 call searchElement and display.
                else if ( userOption == 3 )
                {
                        // Request user input, call searchElement function.
                        cout << endl << " Enter an integer to search the array for." << endl;
                        cin >> userElement;
                        int *element = &userElement;

                        int location = *searchElement ( arr, size, element ); 

                        //Display results.
                        if (location)

                                cout << "Integer found in element: " << location << endl;

                        else 

                                cout << "Integer not found." << endl;

                        cout << "Select 4 to end." << endl;

                }

        } while ( userOption != 4); // User selects 4 exit.

                return 0;

} // End function main.



// Create and initialize dynamic array.
int *createArray (int &size)
{

        // Initialize dynamic array with random numbers.
        int* userArray = new int [size] ();

        for (int ixx = 0; ixx < size; ixx++)
        {
                srand (time(NULL));

                userArray[ixx] = rand() % 100 + 1;
        }

        // Set pointer to address of array and return.  
        arr = *&userArray;
        return arr;

}// End function createArray.


void findStats (int *arr, int size, int &min, int &max, double &average) 
{
        int     ifs = 0,
                sum = 0;

        min = (arr[ifs]);

        for (ifs = 0; ifs < size; ifs++)
        {

                if (min > (arr[ifs]))
                {
                        min = (arr[ifs]);
                }

                if (max < (arr[ifs]))
                {
                        max = (arr[ifs]);
                }

                sum += (arr[ifs]);
        }
        
        average = sum / size;
        
}// End function findStats.

int *searchElement(int *arr, int size, int *element)
{
        int             *location, 
                        index = 0,
                        position = -1;
        bool    found = false;

        while ( index < size && !found)
        {
                if ( arr[index] == *element );
                {
                        found = true;
                        position = index;
                }

                index++;
        }

        return location = &position;

}// End function searchElement.