0

i want input randomly 3 number and then Ascending order output.
big one->small one
you know what i mean?

#include <stdio.h>
int main(void) {

    int a, b, c;
    int min, mid, max;


    scanf("%d %d %d", &a, &b, &c);

    if (a > b) {
        max = a;
        min = b;
    }

    else {
        max = b;
        min = a;
    }

    mid = c;

    if (c > max) {
        max = c;
        mid = a > b ? a : b;
        min = a < b ? a : b;
    }

    else if (c < min) {

        min = c;
        mid = a < b ? : a : b;
            max = a > b ? : a : b;

    }

    printf("1:%d 2:%d 3:%d", max, mid, min);



    return 0;

}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
강민규
  • 23
  • 2
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 04 '19 at 11:52
  • 5
    And please tell us what's wrong with the program. Do you get build errors? Crashes when running? Wrong results? If you get build errors, please copy-paste them into the question. For wrong results, please tell us the input you give, together with the actual and expected output. – Some programmer dude Sep 04 '19 at 11:52

1 Answers1

1

The code is illogical and tangled up. The simplest way of sorting 3 numbers is to use swap three times:

if (a > c) swap(&a, &c);
if (a > b) swap(&a, &b);
if (b > c) swap(&b, &c);

Introducing new variables is not necessary, and I'm not convinced they are all set on every control path so that could produce undefined results. To build swap, see Is there a built-in way to swap two variables in C, or use this one:

void swap(int* a, int* b)
{
   int t = *b;
   *b = *a;
   *a = t;
}

Finally, don't forget to check the return value of scanf; it should be 3 in your case.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483