Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: How to arrange some numbers (http://www.velocityreviews.com/forums/t958715-re-how-to-arrange-some-numbers.html)

 Tim Rentsch 03-16-2013 04:03 AM

Re: How to arrange some numbers

> Hi, a simple question:
> i want to arrange some int numbers; i want to store them
> in an array and then print them on the screen.
>
> Look below:
>
> int numbers[10], arranged[10], i;
> puts("Insert 10 int numbers");
> for(i = 0; i <= 9; i ++, scanf("%d", &numbers[i]));
> ?
> for(i = 0; i <= 9; i ++, printf("/n%d", arranged[i]));
>
> I insert: 10 7 45 678 0 34 6 1 4 3
> I should see on the screen: 0 1 3 4 6 7 10 34 45 678
> Simple. Help me to replace question mark.

After correcting the few minor errors (already pointed out in
other posts), the "arranging" (aka sorting) may be done with
a 'while()' statement:

#include <stdio.h>

int
main(){
int numbers[ 10 ], arranged[ 10 ], i;
puts( "Insert 10 int numbers" );
for( i = 0; i <= 9; scanf( "%d", & numbers[i++] ) );

while(
i < 20 && (i += 1000 + 100*(i-10))
|| 1010 <= i && i < 1020 && (arranged[0] = numbers[i%10], i = 11+i%10)
|| 1110 <= i && arranged[i/100%10 -1] > numbers[i%10] && (
arranged[i/100%10] = arranged[i/100%10 -1], i -= 100
)
|| 1110 <= i && (arranged[i/100%10] = numbers[i%10], i = 11+i%10)
);

for( i = 0; i <= 9; printf( "\n%d", arranged[i++] ) );
return 0;
}

 All times are GMT. The time now is 07:16 AM.