Getting the second smallest number from 4 inputs.

Hey guys, I am stuck and don't know what to do next. I was going with this approach where I get both the smallest number and the second smallest and at the end I will eliminate the smallest. I don't know if this is possible though. Can someone please help me. Here is what I have so far.

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
// Returns the second smallest of a, b, c, d.
// I.e. in order from smallest to largest, the second.
int secondSmallest(int a, int b, int c, int d)
{
	// EDIT HERE
	int smallest, secondsmallest;

	cin >> a >> b >> c >> d;
	if (a < b)
	{
		smallest = a;
		secondsmallest = b;
	}
	else
	{
		smallest = b;
		secondsmallest = a;
	}
	if (c < d)
	{
		smallest = c;
		secondsmallest = d;
	}

	if (d < smallest)
		smallest = d;
	else if (d > secondsmallest)
		secondsmallest = d;
	return 0;
} Put the code you need help with here.
Perhaps rather, you could use an array? something along the lines of:
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
int secondSmallest(int arr[],int size)
{
    int smallest,second;
    second = 0;
    smallest = arr[0];
    for(i = 0;i < size;i++)
    {
       if(arr[i] < smallest)
       {
           smallest = arr[i];
        }
     }    
     second = smallest;
     for(i = 0;i < size;i++)
    {
       if(arr[i] == smallest)
       {
           continue;
       }
       if(second == smallest || arr[i] < second)//It will always be larger than smallest
       {
            second = arr[i];
        }
    }
}
Would my program work though? And thanks for your help!
Oh and I can only use the...
1
2
3
4
int secondSmallest(int a, int b, int c, int d)
{
     return 0;
}
njleos3 wrote:
Oh and I can only use the...

1
2
3
4
int secondSmallest(int a, int b, int c, int d)
{
     return 0;
}


no problem:

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
int secondSmallest(int a, int b, int c, int d)
{
 int arr[4];
arr[0]=a;
arr[1]=b;
arr[2]=c;
arr[3]=d;


//@tristan1333's code here

int smallest,second;
    second = 0;
    smallest = arr[0];
    for(i = 0;i < size;i++)
    {
       if(arr[i] < smallest)
       {
           smallest = arr[i];
        }
     }    
     second = smallest;
     for(i = 0;i < size;i++)
    {
       if(arr[i] == smallest)
       {
           continue;
       }
       if(second == smallest || arr[i] < second)//It will always be larger than smallest
       {
            second = arr[i];
        }
    }



}


PS: Maybe your teacher won't love this approach :)

Basically you could put everything in an array, sort the array and pick the 2nd element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int smallest( int a, int b ) { return a < b ? a : b ; }

int smallest( int a, int b, int c ) { return smallest( smallest(a,b), c ) ; }

int smallest( int a, int b, int c, int d ) { return smallest( smallest(a,b), smallest(c,d) ) ; }

int second_smallest( int a, int b, int c, int d )
{
    const int min_value = smallest(a,b,c,d) ;

    if( min_value == a ) return smallest(b,c,d) ;
    else if( min_value == b ) return smallest(a,c,d) ;
    else if( min_value == c ) return smallest(a,b,d) ;
    else return smallest(a,b,c) ;
}
@JLBorges

You really do like using a lot of functions. It just confuses me there's so many functions lol for itty bitty things. I'd rather combine some things. I try to read your code but functions call for other functions all over.

I remember when you answered me for my triangle calculator post and I was bewildered back then lol.
Out of curiosity, what should return from secondSmallest( 3, 2, 5, 2 )?


One logical approach is to find the smallest of the set and remove it from the set. Then look for smallest again from the remaining set, which yields the second smallest of the original set. That, however, is overkill for four values.


have to use
1
2
3
4
int secondSmallest(int a, int b, int c, int d)
{
     return 0;
}

1. return 0; ... 0?
2. Your function has fixed signature/interface, but can your implementation call other functions?


A "priority queue" with two positions.
* The first input goes to the front of the queue.
* The second input either inserts to the front, or goes to the second position
* All remaining inputs have three possibilities:
a) insert front
b) go to second position
c) dismissed
Insertion to front requires moving the previous front to the second position. Second smallest will be in the second position after all input has been processed.

No arrays, no loops. Just if..else ; if..else if..else ; if..else if..else
> what should return from secondSmallest( 3, 2, 5, 2 )?

It was specified in the problem statement:
1
2
// Returns the second smallest of a, b, c, d.
// I.e. in order from smallest to largest, the second. 


> You really do like using a lot of functions.

Yes.


> I remember when you answered me for my triangle calculator post

I don't remember it; but just to refresh my memory, was it something like this?

http://www.cplusplus.com/forum/beginner/197065/#msg946058
boost lexical cast wrote:
You really do like using a lot of functions.

It makes it more structured sometimes to have it split up.

If you want fewer, you can always use variadic templates :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename T>
T smallest(T _a, T _b) {
  return (_a < _b ? _a : _b);
}

template <typename T, typename... others>
T smallest(T _a, T _b, others... other) {
  return smallest(smallest(_a, _b), other...);
}

int main()
{
  int small = smallest(9, 3, -1, 7);

  std::cout << "small = " << small << std::endl;
}

which outputs -1 as the smallest. Then do whatever you need to get the second smallest one.

If you want it all in one function
1
2
3
4
5
6
7
8
int smallest_int(int a, int b, int c, int d)
{
  int smaller = (a < b ? a : b);
  smaller = (smaller < c ? smaller : c);
  smaller = (smaller < d ? smaller : d);

  return smaller;
}


JLBorges answers many questions, and I typically find his input very insightful.
Topic archived. No new replies allowed.