## momike205

Here is the program:

``````int i,n;
double v_in[20+1],v_out[20+1];

cout << "\ninput n ? ";
cin >> n;

for(i=1;i<=n;i++) {
cout << "input v_in[" << i << "] ? ";
cin >> v_in[i];
}

// reverse v_in and store the result in v_out
for(i=1;i<=n;i++) {
v_out[i] = v_in[n-i+1];
}

cout << "\nn = " << n;

cout << "\n\nv_in = ";
for(i=1;i<=n;i++) {
cout << "\n" << v_in[i];
}

cout << "\n\nv_out = ";
for(i=1;i<=n;i++) {
cout << "\n" << v_out[i];
}

cout << "\ndone.\n";
getch();

return 0; }
``````

Now in the Section in Bold, there's a part where he does this
`v_out[i] = v_in[n-i+1]`
What is the significance of `n-i+1`?? How does this reverse v_in???

## vmanes 1,165

A. please put code inside code tags, that makes it more readable.

B. In answer to your question, I'd ask you to work it out by hand. When i is 1, what is the index v_in and v_out? When you answer that, I think you'll see the significance of the statement.

## super-sonic 1

*Use code tags while posting.

*Take an example of n=10; then single step through the for loop of the code to understand the working of the code:
for the first time in the loop:
i = 1 then n-i+1= 10, hence `v_out[1]` is assigned the value `v_in[10].` for the second time in the loop:
i = 2 then n-i+1= 9, hence `v_out[2]` is assigned the value `v_in[9].` and so on

* A point to remember , if an array contains say 5 elements, `int a[5];` then the first element in C and C++ is `a[0]` and the last element is `a[4]` . I am not sure if you have intentionally avoided using the zeroth element.

## momike205

ya it was on purpose to start at 1
but thank u very much guys, i understand it... So helpful and quick!
Awesome! :)