pointers - C programming, if statements (large to small integer swap) -
i have been working on getting code put 3 numbers in ascending , descending order. however, code skips if statements , assumes numbers in order. first time using if statements in c , second day of learning pointers appreciated. thank you
#include<stdio.h> void swap(int *, int *, int *); int main(void){ printf("please enter first number sort: "); scanf("%d",&numberone); printf("please enter second number sort: "); scanf("%d",&numbertwo); printf("please enter third number sort: "); scanf("%d",&numberthree); //swap swap(&numberone, &numbertwo, &numberthree); //return results printf("the 3 numbers in descending order is: %d, %d, %d", numberone, numbertwo, numberthree); printf("the 3 numbers in ascending order is: %d, %d, %d", numberthree, numbertwo, numberone); } void swap(int *numberone, int *numbertwo, int *numberthree){ if (numberone>numbertwo){ if (numbertwo<numberthree){ int temp =*numbertwo; *numbertwo=*numberthree; *numberthree = temp; } // "312" else if (numbertwo>numberone){ if (numberone>numberthree){ int temp =*numberone; *numberone =*numbertwo; *numbertwo= temp; // "231" } else if(numberone<numberthree){ if(numbertwo>numberthree){ int temp =*numberone; *numberone =*numbertwo; *numbertwo =*numberthree; *numberthree = temp; // "132" } } } else if (numberthree > numberone){ if (numbertwo< numberone){ int temp =*numberthree; *numberthree =*numbertwo; *numbertwo =*numberone; *numberone = temp; // "213" } else { int temp = *numberthree; *numberthree = *numberone; *numberone = temp; // "123" } } } else{ printf("look @ these numbers in order..."); } }
when this: if (numbertwo>numberone)
, comparing pointers. , need compare values contained behind pointers. do: if (*numbertwo>*numberone)
Comments
Post a Comment