0

I have another queation, why this program won't sort my numbers from the smallest to the biggest??

#include<stdio.h>

main()
{
float temp, num1, num2, num3, num4;

printf("Input 4 numbers:\n");
scanf("%f%f%f%f", &num1, &num2, &num3, &num4);

if(num1>num2)
    {
        temp=num1;
        num1=num2;
        num2=temp;
    }
if(num2>num3)
    {
        temp=num2;
        num2=num3;
        num3=temp;
    }
if(num3>num4)
    {
        temp=num3;
        num3=num4;
        num4=temp;
    }
if(num1>num2)
    {
        temp=num1;
        num1=num2;
        num2=temp;
    }

printf("The sorted list of the 4 numbers is:\n%f\t%f\t%f\t%f\n", num1, num2, num3, num4);
}

Edited by pyTony: fixed formating

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by zyruz
0

if you starte out whit this numbers:
num1 = 4
num2 = 3
num3 = 2
num4 = 1
and if you look at your code you will see the numbers change like this:
num1 = 3
num2 = 4
num3 = 2
num4 = 1

num1 = 3
num2 = 2
num3 = 4
num4 = 1

num1 = 3
num2 = 2
num3 = 1
num4 = 4

num1 = 2
num2 = 3
num3 = 1
num4 = 4

you shuld use a loop, else you need to make more if testings, and it wold look bad!(at least I think that)

while(num1 >= num2 || num2 >= num3 || num3 >= num4)
{
//put some sorthing here
}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.