I'm working on a program that takes 5 integers from input and then prints out the smallest and second smallest of those integers. The program correctly outputs the smallest number but the program often outputs the second smallest number incorrectly because of ordering.
For example, if I enter 33, -6, -15, 0, and 12, the program will print -15 as the smallest and -6 as the second smallest. If however, I enter 3, 6, 7, 8, and 9, the program outputs 3 as the smallest and instead of outputting 6 as the second smallest, I get garbage for the second smallest. How can I fix this?

My Code:

#include <iostream>
using namespace std;

int main()
{
    
    const int MAX_NUM_INTEGERS = 5;
    int Integers[5];
    int i = 0; 
	int Smallest_Value = INT_MAX;
	int Second_Smallest_Value = 0;

    while ( i < MAX_NUM_INTEGERS)
    {
        cout << "Enter an Integer : ";
		cin >> Integers[i];
        i++;
        
        if (Integers[i-1] < Smallest_Value)
        {
            Second_Smallest_Value = Smallest_Value;
			Smallest_Value = Integers[i-1];
        }
    }

    cout << "The Smallest Integer Entered is: " << Smallest_Value << " and the Second Smallest is: " << Second_Smallest_Value << "\n";

    return 0;
}

First set the smallest and secondsmallest to int_max

int Smallest_Value = INT_MAX;	
int Second_Smallest_Value = INT_MAX;

then you need to set the second smallest then the first like this:

cin >> Integers[i];
if(Integers[i]<Smallest_Value )
{
Second_Smallest_Value =Smallest_Value;
Smallest_Value=Integers[i];
}
else
{
if(Integers[i]<Second_Smallest_Value )
{
Second_Smallest_Value =Integers[i];
}
}
i++;

Edited 5 Years Ago by mazzica1: n/a

First set the smallest and secondsmallest to int_max

int Smallest_Value = INT_MAX;	
int Second_Smallest_Value = INT_MAX;

then you need to set the second smallest then the first like this:

cin >> Integers[i];
i++;
if(Integers[i]<Smallest_Value )
{
Second_Smallest_Value =Smallest_Value;
Smallest_Value=Integers[i];
}
else
{
if(Integers[i]<Second_Smallest_Value )
{
Second_Smallest_Value =Integers[i];
}
}

I added the changes you suggested,

My code looks like this:

#include <iostream>
using namespace std;

int main()
{
    
    const int MAX_NUM_INTEGERS = 5;
    int Integers[5];
    int i = 0; 
	int Smallest_Value = INT_MAX;
	int Second_Smallest_Value = INT_MAX;

    while ( i < MAX_NUM_INTEGERS)
    {
        cout << "Enter an Integer : ";
		cin >> Integers[i];
		i++;
		if(Integers[i]<Smallest_Value )
		{
			Second_Smallest_Value = Smallest_Value;
			Smallest_Value = Integers[i];
		}
		else
		{
			if(Integers[i] < Second_Smallest_Value)
			{
				Second_Smallest_Value =Integers[i];
			}
		}
	}

    cout << "The Smallest Integer Entered is: " << Smallest_Value << " and the Second Smallest is: " << Second_Smallest_Value << "\n";

    return 0;
}

When I compile and run it however, I get garbage for both the smallest and second smallest numbers. Any ideas? I'll keep working on it.

I fixed it! I'm not sure why but changing to [i-1] makes it work.

Working Code:


#include <iostream>
using namespace std;

int main()
{
    
    const int MAX_NUM_INTEGERS = 5;
    int Integers[5];
    int i = 0; 
	int Smallest_Value = INT_MAX;
	int Second_Smallest_Value = INT_MAX;

    while ( i < MAX_NUM_INTEGERS)
    {
        cout << "Enter an Integer : ";
		cin >> Integers[i];
		i++;
		if(Integers[i-1] < Smallest_Value ) //[i-1]
		{
			Second_Smallest_Value = Smallest_Value;
			Smallest_Value = Integers[i-1]; //[i-1]
		}
		else
		{
			if(Integers[i-1] < Second_Smallest_Value)//[i-1]
			{
				Second_Smallest_Value = Integers[i-1]; //[i-1]
			}
		}
	}

    cout << "The Smallest Integer Entered is: " << Smallest_Value << " and the Second Smallest is: " << Second_Smallest_Value << "\n";

    return 0;
}

Thanks Mazzica1!!

Edited 5 Years Ago by NickPatton: n/a

sorry it was my fault you read the value to integers then add i so the next code will use the un assigned location

This article has been dead for over six months. Start a new discussion instead.