Reverse and sort arrays

Hi,

How can I reverse my output array? In other words if user enters 1,8,3,10,5 I wan t them to appear as 5,10,3,8,1.

Finally, how can I sort them out highest to lowest?

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

using namespace std;

int main()
{
    const int SIZE = 5; // must be a constant variable
    int myArray[SIZE];

    cout << "Enter 5 Numbers: "<< endl;
    for(int i = 0; i < SIZE; i++)
    {
        cin >> myArray[i];
    }

    cout << "Your 5 numbers are: ";
    for(int i = 0; i < SIZE; i++)
    {
        cout << myArray[i] << " ";
    }

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


As always thanks a lot!
I already tried these functions, but I cannot make it work. I'm new to arrays.
Since you are new to arrays, I bet you tried to use reverse like this: reverse (array[0], array[n]), or something like that.
But, if you read the reference you'll see that both use "iterators", and basicly what those are, is pointers. So, use it like this:
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
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    const int SIZE = 5; // must be a constant variable
    int myArray[SIZE];

    cout << "Enter 5 Numbers: "<< endl;
    for(int i = 0; i < SIZE; i++)
    {
        cin >> myArray[i];
    }
    reverse(myArray, myArray+SIZE);
    cout << "Your 5 numbers are: ";
    for(int i = 0; i < SIZE; i++)
    {
        cout << myArray[i] << " ";
    }

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

As you can see, i used myArray without the bracket operator. That meant i used a pointer(aka "iterator") to the beggining, and one space after the end of the array.
Awesome! Thanks a lot

How can I sort them?
same thing, just replace the "reverse" with "sort"
Last edited on
Thanks a lot for your help!
Topic archived. No new replies allowed.