Question:
question on function with parameter passing in C program?
utakata
2008-10-27 12:23:29 UTC
function 1: allow user to enter 4 float values.
function 2: print the 4 float values.
function 3: use the return statement to return the smallest of the 4 float values. print smallest value in the main.
function 4: swap the first and last of 4 float values. upon returning to the main, use your print function to print the float values after the swap has been completed. eg, if 4 float values are initially 2.2, 3.3, 4.4, 5.5, then the values printed after returning to the main should be 5.5, 4.4, 3.3, 2.2.

My question is where do I put return statement and swap function. I really don't know how to use return statement to find smallest value. I have done so far...can someone tell me how to add swap & return function? Thanks!

#include
void load (float *a, float *b, float *c, float *d)
{
printf("enter 4 floats ");
scanf("%f%f%f%f", &(*a), &(*b), &(*c), &(*d));
}

void print (float a, float b, float c, float d)
{
printf("The 4 values are %f %f %f %f\n\n",a,b,c,d)
}

void main()
{
float a, b, c, d;
load(&a,&b,&c,&d)
print(a,b,c,d)
}
Three answers:
Apologetic Avenger
2008-10-27 12:37:30 UTC
For 3, you need a value returning function...



function smallest (float a, float b, float c, float d)

{

if a
else

if b


...comparing all values to see which is smallest.

}



Whatever is after the return, is what will be sent back to your main program.



So in main(), you might have something like this:



float smallFloat

smallFloat = smallest (a,b,c,d)



--------------------



As for the "swap", I don't know what you've learned so far yet in the class, but does it based on the order the numbers are entered in?



Then you'd just have a print statement outputting d, b, c, a...right?



Otherwise, make sure there are no other parameters that require you to "sort".
Chris C
2008-10-27 13:23:44 UTC
The return statement would be from another function you have to create, such as "float smallest(float *a, float *b, float *c, float *d)".



I wrote the function for you here, and the "swap()" function as well, if you don't have a library that contains it (which would be odd).

You merely have to call it like this: printf("Smallest value entered is: %f\n", smallest(&a, &b, &c, &d));



float smallest(float *a, float *b, float *c, float *d)

{

int i;



/* Loop through, because the maximum number of moves would be 2^4 */

/* Keep in mind this the the worst algorithm to use, but then again, so is */

/* idea of passing a specific number of floats, unless you would NEVER */

/* anticipate this routine to ever use more than 4 floats */

/* An array would be THE best answer for this problem! */

for (i = 0; i < 16; i++)

{

if (*b < *a)

swap(a, b);



if (*c < *b)

swap(b, c);



if (*d < *c)

swap(c, d);

}



/* The lowest value is now in the "a" variable */

return *a; /* Return the smallest value */

}



void swap (float *x, float *y)

{

float n = *x;

*x = *y;

*y = n;

}
Odwin Oddball
2008-10-27 12:33:11 UTC
The problem is worded poorly. What he means is that function 3 should find the smallest of the 4 values, and then return it.



as in



float findSmallest(float a, float b, float c, float d)

{

compare them and find smallest



return smallest;

}



The swap function should take the floats as pointers, as in your load function, then sort them. Then you call your print function to print out the now sorted floats.



This whole operation would be a lot easier if you were using an array instead of 4 separate floats.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...