How can i return an array from function in c++..
I have this code:

#include <iostream>
#include <string>
using namespace std;
    int ordincreasente(int num[], int len){
        int curMax,curMin,ordinato[len];
        for(int i=0; i<(len-1); i++){
            for(int j=i+1; j<len; j++){
                if(num[i]<num[j]){
                    swap(num[i], num[j]);
                }
            }//end second loop
        }//end first loop

        for(int i = 0; i<len;i++){
            cout<<num[i]<<" ";
        }
        cout<<endl;
    }
int main() {
   ordincreasente(arr,b);
    cout<<endl;
    return 0;
}

Hi phoenix254 ,

You are not returning an array using int ordincreasente (..) like you are doing. Am sure you know that would only return an integer not an aggregate which array is.
More so, in your main function on line 20 ordincreasente(arr,b);. I suppose, your array is called arr, and the size of it you represented with b. But where are they? What type are they? Of course, you will presume one should think they are numbers? please be specific as much as possible.
Of course, you can use a pointer to return an array from a function. The fact is, since array are passed by reference not by value, basically you really don't need return an array like so:

// Example program
#include <iostream>

void printArray ( int array[], int size ) {
    for (int i = 0; i < size; i++)
        std::cout << array[i] << " ";
}

void sortArray (int array[], int size) {
    for (int i = 0; i < size; i++)
        for ( int j = i; j < size; j++)
            swap(array[i], array[j]); // not shown here     
}

int main(){

    int array[] = {5, 9, 3, 56, 0};
    const int size = sizeof array/sizeof(int);

    std::cout << "Before Sorting Array: \n";
    printArray(array, size);  // 5,9,3,56,0
    // sorting
    sortArray(array, size);
    std::cout << "\nAfter Sorting Array: \n";
    printArray(array, size);  // 0,3,5,9,56

    return 0;
}

Note that I did not show the swap function.

ty for answering me sir, sorry about that i wrote something wrong in first snippet, the main () is like this

int main() {
int i,b=5;
 int arr[b];
   ordincreasente(arr,b);
    cout<<endl;
    return 0;
}

how do i associate sort(ed) array in new array in main ().

  int main() {
    int i,b=5;
     int arr[b];// this array has some value that are inserted with keyboards or whatever.
       ordincreasente(arr,b);// i send arr array to fucntion to soort
        cout<<endl;

       //here i hve new array i want get value of arr array in new arry
       int arr2[b];
       arr2 = arr; //????

    return 0;
}

hi,

since the two array has the same "size", in other word will have the number of elements not necessary the values, you can use the "old fashion" for loop.

array1 = array2 will not work. Find out why.

Lastly, please use meaningful variable names. It will help you on the long run.

how i do with for loop ? i don t have any idea..

how i do with for loop ? i don t have any idea..

How did you come about the codes in your first post, if you don't know what a for loop is all about?

Edit: Is it that you don't know what a for loop is? Or you don't know how to do it with a for loop? Which?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.