reverse array elements

Hello,
I am trying to write a program that reverses the elements of an
array by using an function 'myreverse'. The function should reverse original array and return nothing.

In my program, the function 'myreverse' is not getting invoked and the original array is being displayed as it is.

please help me find my mistake.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

void myreverse(int arr[],int n)
{
    int *p=&arr[n-1];
    int temp;
    for(int i=0;i<n;++i)
    {
        temp=*p;
        *p=arr[i];
        arr[i]=temp;
        --p;
    }


}
int main()
{
    int arr[10],n;
    cout<<"enter the number of elements";
    cin>>n;
    cout<<"enter the array elements";
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"the reverse array is \n";
    myreverse(arr,n);
    for(int i=0;i<n;i++)
        cout<<arr[i];
    return 0;
}

Last edited on
The problem isn't that myreverse isn't getting called, but that you're reversing the array twice resulting in the original array.

Consider an array of just two numbers:
1,2
On the first iteration through the array, you reverse the elements resulting in: 2,1
On the second iteration, you reverse them again, resulting in the original array of: 1,2

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
The program is getting into the reverse function, but it's not giving you the result you want.

If you're setting the array size to be 10 - you'll run into an issue if the user chooses to input a number greater than 10 elements to enter. If you want them to be able to enter any number up to 10, I'd put some sort of logic to reject any number > 10 that will cause your array to go out of bounds and the program to crash.
@AbstractionAnon:
I have used code tags, thanks for pointing out. But i still dont get where i am reversing the array twice.

You can throw in some cout statements to see what's happening at each step or your debugger may let you see how values are changing. I stuck cout statements in between steps in your for loop for the reverse function so you can see what's happening with each iteration. I entered an array size of 5 with array elements 1,2,3,4,5.

enter the number of elements
enter the array elements
the array is 12345
reverse function entered
iteration number 0
array after *p=arr[i]
12341
array after arr[i]=temp
52341
iteration number 1
array after *p=arr[i]
52321
array after arr[i]=temp
54321
iteration number 2
array after *p=arr[i]
54321
array after arr[i]=temp
54321
iteration number 3
array after *p=arr[i]
52321
array after arr[i]=temp
52341
iteration number 4
array after *p=arr[i]
12341
array after arr[i]=temp
12345
//back to main function
the reverse array is 
12345
Last edited on
@wildblue

Thank you, i found my mistake.
cout statements can be helpful in programming
Topic archived. No new replies allowed.