Saturday, May 22, 2010

How do i return 2 or more variables from a function in C?

example:


int funct(int *data[]){


/*find a few values, ie range min max*/


return ??;


}


int main(){


//need to find min max and range of data


min


max


range


}





do i use arrays pointers or what??





think that explains it, thanks

How do i return 2 or more variables from a function in C?
If you need to return more than a variable in method it is better to use an array. It is also a good programming practice to review your code when you think that a method will return more than a value and try to make the code that only return a single variable in order to leave the code a little more easier to understand.
Reply:Hi,Rich


sorry, i don`t sure i had left C language for long time put i hope this will helps you


}


int main(){


//need to find min max and range of data


return min


return max


return range


}
Reply:void funct(i* data[], int* min, int* max)


{


*min = //calculate min


*max = // calculate max


}





// calling the function


int min, max;


funct (data, %26amp;min, %26amp;max);























another way:





struct MINMAX


{


int min;


int max;


};








MINMAX funct(i* data[])


{


MINMAX result;


result.min = // calulate min;


result.max = // calculate max;


return result;


}





// calling the function


printf("The maximum is %d", funct(data).max);
Reply:I am not exactly sure about C but in C++ you would could use some sort of array and a binary search method in order to order the values from least to greatest or which ever way you wanted that to go and then all that you would need to do is to pull the first and last values in order to get the min and max. Actually this would also work with a pointer system as well so basically its what ever you are the most comfortable with.

kudzu

No comments:

Post a Comment