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.
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/
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.
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