# Thread: reverse array logic error

1. ## reverse array logic error

I'm having problems getting the last part of this program to work. Help!!
Code:
```//reverse array prblm 5 pg.290 c++ primer plus

#include <iostream>
using namespace std;

int fill_array(double ar[], int limit);
void show_array(const double ar[],int limit);
void reverse(double ar[],int limit);

int main()
{
int limit=20;
double ar[20];
limit=fill_array(ar,limit);
cout << "You entered:\n";
show_array(ar,limit);
reverse(ar,limit);
cout << "Reversed:\n";
show_array(ar,limit);
cout << "Reversed again except for first and last elements:\n";
reverse(ar+1,limit-1);
show_array(ar,limit);
return 0;
}

int fill_array(double ar[], int limit)
{
double temp;
int i;
for (i=0;i<limit; i++)
{
cout << "Enter value #"<<(i+1)<<":";
if (!(cin>>temp))
break;
ar[i]=temp;
}
return i;
}

void show_array(const double ar[],int limit)
{
for (int i=0;i<limit;i++)
{
cout << "Array element #" << (i+1) << ":" << ar[i] << endl;
}
}

void reverse(double ar[],int limit)
{
double temp;
for (int i=0;i<limit;i++)
{
if (i==limit-i-1)
break;
temp=ar[limit-i-1];
ar[limit-i-1]=ar[i];
ar[i]=temp;
}
}```

2. Here is a reverse function that I just threw together:

Code:
```void reverse(double ar[], int limit)
{
double *tempArray = new double[limit];

for( int i = 0; i < limit; i++ )
tempArray[limit - i] = ar[i];

for( int k = 0; k < limit; k++)
ar[k] = tempArray[k];

delete [] tempArray;
}```
It should work. Try it out and let me know.

3. biosx's system writes all elements backward to a temp array and then rewrites the temp array back to the original array. Should work fine.

To do it in one read through using switches like rippascal attempted I would just make a slight change to the following line:

for (int i=0;i<limit;i++)

I would have the terminating condition be i < limit/2. the dividend will be an integer since the compiler will use integer math. You only need half as many times through loop since you switch 2 elements with every loop. Think about it like this;

array[3] = {1, 2, 3};
limit = 3;
limit/2 = 1;
for(int i = 0; i < limit/2; i++)
{
temp = array[i];
arrray[i] = array[limit - 1 - i];
array[limit - 1 - i] = temp;
}

First time through loop array[0] is switched with array[2] with the results being 3 2 1 if array elements were read out after first loop through. If you did the loop limit times, then you would be switching back to original positions once i was > limit/2.

4. ## xcept for first, last

I thought about both methods you all posted for reversing the array. The problem i am having is calling the function to reverse all but the first and last elements.

reverse(ar+1,limit-1);

shouldnt this work if double ar[] is equivalent to double * ar. passing then the address ar+1 in affect giving an array starting on the second element where the function just sees an array
void reverse(double ar[],int limit)

5. ## solved

Code:
```int main()
{
int limit=20;
double ar[20];
limit=fill_array(ar,limit);
cout << "You entered:\n";
show_array(ar,limit);
reverse(ar,limit);
cout << "Reversed:\n";
show_array(ar,limit);
cout << "Reversed again except for first and last elements:\n";
reverse(&ar[1],limit-2); //******** this is the solution
show_array(ar,limit);
return 0;
}```

6. I would avoid fooling around with pointer math and expand the parameter list passed to the function to include where you want to start the reversal and where you want to end the reversal. There are several ways to do this. Here's one possible way:

Code:
```void reverse(int  * array, int limit, int startIndex, int endIndex)
{
int i;
int loops = 0;
int temp;
if(startIndex >= 0 && endIndex < limit && startIndex < endIndex)
{
for(i = startIndex; i < ((endIndex - startIndex)/2); i++)
{
temp = array[i];
array[i] = array[endIndex - loops];
array[endIndex - loops]= temp;
loops++;
}
}
}```
HTH