I would like to return the minimum of three given numbers like this.
I don't know why it doesn't return anything
#include <stdio.h>
int minimum3(int un, int deux , int trois)
{
int minimum;
if (un<deux && un <trois)
minimum= un;
else if (deux<trois && deux<un)
minimum= deux;
else if (trois<deux && trois<un )
minimum= trois;
return minimum;
}
int main(void) {
minimum3(4,88,8999);
return 0;
}
As others mentioned in the comments, you ignore the returned value, a quick fix for that would be:
int main(void) {
int min = minimum3(4,88,8999);
printf("min: %d\n",min);
return 0;
}
Despite that, your algorithm isn't that effective, as Jonathan mentioned, if 2 of the numbers you process are equal, there is no way to calculate the minimum. The better would,imo, would be to have another function that calculates the minimum of 2 numbers and then use that to compare to the third number. Much cleaner this way.
#include <stdio.h>
int min2(int a,int b)
{
return ((a <= b) ? a : b);
}
int min3(int a,int b,int c)
{
int mintmp = 0;
mintmp = min2(a,b);
return ((mintmp <= min2(mintmp,c)) ? mintmp : min2(mintmp,c));
}
int main(void)
{
printf("%d\n",min2(5,10));
printf("%d\n",min3(5,-1,1));
return 0;
}
You can of course replace the conditional expressions
with simpler if else
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.