Newbie need help!

What kind of code should i use for sorting numbers in both ascending and descending order? I don't know how to use bubble sorting either, is there another easy way to sort this out?
ok! no need anymore i figure it out...one way or another.
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

int main()
{
srand(time(0));
int third=rand()%10;
int first, second;
int max, mid, min;

printf("Type ur first number that is lesser than 10: ", first);
scanf("%d", &first);

printf("Type your second number that is lesser than 10: ", second);
scanf("%d", &second);

printf("Your random number is: %d \n", third);

{
if (first <= second && second <= third)
{
max=first;
mid=second;
min=third;

}

if (first <= third && third <= second)
{
max=first;
mid=third;
min=second;
}

if (second <= first && first <= third)
{
max=second;
mid=first;
min=third;
}

if (second <= third && third <= first)
{
max=second;
mid=third;
min=first;
}

if (third <= first && first <= second)
{
max=third;
mid=first;
min=second;
}

if (third <= second && second <= first)
{
max=third;
mid=second;
min=first;
}
}
printf("The ascending order is: %d, %d, %d \n", min, mid, max);
printf("The descending order is: %d, %d, %d", max, mid, min);


return 0;
}

looks a bit childish and messy but this is the best that i can do now.
If anyone have some comment or opinion or suggestion please be my guest.
I would love to hear more to improve my skills.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdlib> // <stdlib.h>
#include <ctime> // <time.h>
#include <iostream> // <stdio.h>

int main()
{
    std::srand( std::time( 0 ) );

    std::cout << "Type ur first number that is lesser than 10: " ;
    int first ;
    std::cin >> first ;

    std::cout << "Type ur first second that is lesser than 10: " ;
    int second ;
    std::cin >> second ;

    int third = std::rand() % 10;
    std::cout << "Your random number is: " << third << '\n' ;

    if( first > second ) { int temp = first ; first = second ; second = temp ; } // now second >= first
    if( second > third ) { int temp = second ; second = third ; third = temp ; } // now third >= both second and first
    if( first > second ) { int temp = first ; first = second ; second = temp ; } // now first <= second <= third

    std::cout << "The ascending order is: " << first << ' ' << second << ' ' << third << '\n' ;
    std::cout << "The descending order is: " << third << ' ' << second << ' ' << first << '\n' ;
}


Using std::swap()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>

int main()
{
    std::srand( std::time( 0 ) );

    std::cout << "Type ur first number that is lesser than 10: " ;
    int first ;
    std::cin >> first ;

    std::cout << "Type ur first second that is lesser than 10: " ;
    int second ;
    std::cin >> second ;

    int third = std::rand() % 10;
    std::cout << "Your random number is: " << third << '\n' ;

    if( first > second ) std::swap(first,second) ; // now second >= first
    if( second > third ) std::swap(second,third) ; // now third >= both second and first
    if( first > second ) std::swap(first,second) ;  // now first <= second <= third

    std::cout << "The ascending order is: " << first << ' ' << second << ' ' << third << '\n' ;
    std::cout << "The descending order is: " << third << ' ' << second << ' ' << first << '\n' ;
}
Thanks! i think i can learn a lot from these.
Topic archived. No new replies allowed.