I’m working in c++ and can’t find what to put for the return value! I need it to get my arrays set up so my other functions can sort them.
The requirement is to pass three empty arrays to a method named PopulateArray() and get that arrays filled with populated values.
C++ does not support returning multiple values as well as returning multiple arrays. Thus this needs oto be achieved by passing a pointer of array's first element to the function.
When pointer is passed, all the values in the array will be populated in called function PopulateArrays and there is no need to return the arrays as the changes are actually made at the address of the array in memory.
Thus the required function will have below prototype:
void PopulateArrays(int *ascendingValues, int *descendingValues, int *RandomValues)
Void is the return type
PopulateArrays: The name of the function
Parameters: The function has three parameters which are pointers to the array
Please find below the function along with comments to understand the functionality. Please note that there is no need to add 3 for loops ( as used in shared function), all the arrays will be populated in single for loop.
void PopulateArrays(int *ascendingValues, int *descendingValues, int *RandomValues)
{
int x = 144;//set initial x to 144
int y= 1999;
int z = rand();
for (int i = 0; i < MAX; i++){//use one loop for all arrays
ascendingValues[i] = {x+2};//ascendingValues will have value x+2
x= ascendingValues[i];//set x to previous element value + 2
descendingValues[i] = {y-2};//descendingValues will have value y - 2
y = descendingValues[i]; //set y to previous element value -2
RandomValues[i] = {z+rand()}; //this is random value which will add initial value of z to this method
}//end for
}//end function
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 3 images