0

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;   
}
3
  • 1
    You are not dynamically allocating anything here! Your array is pretty much statically allocated int numbers [MAX_NUM]; Commented Nov 24, 2015 at 20:21
  • I know I am not, I'm unsure on how to construct this dynamic array with what I've already put together. Commented Nov 24, 2015 at 20:23
  • 1
    Raw arrays in C++ are not dynamic arrays (grow larger or smaller as needed), but may be allocated dynamically (essentially the dynamic-arrays tag is incorrect to use for this question - check the description of the tags when entering). Commented Nov 24, 2015 at 20:26

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Smart pointers or containers are a good way to go, but something tells me that this is more of an academic exercise to demonstrate an understanding of the process of dynamically allocating and deallocating memory.
@Derek Well, I don't care about academia as long not stated explicitly. We have these mechanisms in place and asking beyond seems to be much useless. I actually hate academia trying to teach such in first place, before getting the available language constructs right
@πάνταῥεῖ 10000% agree. That's why lots of students learn C++ the wrong way, then complain how hard and error-prone the language is. The instructor ends up teaching C with classes. It's not 1981 anymore.
0

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.

19 Comments

For an array in C++, this is not quite the correct answer.
More specifically, for arrays, new[] should be used to allocate memory and delete[] to deallocate memory.
@BaummitAugen Sure, but the problem explicitly asks to use dynamic allocation.
@AnkitAcharya Perhaps I am being blinded by my vast intellect making me unable to empathize, but teaching dynamically allocated array before std::vector is placing the cart before the horse. std::vector is pretty much fire-and-forget while the manual memory management involved in array handling is definitely not what I see as the basics.
@ElSpiffy Well, your professor is an ignorant twat, and doesn't teach C++ really. Do not take that, I'm recommending about telling them so!
|
0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.