I was solving a code of maximum and minimum using functions. I wrote the code like this:
#include <stdio.h>
int maxmin(int x, int y);
int main () {
int a, b, c;
scanf("%d%d", &a, &b);
c = maxmin(a, b);
if (maxmin == 1) {
printf("%d is maximum,%d is minimum", a, b);
}
else
printf("%d is maximum,%d is minimum", b, a);
return 0;
}
int maxmin(int x, int y) {
int XisMax = 0;
if (x > y) {
XisMax=1;
}
else {
XisMax=0;
}
return XisMax;
}
So my output shows this results:
Input:9,10;
10 is maximum,9 is minimum
Input:10,9;
9 is maximum,10 is minimum
What is the mistake here? What should I do?
PS:I have an exam on functions so solutions using functions will be helpful.
if (maxmin==1)should beif (c==1).c= maxmin(a,b);and does not need to call it again as suggested. The suggestion might be better practice, but is not the actual error. There is no harm in using intermediate variables, as OP's code does both inmain()and inmaxmin(), and the compiler will probably optimise them out.