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>
usingnamespace 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;
}
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;
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:
/*** 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 xifx 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
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
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"
}
constint 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;