Aug 22, 2020 at 7:38pm UTC
}[/code]
Last edited on Aug 24, 2020 at 3:20am UTC
Aug 22, 2020 at 8:08pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
void my_swap(double * x, double * y)
{
double temporary = *x;
*x = *y;
*y = temporary;
}
void reverse(double begin[], int size)
{
for (double * end = begin + size; end > begin; )
my_swap(begin++, --end);
}
Last edited on Aug 22, 2020 at 8:09pm UTC
Aug 22, 2020 at 8:14pm UTC
You have a for loop inside a while loop. They are both trying to do the reversing. You only need one of them.
Show ALL code (so that we can run it) and put it in CODE TAGS (so that we can run it without downloading it, as well as seeing the code structure from your indentation ...)
Aug 22, 2020 at 8:23pm UTC
Okay here is my code :
1 2 3 4 5 6 7
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
Last edited on Aug 24, 2020 at 3:20am UTC
Aug 22, 2020 at 8:37pm UTC
Remove the spurious uncommented line (59) and it runs OK and appears to produce the correct answer.
Enter the size of the array : 7
After initializing
13.000 0x756fc0
16.000 0x756fc8
27.000 0x756fd0
25.000 0x756fd8
23.000 0x756fe0
25.000 0x756fe8
16.000 0x756ff0
After reversing
16.000 0x756fc0
25.000 0x756fc8
23.000 0x756fd0
25.000 0x756fd8
27.000 0x756fe0
16.000 0x756fe8
13.000 0x756ff0
You need a call to srand() somewhere, or you'll always get the same array.
You can also do it with
1 2 3 4 5 6 7 8 9
void reverse( double *first, double *last )
{
while ( first < last )
{
double comp = *first;
*first++ = *last;
*last-- = comp;
}
}
called by
reverse(p_arr, p_arr + size_arr - 1 );
Last edited on Aug 22, 2020 at 8:47pm UTC