Sorting integers from lowest to highest

Hello,
I have to short the numbers entered in order from lowest to highest. When first did it I just had cout statements under each if putting them in order. I am now being told I can only have the one cout statement at the end. SOOO I tried declaring another int but it only prints out the first number. For example,if you enter 3, 2, 4 x=3 y=2 z=4, sorted it should read 2,3,4 but it only reads 4. Any suggestions?

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

/**
 * This program uses if statments to sort 3 integers entered in by a user in 
 * numerical order from lowest to highest. Then it will find the sum of all 
 * the integers entered and the average. It will loop 9 times
 * 
 * @creator
 */

int main(int, char**) {
   
   float sum=0; 
   float average=0;
    
   for(int i= 0; i<9; ++i) {
   
   int x,y,z;
   int order;

   cout<<"Enter 3 integers: ";                              
   cin>>x>>y>>z;
   
       if ((x>y && y>z))
              order = x,y,z;
       if ((x>z) && (z>y))
              order = x,z,y;
       if ((y>x) && (x>z))
              order = y,x,z;
       if ((y>z) && (z>x))
              order = y,z,x;
       if ((z>x) && (x>y))
              order = z,x,y;
       if ((z>y) && (y>x))
              order = z,y,x;
         
       cout<<x<<" "<<y<<" "<<z<<" sorted is "<<order<<endl;    
            
   cout<<endl;
   
   sum+= x+y+z;
   
}
   average = sum/27;
   
   cout <<"The sum of all the integers is: " <<sum<<endl;
   
   cout <<"The average of all the integers is: " <<average<<endl;
   
   cout<<endl; 
    
   (void) system("pause");      
   return EXIT_SUCCESS;
}
The 'order' is an integer. One number. It can have only one number in it.

The comma-operator evaluates its operands, but returns only one. Chaining two comma operators still returns only one of the operands.

Most sorting algorithms do use a swap operation.
See http://www.cplusplus.com/reference/algorithm/swap/
I may be too late to help you, my friend.

However, I think what you might want is a array of integers for your order variable.

Array C++ Tutorials:
https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
http://www.cplusplus.com/doc/tutorial/arrays/

Another possible solution is a vector of integers.

Vector C++ Tutorial:
http://www.cplusplus.com/reference/vector/vector/

I hope this helps,

~Hirokachi
Last edited on
I probably should have mentioned the assignment does say "I can't use any type of array".
Ah, so maybe what you should do then is change the order to something like:

1
2
3
int seeFirst;
int seeSecond;
int seeLast;


It seemed like you wanted an array or something because you only had one integer type variable.

Maybe I should have mentioned this previously, but it's not a good idea/practice to use the system("pause") command, at all, because if you were to have threads in your code you invite the extremely high possibility to have unexpected results as output for your program.

Hopefully this helps,

~Hirokachi
Last edited on
closed account (LA48b7Xj)
Here is the sorting numbers part

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int x, y, z;
    cin >> x >> y >> z;

    if (x > y)
        swap(x, y);

    if (x > z)
        swap(x, z);

    if (y > z)
        swap(y, z);

    cout << x << ' ' << y << ' ' << z << ' ' << endl;
}

Thank you everyone. Its at these moments that I hate this course. Something so simple, once its explained, has taken me over 16 hours of work to figure out. We haven't even discussed a swap command. Unfortunately, the system("pause") part of the code is a required template from the teacher so I can't change anything other than the description and the code between the main and void section.

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

/**
 * This program uses if statments to sort 3 integers entered in by a user in 
 * numerical order from lowest to highest. Then it will find the sum of all 
 * the integers entered and the average. It will loop 9 times
 * 
 * @creator
 */

int main(int, char**) {       ///other than the description,cant start editing until after this line
   
   float sum=0; 
   float average=0;
    
   for(int i= 0; i<9; ++i) {
   
   int x,y,z;

   cout<<"Enter 3 integers: ";                              
   cin>>x>>y>>z;
   
      if (x > y)
         swap(x, y);

      if (x > z)
         swap(x, z);

      if (y > z)
         swap(y, z);
       
       cout<<x<<" "<<y<<" "<<z<<" sorted is "<<x<<" "<<y<<" "<<z<<endl;    
            
   cout<<endl;
   
   sum+= x+y+z;
   
}
   average = sum/27;
   
   cout <<"The sum of all the integers is: " <<sum<<endl;
   
   cout <<"The average of all the integers is: " <<average<<endl;
   
   cout<<endl; 
                                                                               /////until here, rest is required template
 
  (void) system("pause");      
   return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.