El Spiffy
El Spiffy

Reputation: 135

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;   
}

Upvotes: 0

Views: 900

Answers (3)

thebarylowi
thebarylowi

Reputation: 55

To dynamically allocate array you need to build code like this:

int *arr = new int[size];  // you have to remember to free memory when you won't need this array anymore - use delete[] achieve this

size variable don't have to be const and compiler don't need to know value of it during compilation. You can ask user to provide size :) Not to reverse your original table just assassin elements in reversed way:

for (int i = 0; i < size; ++i)
{
    arr[i] = numbers[size - i - 1]; <-- '-1' to not read outside of orginal array (in C++ index starts with 0)
}

If you want to revers table without using new one you should visit: Reverse Contents in Array. There are few methods to do this :)

Upvotes: 0

VolAnd
VolAnd

Reputation: 6407

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.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Dynamic memory management should be done using the C++ standard classes and concepts as available with either smart pointers or containers.

Using C++ language correctly doesn't require you to use new/delete for most of the use cases you actually need to cover.

Upvotes: 4

Related Questions