First post on StackOverflow. I'm supposed to create a function:
int sumsort(int *a, int *b, int *c)
This function should arrange the 3 values in the memory locations pointed to by a, b, and c in ascending order and also return the sum of the contents of the memory locations a, b, and c.
Here's my function:
int sumsort(int *a, int *b, int *c) {
int sum = *a + *b + *c;
int sorted[] = {*a, *b, *c};
for (int i = 0; i <= 2; i++) {
if (sorted[0] > sorted[1])
{
int temp = sorted[1];
sorted[1] = sorted[0];
sorted[0] = temp;
} // end if
if (sorted[1] > sorted[2])
{
int temp2 = sorted[2];
sorted[2] = sorted[1];
sorted[1] = temp2;
} // end if
} // end for
return sum;
} // end sumsort function
How can I access the sorted[]
array in main? I need to print the 3 variables in ascending order but don't really see how I can do that since the sumsort
function has to return the sum and the actual sorting has to happen in the sumsort
function too.
I tried creating a new array variable in main and assigning it sorted[]
after I call the sumsort
function, but that doesn't work because it's out of scope?