Reputation: 103
I have an array that will contains 5 random numbers between 0 to 99. I want to find the smallest and second smallest number of that array, I have the code below but it will not work if the first element is the smallest number. How can I fix this? Thank you for any help! :)
int numbers[5];
int main()
{
srand(time(NULL));
for(int i=0; i<5; i++){
numbers[i]=rand()%100;
cout<<numbers[i]<<" ";
}
cout<<endl;
int smallest = numbers[0], secondSmallest=numbers[0];
for(int i=0; i<5; i++){
if(numbers[i]<smallest){
secondSmallest=smallest;
smallest=numbers[i];
}
else if(numbers[i]<secondSmallest)
secondSmallest=numbers[i];
}
cout<<"Smallest number is : "<<smallest<<endl;
cout<<"Second smallest number is : "<<secondSmallest<<endl;
}
EDIT : this need to be done without sorting the array based on the requirements of the assignment.
Upvotes: 2
Views: 5327
Reputation: 708
Yet another way to get this done without using two loops. Takes care of duplicates.
int smallest = numbers[0],secondSmallest = numbers[0];
for(int i=0;i<5;i++){
if(numbers[i]>smallest){
if(numbers[i]<secondSmallest || smallest==secondSmallest){
secondSmallest = numbers[i];
}
}else{
if(secondSmallest>smallest && numbers[i]!=smallest){
secondSmallest=smallest;
}
smallest = numbers[i];
}
}
Upvotes: 2
Reputation: 3332
This can be solved by finding smallest and secondSmallest separately and initially assign them different values.
Here is the code:
int smallest = numbers[0], secondSmallest=99; //max value to secondSmallest
for(int i=0; i<5; i++){
if(numbers[i]<smallest){
smallest=numbers[i];
}
}
for(int i=0; i<5; i++){
if(numbers[i]<secondSmallest && numbers[i]!=smallest){
secondSmallest=numbers[i];
}
}
Upvotes: 1
Reputation: 12749
This can be solved in two ways.
You can initialize smallest
and secondSmallest
with the biggest number possible:
#include<limits>
// ...
int smallest = std::numeric_limits<int>::max(), secondSmallest = smallest;
Then I'd change the if statement inside your loop like this:
for( int i = 0; i < 5; i++ ) {
// ^^^^^ use a std::vector and change that to i < numbers.size()
if ( numbers[i] < secondSmallest ) {
if ( numbers[i] < smallest ) {
secondSmallest = smallest;
smallest = numbers[i];
} else {
secondSmallest = numbers[i];
}
}
}
Otherwise if you are sure to have at least 2 elements in your container, you can start comparing the first ones:
int smallest = numbers[0], secondSmallest = numbers[1];
if ( numbers[1] < numbers[0] ) {
secondSmallest = numbers[0];
smallest = numbers[1];
}
Then the starting condition of the loop can be changed:
for( int i = 2; i < numbers.size(); i++ ) { ...
// ^^^ we have already compared the first ones
Upvotes: 1
Reputation: 11
I would try to initialize secondSmallest number to be largest number, like 99 here. ( even if all the 5 numbers random to be 99, the second smallest number still be 99.)
Upvotes: 1
Reputation: 180585
If numbers[0]
is the smallest then you know some other index has to be the second smallest. Because of that we can use secondSmallest=numbers[1]
. Even if numbers[1]
is the smallest
if(numbers[i]<smallest){
secondSmallest=smallest;
smallest=numbers[i];
}
changes secondSmallest
to what smallest
was, which would be numbers[0]
, so it will still work.
We could also use std::numeric_limits
from <limits>
and set both variables to std::numeric_limits<int>::max()
which will make them the largest value an int
can store. None of the variables in the array can be greater than that so they will either be equal to or smaller than it.
Upvotes: 2
Reputation: 35
Try something like this in your else if:
else if(numbers[i]<secondSmallest && numbers[i] != smallest)
secondSmallest=numbers[i];
Upvotes: 1