0

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.

6
  • 8
    Typo? if (maxmin==1) should be if (c==1). Commented Nov 25, 2019 at 16:15
  • 1
    You have a function named maxmin, you need to change the if condition to if ( maxmin(a,b) == 1 ) Commented Nov 25, 2019 at 16:17
  • @SPlatten OP already called the function and is using intermediate variables and this is legitimate, as in the function too. Commented Nov 25, 2019 at 16:18
  • @SPlatten you mean like this? if (maxmin(a,b)==1) { printf("%d is maximum,%d is minimum",a,b); } else printf("%d is maximum,%d is minimum",b,a); return 0; Commented Nov 25, 2019 at 16:18
  • @Yunnosch OP already called the function with 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 in main() and in maxmin(), and the compiler will probably optimise them out. Commented Nov 25, 2019 at 16:30

2 Answers 2

1

if (maxmin==1)

change toif (c==1)

your problem is solve. Have a Good day

Sign up to request clarification or add additional context in comments.

Comments

0

You should be checking if(c == 1), not if(maxmin == 1).

Your function can also be made shorter:

int maxmin(int x, int y)
{
    if (x>y)
    {
        return 1;
    }
    else
    {
       return 0;
    }
}

Also, I think your scanf is missing a comma between the two %d's.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.