c - Arranging 3 numbers in ascending order - function not working as expected -


as part of bigger problem have sort 3 numbers in ascending order. simple enough task, reason i'm not getting expected result. using arrays not allowed. please let me know if can tell problem is. i've been racking brains , can't see :< thank you!

#include <stdio.h>  void order(int a, int b);  int main(void) {            int x, y, z;      scanf("%d %d %d", &x, &y, &z);      order(x, y);     order(x, z);     order(y, z);      printf("%d %d %d", x, y, z);      return 0; }  void order(int a, int b) {     int inter;      if(a > b)        {          inter = a;         = b;         b = inter;     } } 

you passing numbers x, y, , z value. swap in order() not reflect in main().

instead pass address of variables , modify them original variables in main() reflect changes did in order():

#include <stdio.h>  void order(int *a, int *b);  int main(void) {            int x, y, z;      scanf("%d %d %d", &x, &y, &z);      order(&x, &y);     order(&x, &z);     order(&y, &z);      printf("%d %d %d", x, y, z);      return 0; }  void order(int *a, int *b) {     int inter;      if(*a > *b)        {          inter = *a;         *a = *b;         *b = inter;     } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -