Second smalest number

Hi! Im new in c++ programing. Stil learning... :)
So, here is my problem: need to find the second smalest number of 10!
Here is my code and its not working!?!


#include <iostream>

using namespace std;

int main()
{
int low;
int low2;
int x;

for (int i=1; i<10; i++)
{
cout<<"input: \n";
cin>>x;
low2=low;
low=x;
if(x<low)
low=x;
if(x>low)
low2=x;
if((x<low2)&&(x>low))
low2=x;
}
cout<<"lowest is: "<<low<<endl;
cout<<"second smalest is:"<<low2;

return 0;
}
Last edited on
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
#include <iostream>

using namespace std;

int main()
{
    int low;
    int low2;
    int x;

    for (int i=1; i<10; i++) //Why do you run the loop 9 times?
    {
        cout<<"input: \n";
        cin>>x;
        low2=low; //What is the value of low in the firt time the loop is run?
        low=x;
        if(x<low)
            low=x;
        if(x>low)
            low2=x;
        if((x<low2)&&(x>low))
            low2=x;
    }
    cout<<"lowest is: "<<low<<endl;
    cout<<"second smalest is:"<<low2;

    return 0;
}
Last edited on
#include <iostream>

using namespace std;

int main()
{
int low;
int low2;
int x;

for (int i=0; i<10; i++) //it should take input for 10 numbers
{
cout<<"input: \n";
cin>>x;
low2=low; //it should be x? or ?
low=x;
if(x<low)
low=x;
if(x>low)
low2=x;
if((x<low2)&&(x>low))
low2=x;
}
cout<<"lowest is: "<<low<<endl;
cout<<"second smalest is:"<<low2;

return 0;
}
Last edited on
Why would it be x? You never assign any value to low before that statement, so the first time you use it it will be a garbage value...
What do you think should be the answer?
I'm realy out of idea.... :(
I'm asking the question to point out your design flaw. Surely you see that low has to have SOME value? And unless you set it yourself, there is a massive chance that is isn't going to be a value that you want.
If I give the list of numbers {3, 5, 10, 2} and ask for the second lowest number, what do you do to find it?

Now let look at the exceptional cases:
- If I give you the one element list {6} and ask for the second lowest number, what's your answer?
- What if I give you the empty list {}?


You need to first make sure the list has 2 elements (or at least assume it does.) Then you can run a loop stating from there. Something like:
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
/*** pseudo-code ***/

read a value into low
//Store it as the lowest value because it's the only value read so far.

read another value into x

if x is less then low {

    store low into low2
    //because it's the second lowest value read so far,
    //and you don't want to overwrite the value stored in low (yet)

    store x into low
    //because it's now the lowest value read in so far

} else { //(x must be greater then or equal to low)

    store x into low2
    //because it's the second lowest value read so far,
    //(the lowest is still in low where it should be)

}

loop for the remaining 8 values...
//just like you already had done 
Last edited on
Ok...something like this? ;)
...but still doesn't work as it should...

#include <iostream>
using namespace std;
int main()
{
int low,low2,x,in;
cout<<"Input: \n";
cin>>in;
low=in;
for(int i=0;i<10;i++)
{
cout<<"Input: \n";
cin>>x;
if(x<low)
{
low2=low;
low=x;
}
else
{
low2=x;
}
}
cout<<"Smalest: "<<low<<endl;
cout<<"Second smalest: "<<low2<<endl;

return 0;
}
This problem is pretty simple. You could make an array for four thousand elements if you want and its no harder. Just loop through your numbers, and sort them from highest to lowest or lowest to highest, your choice, and then move over 1 from the smallest number. This is just a sorting problem
After reading your code, I would recommend you make an array of 10 elements, and then either fill it using rand(), or fill it with input. I personally think this would be much easier, and you can see what's happening with your numbers much easier
1
2
3
4
5
6
cin >> in;
low = in;

//this could just be

cin >> low


Start with this (you don't need that extra "in" variable, that's what "x" was for.)
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
int low,  //lowest
    low2, //second lowest
    x;    //temp (for input)

cin >> low; //The first value entered is the lowest so far.
cin >> x; //Read a second value (we don't know if it's the lowest or second lowest yet.)
if( x < low ) {
    //We have a new lowest value, so...
    low2 = low; // what was in "low" is now the second lowest
    low = x; //"x" has the new lowest value
} else {
    //"x" is not the lowest, so it's the second lowest
    //because only two values have been entered so far
    low2 = x;
    //the lowest value is still stored in "low"
}

const int N = 10; //read in this many values
for( int i = 3; i <= N; i++ ) {
    //Here we read in values 3 through N
    ... //finish up this code
}

... //print the answer

return 0;
If you already have a container (vector, array, etc) C++ has the function nth_element, which will give you the 2nd (or any nth) smallest value:

1
2
3
4
5
6
7
8
#include <iostream>
#include <algorithm>
int main()
{
    int a[10] = {20, 11, -1, 20, 6, 101, -10, 0, 12, 11};
    std::nth_element(a, a+1, a+10);
    std::cout << "Second smallest: " << a[1] << '\n';
}


I think he should develop his own algorithm, for learning purposes.
This problem should be solved without using array....
Thanx for help!
Like i said still learning (noob) he he he ....


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
#include <iostream>
using namespace std;
int main()
{
    int low;
    int low2;
    int x;
    cout<<"Input: \n";
    cin>>low;
    for(int i=0;i<5;i++)
    {
        cout<<"Input: \n";
        cin>>x;

    if(x<low)
    {
        low2=low;
        low=x;
    }
    if((x>low)&&(x<low2))
    low2=x;

    }
    cout<<"Smalest: "<<low<<endl;
    cout<<"Second smalest: "<<low2<<endl;

    return 0;
}

Last edited on
Topic archived. No new replies allowed.