Swap elements in array to reverse an array

I got an assignment to reverse an dynamic array in C++. So far, from my logic, I thinking of loop thru the array to reverse it. And here comes my code :

int main ()
{
    const int size = 10;
    int num_array[size];

    srand (time(NULL));

    for (int count = 0; count< sizeof(num_array)/sizeof(num_array[0]) ; count++){
        /* generate secret number between 1 and 100: */
        num_array[count] = rand() % 100 + 1;
        cout << num_array[count] << " " ;
    }

    reverse(num_array[size],size);

    cout << endl;

    system("PAUSE");
    return 0;
}

void reverse(int num_array[], int size)
{
    for (int count =0; count< sizeof(num_array)/sizeof(num_array[0]); count++){
        cout << num_array[sizeof(num_array)/sizeof(num_array[0])-1-count] << " " ;
    }

    return;
}

Somehow I think my logic was there but this code doesn't works, there's some error. However, my teacher told me that this isn't the way what the question wants. And here is the question :

    Write a function reverse that reverses the sequence of elements in an array. For example, if reverse is called with an array containing 1 4 9 16 9 7 4 9 11,
then the array is changed to 11 9 4 7 9 16 9 4 1.

So far, she told us in the reverse method, you need to swap for the array element. So here's my question how to swap array element so that the array entered would be reversed?

Thanks in advance.

Updated portion

    int main ()
{
const int size = 10;
int num_array[size];

srand (time(NULL));

for (int count = 0; count< size ; count++){
    /* generate secret number between 1 and 100: */
    num_array[count] = rand() % 100 + 1;
    cout << num_array[count] << " " ;
}

reverse(num_array,size);

cout << endl;

system("PAUSE");
return 0;

}

void reverse(int num_array[], const int& size)
{
for (int count =0; count< size/2; count++){
    int first = num_array[0];
    int last = num_array[count-1];
    int temp = first;
    first = last;
    last = temp;
}

}

Answers


You reverse function should look like this:

void reverse(int* array, const size_t size)
{
    for (size_t i = 0; i < size / 2; i++)
    {
        // Do stuff...
    }
}

And call it like:

reverse(num_array, size);

I am no C++ programmer, however I do see an easy solution to this problem. By simply using a for loop and an extra array (of the same size) you should be able to reverse the array with ease.

By using a for loop, starting at the last element of the array, and adding them in sequence to the new array, it should be fairly simple to end up with a reversed array. It would be something like this:

Declare two arrays of the same size (10 it seems)

  • Array1 contains your random numbers
  • Array2 is empty, but can consist of 10 elements

Also declare an integer, which will keep track of the progression of the for loop, but in the opposite direction. i.e not from the end but from the start.

  • Counter = 0

Next you will need to create a for loop to start from the end of the first array, and add the values to the start of the second array. Thus we will create a for loop to do so. The for loop will be something like this:

for(int i = lengthOfArray1; i > 0; i--){
    Array2[Counter] = Array1[i]
    Counter++
}

If you only wish to print it out, you would not need the counter, or the second array, you will simply use the Array1 elements and print them out with that style of for loop.

That's it. You could set Array1 = Array2 afterward if you wished to keep Array1 the original for some reason. Hope this helps a bit, changing it to C++ is your job on this one unfortunately.


Need Your Help

jpa merge and default values in database

jpa merge field

I'm trying to make a Spring MVC application using Spring Security. I have a problem with JPA - I have a code like this (part of register action):

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.