0
    #include <iostream>
    #include <cstdlib>

    using namespace std;

    void swapNum(int *q, int *p)
    {
        int temp;
        temp = *q;
        *q = *p;
        *p = temp;
    }

    void reverse(int *ip, int const size)
    {
        for (int k = 0; k < size; k++)
        {
            if (k == (size/2))
            {
                int *q = &ip[k];
                int *p = &ip[k+1];
                swapNum(q,p);
                break;
            }
            else
                swap(ip[k], ip[size-k]);
        }
    }

    int main()
    {
        const int size = 20;
        int arr[size];
        int *ip;
        ip = arr;

        cout << "Please enter 20 different numbers." << endl;

        for (int i = 0; i < size; i++)
        {
            cout << "\nNumber " << i+1 << " = ";
            cin >> ip[i];
        }

        reverse(ip, size);

        cout << "I will now print out the numbers in reverse order." << endl;

        for (int j = 0; j < size; j++)
        {
            cout << ip[j] << " ";
        }

        return 0;
    }

When I try to run this program it crashes. I don't know what's wrong and the purpose of my program is to swap number of the array using pointers. I am recently introduced to this so I am not that familiar with it. But I think that I am swapping the address of the numbers instead of swapping the numbers in the address. Correct me if I am wrong.

5
  • (Removed that comment, sorry, need more coffee.) Commented Mar 3, 2014 at 20:28
  • Where is the definition of swap? Commented Mar 3, 2014 at 20:31
  • @Gareth No it shouldn't. That will just swap the local variable values, not the integers in the array. Commented Mar 3, 2014 at 20:33
  • It works for me. Have you tried stepping through the code to see where it crashes? Commented Mar 3, 2014 at 20:35
  • To clarify I ran the code at www.sourcelair.com, I also changed the first for loop to ip[i] = i (fills with the values 0-19) Commented Mar 3, 2014 at 20:37

3 Answers 3

2

You're accessing outside the array bounds in reverse() when you do:

swap(ip[k], ip[size-k]);

On the first iteration of the for loop, k is 0 and size-k is size. But array indexes run from 0 to size-1. So it should be:

swap(ip[k], ip[size-k-1]);

But I don't see a definition of swap in your program. I think it should actually be:

swapNum(&ip[k], &ip[size-k-1]);

Another improvement: Instead of handling size == k/2 specially and using break, just use size < k/2 as the bound test in the for loop.

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

1 Comment

Yes I used the a separate swap method because my Prof told me if i try to swap the middle 2 numbers, it might overload and i forgot what else happens(i think it crashes).
1
swap(ip[k], ip[size-k]);

Your problem is there. size - k when k is 0 will lead to undefined behavior (accessing an array out of bounds). Your loop structure in reverse can be simplified:

for (int k = 0; k < size / 2; k++)
    swapNum(&ip[k], &ip[size - k - 1]); // updated to use the address since your swap function takes pointers.

1 Comment

wow thanks! LOL i just realized my assignment told me to use size - x -1 in the swap function as well ><
0

Function reverse is invalid

void reverse(int *ip, int const size)
{
    for (int k = 0; k < size; k++)
    {
        if (k == (size/2))
        {
            int *q = &ip[k];
            int *p = &ip[k+1];
            swapNum(q,p);
            break;
        }
        else
            swap(ip[k], ip[size-k]);
    }
}

For example when k is equal to 0 then you call

            swap(ip[0], ip[size]);

However the array has no element with index size.

ALso you mess two functions std::swap and swapNum

This code snippet also is invalid

        if (k == (size/2))
        {
            int *q = &ip[k];
            int *p = &ip[k+1];
            swapNum(q,p);
            break;
        }

When size is an even number (or an odd number) as in your code then you make incorrect swap. For example if size is equal to 20 then you should swap ip[9[ with ip[10]. However according to the code snippet above you swap ip[10] with ip[11].

You could use standard algorithm std::reverse

for example

#include <algorithm>
#include <iterator>

//...

std::reverse( std::begin( arr ), std::end( arr ) );

or

#include <algorithm>

//...

std::reverse( arr, arr + size );

If you want to write the function yourself then it could look as

void reverse( int a[], int size )
{
    for (int k = 0; k < size / 2; k++)
    {
        int tmp = a[k];
        a[k] = a[size-k-1];
        a[size-k-1] = tmp; 
    }
}

Or if you want to use your function swapNum then

void reverse( int a[], int size )
{
    for (int k = 0; k < size / 2; k++)
    {
        swapNum( &a[k], &a[size-k-1] );
    }
}

EDIT: I removed qualifier const from the first parameter that was a typo.

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.