# Thread: am i going crazy?

1. ## am i going crazy?

I'm trying to take an array of n user defined values and write 2 functions...one that shows the value of this array and one that reverses the order of the values of the array. HEre are my two functions:
Code:
```void show_array(double ar[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Value #" << (i + 1) << "= ";
cout << ar[i] << "\n";
}
}

void reverse_array(double ar[], int n)
{
double temp;
for (int i = 0; i < n; i++)
{
temp = ar[i];
ar[i] = ar[i + (n- 1)];
ar[i + (n - 1)] = temp;
}
}```
My problem is that let's say a 3 element array is given the following values: ar[0] = 3, ar[1] = 2, and ar[2] = 1, the show_array function displays the following values: 1 (which is right), 8.59976e-298 (very wrong), and1.39068e-309 (even more wrong)...any idea why this isn't doing what I'd like it to...? Thanks -Chap

2. The problem is in your reverse_array function (which you must be calling before show_array, because thats the only way I could duplicate what your seeing).

The first iteration through the loop is good, because i = 0 and n-1 = 2, so you swap indexes 0 & 2. But the next two iterations are not going to work...let me know if you see the problem.

PK

3. Code:
```void reverse_array(double ar[], int n)
{
double temp;
int i;
for ( i = 0; i < (n-1)/2; i++)
{
temp = ar[i];

ar[i]=ar[n-i-1];
ar[n-i-1]=temp;
}
}```

4. Code:
```void reverse_array(double ar[], int n)
{
double temp;
int i;
for ( i = 0; i < (n-1)/2; i++)
{
temp = ar[i];

ar[i]=ar[n-i-1];
ar[n-i-1]=temp;
}
}```

5. > ar[i] = ar[i + (n- 1)];
> ar[i + (n - 1)] = temp;

Both of these should be n-1-i (or -i+n-1):
Code:
```        ar[i] = ar[n - 1 - i];
ar[n - 1 - i] = temp;```

6. Originally Posted by Chaplin27
I'm trying to take an array of n user defined values and write 2 functions...one that shows the value of this array and one that reverses the order of the values of the array. HEre are my two functions:

...

My problem is that let's say a 3 element array is given the following values: ar[0] = 3, ar[1] = 2, and ar[2] = 1, the show_array function displays the following values: 1 (which is right), 8.59976e-298 (very wrong), and1.39068e-309 (even more wrong)...any idea why this isn't doing what I'd like it to...? Thanks -Chap

Well, Micko has given you a solution, including a bug fix on the limits of the loop in reverse_array().

For future problems, here's a way to find out for yourself what's going on.

Go back to your original program. Now, show_array() seems pretty straightforward, and I'm guessing that if you called show_array() with your initial array values it gave you what you expected.

If it seems that reverse_array() is not working right, then put some output statements to see what it is trying to do.

Something like:

Code:
```void reverse_array(double ar[], int n)
{
double temp;
for (int i = 0; i < n; i++)
{
cout << "i = " << i << ", i+(n-1) = " << i+(n-1) << endl;
temp = ar[i];
ar[i] = ar[i + (n- 1)];
ar[i + (n - 1)] = temp;
}
}```
Seeing that the value of the second expression gives an out-of-range index value, you can go back to pencil-and-paper to see if you can discover what it should be. Then fix the code accordingly.

After you fix the array index expression, the output statement will also give you a hint as to why the loop limit should be changed.

cout<< is a very powerful debugging tool, and (I claim) faster than posting a request for help and waiting for a helpful response.

Regards,

Dave

7. I was really hoping no one would post the solution, as it seemed that Chaplin simply needed a nudge in the right place to bug hunt.

8. Well, I personaly prefer to post code when one who ask also post code. My experience is that it is much better to understand mistakes when examin right code