Looking For Help With Reversing the Order of an Array

In the below code I am attempting to print the reverse order of array1 by first storing the reversed contents into array2 and printing array2. If it was simply printing the reversed contents of array1 its pretty straightforward but I'm having trouble saving the reversed order into a seperate array. This is what i have right now and I'm not sure if I'm on the right track. Some guidance would be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;

int main()
{
    const int ARRAY_SIZE = 7;

    int array1[ARRAY_SIZE] = {4, 7, 2, 8, 1, 3, 0};
    int array2[ARRAY_SIZE] = {0, 0, 0, 0, 0, 0, 0};

    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        for(int k = 6; k >= 0; k--)
             array2[i] = array1[k];
    }

    for(int x = 0; x < ARRAY_SIZE; x++)
        cout << array2[x] << endl;
    cout << endl;

    return 0;
}
closed account (SECMoG1T)
you are having trouble with your loops

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
    const int ARRAY_SIZE = 7;

    int array1[ARRAY_SIZE] = {4, 7, 2, 8, 1, 3, 0};
    int array2[ARRAY_SIZE] = {0, 0, 0, 0, 0, 0, 0};

    for(int frwd_indx = 0, rvrs_indx = (ARRAY_SIZE-1); frwd_indx < ARRAY_SIZE; frwd_indx++,rvrs_indx--)
    {
        array2[frwd_indx] = array1[rvrs_indx];
    }

    for(int x = 0; x < ARRAY_SIZE; x++)
        cout << array2[x] << endl;
    cout << endl;

    return 0;
}
Last edited on
Ah I see. I haven't been introduced to having multiple counter variables in a single for loop so I didn't even know that was possible. Very helpful, thanks!
closed account (SECMoG1T)
you can also use a while loop, it makes everything much cleaner.
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
#include <iostream>
using namespace std;

int main()
{
    const int ARRAY_SIZE = 7;

    int array1[ARRAY_SIZE] = {4, 7, 2, 8, 1, 3, 0};
    int array2[ARRAY_SIZE] = {0, 0, 0, 0, 0, 0, 0};

    int frwd_indx =0, rvrs_indx = (ARRAY_SIZE-1);

    while(frwd_indx < ARRAY_SIZE)
    {
        array2[frwd_indx] = array1[rvrs_indx];

        ++frwd_indx;
        --rvrs_indx;
    }

    for(int x = 0; x < ARRAY_SIZE; x++)
        cout << array2[x] << endl;
    cout << endl;

    return 0;
}


 having multiple counter variables


you can use as many as you want but that becomes quite messy.
closed account (E0p9LyTq)
If you are not required to write your own reverse function, <algorithm> has std::reverse and std::reverse_copy.

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
#include <iostream>
#include <algorithm>

int main()
{
   int arr1[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
   int arr2[9] = { 0 };

   for (auto itr : arr1)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   for (auto itr : arr2)
   {
      std::cout << itr << ' ';
   }
   std::cout << "\n\n";

   std::reverse_copy(arr1, arr1 + 9, std::begin(arr2));

   for (auto itr : arr1)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   for (auto itr : arr2)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}

1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
Topic archived. No new replies allowed.