Alright, I have written a program for class that allows the user to input three integers then sorts then and prints them in ascending order.
It works sometimes but sometimes it prints strange results. I'll post the code below and then beneath the code I'll give you a few test results. Any help would be greatly appreciated.
When I input 1, 2, 3... it prints out the correct order.Code:// This program takes integers from user input and // outputs them in ascending order. #include <stdio.h> main (void) { int a, b, c, min, mid, max; printf("Enter Number 1: "); scanf("%d", &a); printf("Enter Number 2: "); scanf("%d", &b); printf("Enter Number 3: "); scanf("%d", &c); if (a > b) { if (a > c) { max = a; if (b > c){ mid = b; min = c; } else mid = c; min = b; } else max = c; mid = a; min = b; } else if (c > b) { max = c; mid = b; min = a; } else if (b > c) { max = b; if (a > c) { mid = a; min = c; } else mid = c; min = a; } printf("%d \n%d \n%d \n", min, mid, max); return 0; }
When I input 3, 2, 1... it prints out 2, 3, 3.
When I input -1, -2, -3 it prints out -2, -1, -1
When I input -3, -2, -1 it prints out -3, -2, -1
When I input -3, -2, -4 it prints out -3, -3, -2
It seems that when a < b the results are wrong..
Does anyone have any ideas? I'm sure its obvious to a fresh pair of eyes, I've just been looking at it for hours and my brain is fried![]()



LinkBack URL
About LinkBacks



