cout second smallest and largest number

Oct 25, 2011 at 6:07am
Hello,
How can i print second smallest and largest numbers entered without using array.
(IF, Switch, Loop) are allowed.

please advise.


double small, ssmall, big, sbig;
double num;
cin >> num;
big = 0;
sbig = 0;
small = 0;
ssmall= 0;
for (; cin >> num ;)
if ( num < big)
small = num;
else if ((num < small))
sbig = num;
else if ( num > big)
{
sbig = big;
big = num;
}
else if (num < small)
{
ssmall = small;
small = num;
}


}//-5 3 -7 0 8 5 1 9 -1 2 11 6 p
Oct 25, 2011 at 7:18am
Is your problem just how to output something or is there something wrong with the program?

Output on the standard output is possible with: cout<<small;

The following is how I'd do it for smallest and second smallest number. Maybe it helps you in some way, it doesn't work perfectly but it works for all input smaller than 0.

#include<iostream>
using namespace std;

int main() {
int smallest = 0;
int second_smallest = 0;
int input;
bool program_continuation = true;
char yes_or_no;

while (program_continuation) {
cin>>input;
if (input < second_smallest) {
if (input <= smallest) {
second_smallest = smallest;
smallest = input;
}
else {
second_smallest = input;
}
}

cout<<"The current smallest number is: "<<smallest<<"\n";
cout<<"The current second smallest number is :"<<second_smallest<<"\n";

cout<<"Do you want to input another number?";
cout<<"Please type y for yes or n for no."<<"\n";
cin>>yes_or_no;

if (yes_or_no == 'n') {
program_continuation = false;
}
else {
program_continuation = true;
}
}
}


Last edited on Oct 25, 2011 at 7:22am
Oct 25, 2011 at 4:49pm
First the syntax for (; cin >> num ;) I guess is some way to loop as long there is input.Why don't you use while(cin >> num) instead?

Anyway you should do two checks for each case (smaller and bigger).
Compare with the bigger, if needed change it and second_bigger also, else compare with second_bigger if needed change it
Do the same for smaller.

1
2
if ( num < big)
small = num;


If num < big it's true you shouldn't do any change! You have the condition wrong here.
Oct 25, 2011 at 4:55pm
I modified the program to below but if i put second_number = 0 and my first input is less than 0 then i will lose first entry. do you have any solution for 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;

double smallest = 0;
double second_smallest = 0;
double input;

cin>>input;

for (;cin >> input;)
{
  if (input < second_smallest) 
  {
  if (input <= smallest) 
    {
	  second_smallest = smallest;
	   smallest = input;
    }
else {
second_smallest = input;
     }
  }
}

cin>>input;

cout<<"The current smallest number is: "<<smallest<<"\n";
cout<<"The current second smallest number is :"<<second_smallest<<"\n";
}
//sample number entered:
//-5 3 -7 0 8 5 1 9 -1 2 11 6 p 

Oct 28, 2011 at 6:47am
Any comment to fix this program?
Thank you.
Nov 3, 2011 at 8:37am
What you could do is not initializing smallest and second_smallest, but assign to them the first two values entered by the user and only then going into the for loop for checking if the next input is smaller. I'm not sure, but I think this should fix the problem.

So it would be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double smallest = 0;
double second_smallest = 0;
double input;

cin>>input;
smallest = input;
cin>>input;
if (input < smallest) {
   second_smallest = smallest;
   smallest = input;
} 
else {
   second_smallest = input;
}

//Rest as you already have it from line 8 on  

Last edited on Nov 3, 2011 at 8:41am
Nov 4, 2011 at 1:27pm

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
 
    double max  = 0;    //  largest # seen so far
    double max2 = 0;    //  second largest # seen so far
    double min  = 0;    //  smallest # seen so far
    double min2 = 0;    //  second smallest # seen so far
    unsigned count = 0; //  # of numbers seen so far
    for(;;){
        double x;
        cin >>x;
    if( !cin )  break;
        count++;
        if( count == 1 || x > max ){
            max2 = max;
            max = x;
        }else if( count == 2 || x > max2 ){
            max2 = x;
        }
        if( count == 1 || x < min ){
            min2 = min;
            min = x;
        }else if( count == 2 || x < min2 ){
            min2 = x;
        }
    }
    if( count >= 2 )
        cout <<min2 <<' ' <<max2 <<endl;
    ignoreWord();
Topic archived. No new replies allowed.