[ISSUE] Sorting [Smallest to Largest]

Good afternoon,

I have written a program in C++ that allows you to enter 3 numbers. The numbers can be entered one after the other, or all at ounce (as long as each value has a space between it). using a while(cin>>current). The numbers are then sorted with arrays and loops to give and the user is shown the entire process.

My issue is something I can't understand, when I enter 3 values such
as
1
2
3
4
5
6
7
Enter three integer values (followed by 'enter'): 16 10 11
    // I get this in response 
val1: 16 val2: 0 val3: 0 
val1: 10 val2: 16 val3: 0 
val1: 10 val2: 16 val3: 11
3 10 2 
val1: 10 val2: 16 val3: 3  


of course this isn't working properly, i want it to show me each instance of the array, and its suppose to fill as 10 11 16. and then show 2 3 10 for the next instance of letters.
Example for everyone
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
 
// Bradley Latreille dd/mm/yy
// PPP Template for excersises in the early chapters 

#include "std_lib_facilities.h" // needed up to Chapter 12 

int main() {
    try
    {
        cout << "Enter three integer values (followed by 'enter'): "; 
        int previous; 
        int values[3] = {0,0,0}; // our 3 values 
        int i = 0; // iteration count 
        int current;
        // go into loop each iteration is 1 of the 3 values 
        while(cin>>current)
        {
            if(previous>current){
                values[i-1] = current;
                values[i] = previous; 
            }else{
                values[i] = current;
            }
            
            cout << "val1: "
                 << values[0] << " val2: " 
                 << values[1] << " val3: " 
                 << values[2] << '\n';
             
            previous=current; 
            i++; 
        }
        
        keep_window_open(); // for some windows(tm) setups 
    }
    catch(runtime_error e) { // this code is to produce error messages
    // *Explained in chapter 5* 
        cout << e.what() << '\n'; 
        keep_window_open("~"); // for some Windows(tm) setups 
    }
}


Any help would be greatly appreciated :)
For begginers who can't get my code to run just run this
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
 
#include <iostream> 

using namespace std; 

int main() 
{
        cout << "Enter three integer values (followed by 'enter'): "; 
        int previous; 
        int values[3] = {0,0,0}; // our 3 values 
        int i = 0; // iteration count 
        int current;
        // go into loop each iteration is 1 of the 3 values 
        while(cin>>current)
        {
            if(previous>current){
                values[i-1] = current;
                values[i] = previous; 
            }else{
                values[i] = current;
            }
            
            cout << "val1: "
                 << values[0] << " val2: " 
                 << values[1] << " val3: " 
                 << values[2] << '\n';
             
            previous=current; 
            i++; 
        }
}
Last edited on
It occurred to me that this method is clearly to advanced for my knowledge so I used the approach I was suppose to use and do all the checking individually.

Solution:
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
// Bradley Latreille dd/mm/yy
// PPP Template for excersises in the early chapters 

#include "std_lib_facilities.h" // needed up to Chapter 12 

int main() {
    try
    {
        // value objects
        int val1 = 0; 
        int val2 = 0; 
        int val3 = 0; 
        // prompt user
        cout << "Enter three integer values (followed by 'enter'): ";
        // get input and check for errors
        cin >> val1 >> val2 >> val3; 
        if (!cin) error("bad input"); 
        // output values 
        cout << "[Inputs] Val1: " << val1 << " Val2: " << val2 
             << " Val3: " << val3 << '\n'; 
        // do checking 
        int smallest = 0; 
        int middle = 0; 
        int largest = 0; 
        if (val1<=val2 && val1<=val3) {	// && means and
		    smallest = val1;
		    if (val2<=val3) {
			    middle = val2;
			    largest = val3;
		    }
		    else {
			    middle = val3;
			    largest = val2;
		    }
	    }
	    else if (val2<=val1 && val2<=val3) {
		    smallest = val2;
		    if (val1<=val3)  {
			    middle = val1;
			    largest = val3;
		    }
		    else {
			    middle = val3;
			    largest = val1;
		    }
	    }
	    else {	// since neither val1 nor val2 was smaller than val3, val3 must be the smallest
		    smallest = val3;
		    if (val1<=val2) {
			    middle = val1;
			    largest = val2;
		    }
		    else {
			    middle = val2;
			    largest = val1;
		    }
	    }
        
	    cout << "values sorted : " << smallest << ", " << middle << ", " << largest <<'\n'; 
    }
    catch(runtime_error e) { // this code is to produce error messages
    // *Explained in chapter 5* 
        cout << e.what() << '\n'; 
        keep_window_open("~"); // for some Windows(tm) setups 
    }
}
For just three values, we do not really need any loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm> // for std::swap
using namespace std ;

int main()
{
    cout << "Enter three integer values (followed by 'enter'): ";
    int values[3] = {};
    cin >> values[0] >> values[1] >> values[2] ;

    // bring the smallest value to the front
    if( values[0] > values[1] ) swap( values[0], values[1] ) ;
    if( values[0] > values[2] ) swap( values[0], values[2] ) ;

    // bring the next smallest value to the position one
    if( values[1] > values[2] ) swap( values[1], values[2] ) ;

    // print out the sorted values
    cout << values[0] << ' ' << values[1] << ' ' << values[2] << '\n' ;
}


Now, the same idea as above, with loops (we need not restrict ourselves to just three values):
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
#include <iostream>
#include <algorithm> // for std::swap
using namespace std ;

int main()
{
    const int NVALUES = 10 ;

    cout << "Enter " << NVALUES << " integer values (followed by 'enter'): ";
    int values[NVALUES] = {};
    for( int i = 0 ; i < NVALUES ; ++i ) cin >> values[i] ;

    // bring the smallest value to the front,
    // the next smallest value to the position one and so on
    for( int i = 0 ; i < NVALUES ; ++i ) // repeat for each position in the array
    {
        // for each position j which is after position i
        for( int j = i+1 ; j < NVALUES ; ++j ) // invariant: i < j
        {
            // if the value that is earlier in the array
            // is greater than the value which is at a later position
            if( values[i] > values[j] ) swap( values[i], values[j] ) ; // swap them
        }

    }

    // print out the sorted values. we will use a rage-based loop for variety
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : values ) cout << v << ' ' ;
    cout << '\n' ;
}
Topic archived. No new replies allowed.