how can i print the 2nd smallest number

Input: Take integer inputs from the user until she enters a negative number.
Output: The smallest and the 2nd smallest of all the non-negative numbers entered.
For example, is the input was 4, 3, 2, 121, 12, -5: the outputs should be 2 and 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>
using namespace std;
int main(){
	int a=0;int b=99999999;int c=9999999;
	cout<<"enter the numbers \n";
	while (a>=0){
		cin>>a;
		if(a<b && a>=0)b=a;
		if(a>b && a<c && a>=0)c=a;
	}
	cout<<"the smallest pasitive number is: "<<b<<endl;
	cout<<"the second smallest pasitive number is: "<<c<<endl;
	return 0;
}




can some one tell me where an why and how i need correction
Why don't you use a vector instead? You can then simply sort it in ascending order and write the values of the first two elements to the console screen.
Using a vector (or an array) is a vast overkill for this task. It is necessary to store only three numbers:
smallest
second smallest
current user input
First, a design question: If the input is {2,4,2,3}, what is the output?
* Is it {2,2}, because they are indeed the two smallest inputs, or
* Is it {2,3}, because they are the two smallest unique values

Now, you have got new input. The options are:
0. Negative
1. It is a new record on smallness
2. It equals current minimum
3. It is between minimum and second
4. It equals second smallest
5. It is big

Case 1 must clearly update both current values.
Case 3 has to update the second.


There are numeric limit constants defined in standard library that are more expressive for the b and c to start with than those magic 99..9's.

Consider
1
2
3
4
5
6
while ( cin>>a ) {
  if ( a < 0 ) { break; }
  else if ( a < b )
   { ... }
  else if ...
}

The >> can fail. This version protects from it.
A negative input breaks out from the loop and thus you don't need to test for it in the later cases.
Your current problem is with the first three numbers (4,3,2) they overwrite each other in the first if statement so it never gets to the next

here is how your while loop looks as the numbers are put in so maybe you can understand it better, I replace the variables with actual numbers so it is more clear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//numbers 4,3,2,121,12 (5 numbers)
//first pass
a = 4;
if(4 < 99999999 && 4 >= 0) b = 4; //true so b is now 4
if (4 > 4 && 3 < 9999999 && 3 >= 0) c=a; //not true so c stays as 99999999
//second pass
a = 3;
if (3 < 4 && 3 >= 0) b = 3; // true so 4 is now gone
if (3 > 3 && 3 < 9999999 && 3 >= 0) c=a; //the if statement is not true so no assignment
//third pass
a = 2;
if (2 <  3 && 2 >= 0) b = 2; //true so b = 2
if (2 >2 && 2 < 9999999 && 3 >= 0) c=a; //false so c stays as 99999999
//forth pass
a = 121;
if (121 < 3 && 2 >= 0 ) b = a; //not true so b is still 2
if (121 > 2 && 121 < 9999999 && 121 >= 0) c = 121;  //true so c is now 121
//final pass
a = 12;
if (12 < 2 && 12 >= 0) b = a;//not true so b is still 2
if (12 > 2 && 12 < 121 && 12 >= 0) c = 12; //true so c is now 12

cout << b; //b is 2 at the end
cout << c; //c is 12 at the end 



so the problem is that once b is reassigned the number that was there just goes away. So you need to find a way from losing what b used to be.

Last edited on
thanks to all...
Topic archived. No new replies allowed.