Making sorting function

I have to create function with sorts array. I have to give array, lenght of array and direction from low to higher and vica versa. I have to make sorting function by myself, but how can I do that since function cant return array?

Also when I make a new function can I write shortint myfunction(int blah){}?
Will it make any difference?
See 'Arrays as parameters' in http://www.cplusplus.com/doc/tutorial/arrays/
I dont need to print it, but sort it and give it back to main function. =]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <algorithm>
#include <functional>
#include <iostream>

void array_sort(int* arr, int length, bool descending = false)
{
    if(descending)
        std::sort(arr, arr + length, std::greater<int>());
    else
        std::sort(arr, arr + length);
}

int main()
{
    const int size = 5;
    int array[size] = {1, 5, 3, 0, -1};
    array_sort(array, size);
    for(int i = 0; i < size; ++i)
        std::cout << array[i] << ' ';
    std::cout << std::endl;
    array_sort(array, size, true);
    for(int i = 0; i < size; ++i)
        std::cout << array[i] << ' ';
}
http://ideone.com/l2lyXZ
Townsheriff wrote:
I dont need to print it, but sort it and give it back to main function

I said nothing about printing.
Tutorial wrote:
At some point, we may need to pass an array to a function as a parameter. In C++, it is not possible to pass the entire block of memory represented by an array represents to a function directly as an argument. But what can be passed instead is its address. In practice, this has almost the same effect, and it is a much faster and more efficient operation.

If a function gets the address of the memory that for the caller is an array, then the function can modify that array of the caller.

Easy to test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void foo( int a[] )
{
  a[0] = 7;
}

void bar( int * b )
{
  b[0] = 42;
}

int main()
{
  int array[2];
  array[0] = 3;
  foo( array );
  std::cout << array[0] << '\n';
  bar( array );
  std::cout << array[0] << '\n';
  return 0;
}
Thanks you guys.
Topic archived. No new replies allowed.