a program that sorts five integers

May 8, 2013 at 3:56pm
im not very understand about this...when entered 3,2,4,1,5 , the result doesn't come out with 5,4,3,2,1... any wrong on this code?
Note: The integers are entered from the console and stored in the variables num1, num2, num3, num4 and num5, respectively. The program sorts the numbers in descending order, so that the biggest is first and the smallest number is the last. Print the numbers to the console in sorted order

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
using namespace std;
int main ()

{
   int a,b,c,d,e;
   cout<<"enter num 1 : ";
   cin>>a;
   cout<<"enter num 2 : ";
   cin>>b;
   cout<<"enter num 3 : ";
   cin>>c;
   cout<<"enter num 4 : ";
   cin>>d;
   cout<<"enter num 5 : ";
   cin>>e;
   if(a>b)
          cout<<a<<">"<<b<<endl;
          else
          cout<<b<<">"<<a<<endl;
   if(b>c)
          cout<<b<<">"<<c<<endl;
          else
          cout<<c<<">"<<d<<endl;
   if(c>d)
          cout<<c<<">"<<d<<endl;
          else
          cout<<d<<">"<<c<<endl;
   if(d>e)
          cout<<d<<">"<<e<<endl;
          else
          cout<<e<<">"<<d<<endl;
   if(e>a)
          cout<<e<<">"<<a<<endl;
          else
          cout<<a<<">"<<e<<endl;
 

 system ("pause");
 return 0;   
}


the output is like this,

enter num 1 : 3
enter num 2 : 2
enter num 3 : 4
enter num 4 : 1
enter num 5 : 5
3>2
4>1
4>1
5>1
5>3
May 8, 2013 at 4:03pm
If you're using a hardly-scalable amount of if statements to do something repetitive; then stop at once. The problem is that you don't compare correctly. Here's a version using a raw array and library functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main()
{
    enum {COUNT = 5};
    int array[COUNT];
    for (int i = 0; i < COUNT; ++i)
        std::cin >> array[i];
    std::sort(array, array + COUNT);
    std::reverse(array, array + COUNT);
    for (int i = 0; i < COUNT; ++i)
        std::cout << array[i];
    return 0;

}
May 8, 2013 at 4:10pm
I haven't learn about array yet... so i just try use the basic method to create this integer program. i need a solution using basic method...
May 8, 2013 at 4:51pm
The program sorts the numbers in descending order,

Well, it does not sort anything, but just compares pairs of values. You are missing the part when based on a comparison smaller numbers are swaped with larger ones. Take a look at this article. Probably the best explanation is provided by the animated gif :)
http://en.wikipedia.org/wiki/Bubble_sort
May 8, 2013 at 4:58pm
I haven't learn about array yet... so i just try use the basic method to create this integer program. i need a solution using basic method...

Basic method? It can't get any more basic than Bourgond's version.

You will use a data structure to store the numbers, then you sort them. Bourgond chose arrays as the data structure. I chose std::priority_queue (but clearly there's no reason to post my code). On a better day, I would have suggested you try to use std::list.

But printing five variables in a certain order is idiotic. Your school assignment is idiotic. It's hard because it's stupid, and it teaches you nothing about C++ and its features.
May 8, 2013 at 5:04pm
This looks like it works.


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <cstdio>


int main ()
{
    using std::cout;
    using std::cin;

    int a(0), b(0), c(0), d(0), e(0), highest(0), temporary(0);
    cout << "enter num 1 : ";
    cin >> a;
    cout << "enter num 2 : ";
    cin >> b;
    cout << "enter num 3 : ";
    cin >> c;
    cout << "enter num 4 : ";
    cin >> d;
    cout << "enter num 5 : ";
    cin >> e;

    highest = a > highest ? a : highest;
    highest = b > highest ? b : highest;
    highest = c > highest ? c : highest;
    highest = d > highest ? d : highest;
    highest = e > highest ? e : highest;

    temporary = a;
    a = highest;

    if (highest == b)
        b = temporary;
    if (highest == c)
        c = temporary;
    if (highest == d)
        d = temporary;
    if (highest == e)
        e = temporary;




    highest = 0;

    highest = b > highest ? b : highest;
    highest = c > highest ? c : highest;
    highest = d > highest ? d : highest;
    highest = e > highest ? e : highest;

    temporary = b;
    b = highest;

    if (highest == c)
        c = temporary;
    if (highest == d)
        d = temporary;
    if (highest == e)
        e = temporary;



    highest = 0;

    highest = c > highest ? c : highest;
    highest = d > highest ? d : highest;
    highest = e > highest ? e : highest;

    temporary = c;
    c = highest;

    if (highest == d)
        d = temporary;
    if (highest == e)
        e = temporary;




    highest = 0;

    highest = d > highest ? d : highest;
    highest = e > highest ? e : highest;

    temporary = d;
    d = highest;

    if (highest == e)
        e = temporary;




    highest = 0;

    highest = e > highest ? e : highest;

    temporary = d;
    e = highest;



    cout << a << " " << b << " " << c << " " << d << " " << e << "\n";

    std::getchar();
    return 0;
}
Topic archived. No new replies allowed.