reversing selected elements of an array

In this code, the line:
rev(&orr[1], numentries - 2);
results in the first and last numbers in the array reversing and leaves the middle numbers untouched. Why is it not the reverse case, that is why is it not the case that the the first and last numbers are untouched and the middle numbers are reversed? I thought that &orr[1] would start the reverse function from the second element of the array and that numentries - 2 would leave the last entry untouched.

Also, when I try to preview messages in Firefox and Chrome it does not work, that is a window opens but it just has my name, no message.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>

const int maxsize = 10;

int inp(int *, int size);
void rev(int *, int size);
void display(int *, int size);
using namespace std;

int main()
{
	int orr[maxsize];
	int numentries = inp(orr, maxsize);
	display(orr, numentries);
	rev(orr, numentries);
	display(orr, numentries);
	if (numentries > 2){
		rev(&orr[1], numentries - 2);
		display (orr, numentries);
	}
	return 0;
}

int inp(int *dorr, int size) {
	int val = 0, numval = 0;
	cout << "Enter values: ";
	while (cin >> val && numval < size) {
		dorr [numval++] = val;
	}
	return numval;
}

void display(int *dorr, int size) {
	for (int x = 0; x < size; x++) {
		cout << "Value #" << (x + 1) << ": " << dorr [x] << endl;
	}
	cout << endl;
}

void rev(int *dorr, int size) {
	for (int x = 0; x < (size/2); x++) {
		int temp = dorr[x];
		dorr[x] = dorr[size - x - 1];
		dorr[size - x - 1] = temp;
	}
}
First you call rev(orr, numentries); which reverses the whole array, then you call rev(&orr[1], numentries - 2); which reverses the middle numbers so that they end up in their original positions. The effect of both these calls together is that only the first and last numbers are swapped.

I also noticed the program requires you to enter one extra number that is not added to the array. To fix this you should check to see if numval is less than size before reading the number.

 
while (numval < size && cin >> val) {


Also, when I try to preview messages in Firefox and Chrome it does not work, that is a window opens but it just has my name, no message.

Unfortunately, this is a forum bug. If you first post the message and then edit it you will be able to view the preview.
I thought that &orr[1] would start the reverse function from the second element of the array and that numentries - 2 would leave the last entry untouched.


Yes, that's exactly what it does!

However, you have called the function rev TWICE.
The first time, on line 15, reverses everything.
The second time, on line 18, reverses just the internal elements
The net effect is that the internal elements are reversed ... and then reversed back again (so no overall change), whilst the outermost elements are just reversed once (so end up being swapped).


Note: There's something wrong with your input routine.
Thanks very much. I didn't realise that after the first call the values were staying reversed. Is there then a way of making the initial call and leaving the elements in their original order? (I mean without writing another function to reverse them back.)
Topic archived. No new replies allowed.