reversing an array?

This problem is supposed to reverse an array without changing main. I've gotten to here, but for some reason it only reverses the first three elements of the array. Could someone help me figure out why that is happening?

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
  

#include <iostream>
using namespace std;
void backwards(int [], int);

int main()
{
  int arr[] = {2,8,17,3,5,16}; int i;

  backwards(arr,6);

  for (i = 0; i< 6; i++)
    cout<<arr[i]<<endl;

//system("pause");
return 0;
}

void backwards(int  array[], int number)
{
  /*Pre: array[] - array of integers
            number - number of elements in the array that have values
    Post: Nothing
    Purpose: Reverse the order of the elements of an array */
    int i; int temp;int j;
    for(i = 0; i < number;i++)
   {
     temp = array[number - 1 - i];
     array [i] = array [number - 1 - i];
     array [i] = temp;
  }

return;

}
You are close with the swapping mechanism...
1
2
3
4
5
6
7
int first = 8;
int second = 29;
int temp;

temp = first;
first = second;
second = temp;


Line 30-31 should follow this pattern...
Last edited on
One way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void backwards(int  array [], int number)
{
  int first = 0;
  int last = number - 1; // last legal index

  while (first < last)
  {
    int temp = array[first];
    array[first] = array[last];
    array[last] = temp;
    ++first;
    --last;
  }
}
... without changing main.
what is this supposed to mean?
anyways, using std::reverse
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = {2,8,17,3,5,16};
    size_t arr_size = sizeof(arr)/sizeof(arr[0]);
    std::reverse(arr, arr + arr_size);
    for (size_t i = 0; i < arr_size; ++i)std::cout << arr[i] << "\n";
}
... without changing main.
what is this supposed to mean?

@gunnerfunner,
my guess is that main was provided and coding the backward function is the assignment/exercise.
oh i see what you mean, thomas, thanks. so this might still qualify?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm> // not changing main() ;)
using namespace std;//leaving this in so that main() is untouched but ...
//http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
void backwards(int [], int);

int main()
{
  int arr[] = {2,8,17,3,5,16}; int i;

  backwards(arr,6);

  for (i = 0; i< 6; i++)
    cout<<arr[i]<<endl;

//system("pause");
return 0;
}

void backwards(int  array[], int number)
{
  std::reverse(array, array + number);
}
1
2
3
4
void backwards(int  array[], int number)
{
  std::reverse(array, array + number);
}



Explain to me how this helps the OP understand how an array can be reversed.
Last edited on
anyways, using std::reverse


Arslan - explain to me how difficult it is to google std::reverse()
Arslan7041 wrote:
how this helps the OP understand how an array can be reversed.

to be fair, that is exactly how an array is reversed. It probably doesn't help the OP with their homework, but it helps someone stumbling on this question in a web search a year later.
Last edited on
to be fair, that is exactly how an array is reversed. It probably doesn't help the OP with their homework, but it helps someone stumbling on this question in a web search a year later.


Not at all. Anyone googling how to reverse an array is likely looking for the actual code that does the work, not the interface.

Replying to an obvious beginner with a built-in function does no help to anyone, not even a person stumbling across the question a year later.

Last edited on
Arslan - explain to me how difficult it is to google std::reverse()


Explain to me how difficult it is to actually answer the OP's question? You clicked on this thread and wasted your own time and energy to give an unhelpful reply, when you could have used that same time and energy to actually answer the question.

If you wanted him to google it, you would've been better off saying "google it" instead of giving that ridiculous reply.

Next time someone asks you how quicksort works, I suppose you can simply answer with std::sort and leave it at that.
Arslan - thank you for your valuable advice.
closed account (48T7M4Gy)
@OP Here's another way as a result of slight embellishment. Your difficulty was the halfway mark not being the loop termination.

I decided to generalize the size of the array as part of the debugging and thought I may as well leave it in, subject as always to approval from the C++ police.

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
#include <iostream>
using namespace std;
void backwards(int [], int);

int main()
{
    int arr[] = {2,8,17,3,5,16};
    
    int size = sizeof(arr)/sizeof(int);
    
    backwards(arr, size);
    
    for (int i = 0; i< size; i++)
        cout<<arr[i]<<endl;
    
    //system("pause");
    return 0;
}

void backwards(int  array[], int number)
{
    /*Pre: array[] - array of integers
     number - number of elements in the array that have values
     Post: Nothing
     Purpose: Reverse the order of the elements of an array */
    
    int temp = 0;
    
    for(int i = 0; i < number/2; i++)
    {
        temp = array[i];
        array[i] = array[number - 1 - i];
        
        array[number - 1 - i] = temp;
    }
    
    return;
    
}


int arr[] = {2,8,17,3,5,16};
16
5
3
17
8
2
Program ended with exit code: 0


int arr[] = {2,8,17,99,3,5,16};
16
5
3
99
17
8
2
Program ended with exit code: 0
Topic archived. No new replies allowed.