c++arrayssortingdynamic-allocation

Using Dynamic Memory Allocation


So I've been told to create an array that will accept 10 integers from the user, store it into an array, and sort these values using a pointer bubble sort in ascending order.

I believe I have successfully done this much, but I am having trouble with the second part.

"Dynamically Allocate another array of 10 integers. Copy elements from the first into the second, but in reverse order (i.e. descending order). Display the elements of the first and second array in order and deallocate the dynamically allocated array."

I am able to display the first array in order, and I know that to deallocate the array you must use the the delete function, but I am not so sure on how to construct the Dynamic Array.

*I have not included the functions since I don't believe they are necessary for this part, but if I do, then I'll post them as well.

Thanks in advance for any suggestions and clarifications.

#include <iostream>

using namespace std;

void sortArray(int * , int);
void showArray(const int * , int);
int binarySearch(const int *, int, int);

int main(void)
{
    int const MAX_NUM = 10;
    int numbers [MAX_NUM];
    int counter;
    int findval;
    int index;
    char again;

    cout<< "Please enter 10 integer values."<< endl;
    for(counter=0; counter< MAX_NUM ; counter++)
    {
        cout << "Enter a value for "<< counter+1 << ": ";
        cin >> *(numbers+counter);
    }


    sortArray(numbers, 10);

    cout << endl << "The values in ascending order are: " << endl;
    showArray(numbers, 10);

    do
    {
        cout<< endl <<  "Enter the value you are searching for: ";
        cin >> findval; 
        cout << endl;
        index = binarySearch(numbers , MAX_NUM , findval);
        // Display the results of the search.
        if (index == -1)
            cout << "Number was not found." << endl << endl;
        else
            cout << "Number "<< findval<<" found in position " << index + 1 << endl << endl;
        // Does the user want to do this again?
        do
        {
            cout << "Would you like to look up another number? (y/n) ";
            cin >> again;
        }
        while(again != 'y' && again != 'Y' && again != 'n' && again != 'N');
    } 
    while (again == 'Y' || again == 'y');

    cout<< endl << "Thank You. Press the return key to continue...";

    cin.get();
    cin.ignore();
    return 0;   
}

Solution

  • Operator new should be used to allocate memory. For dealloaction use delete.

    Start from allocation the memory:

        int * dynArr = NULL; // pointer to work with dynamic array
        dynArr = new int[MAX_NUM]; // allocation of memory
    

    Then check that memory was allocated, like:

        if( dynArr != NULL )
        {
            // do something
        }
        else
        {
            // report about problem and do not use pointer
        }
    

    And use function for copying elements, e.g.:

    void reversCopy(const int * source, int * destination, int number)
    // Function for copying numbers from one array (memory) to other 
    // in the revers order (first element goes to the last position).
    // source - pointer to array where numbers will be read
    // destination - pointer to array where numbers will be written
    // number - number of elements to be copyed
    { 
        for(int i = 0; i < number; i++)
        {
            destination[i] = source[number - 1 - i];
        }
    }
    

    Eventually, free dymanic memory with operator:

      delete[] dynArr;
      dynArr = NULL;
    

    and do not use dynArr after that.