Given a list of positive integers, find the 2nd largest integer.
Note: Usage of arrays is forbidden (only concepts until loops are allowed)
Here is my code:
#include <stdio.h>
#include <limits.h>
int main(){
int N, x, i, max2; //N = total no. of given numbers
int max = INT_MIN;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &x);
if (x >= max)
max = x;
else
continue;
}
for (i = 1; i <= N; i++) {
scanf("%d", &x);
if (x == max)
continue;
else if (x < max) {
int max2 = INT_MIN;
if (x >= max2)
max2 = x;
}
printf("2nd largest no.= %d", max2);
}
return 0;
}
This is what I did but this is not working as expected although no errors were thrown. Basically, I first found the largest number from the given list of numbers and then I was trying to find the largest number from the remaining no.s
what changes should I make in this code? Also please suggest a better method for this problem (if any).
Your code is wrong at least because you need to use only one sequence of entered data and consequently only one for loop.
Pay attention to that in this statement
printf("2nd largest no.= %d", max2);
there is used the variable max2
declared in the outer block scope
int N, x, i, max2; //N = total no. of given numbers
It is not the same as the variable max2
declared in this compound statement
else if (x < max) {
int max2 = INT_MIN;
if (x >= max2)
max2 = x;
}
So in the call of printf
there will be used the uninitialized variable max2
declared in the outer block scope.
To discover the second largest number makes sense when it is assumed that it shall not be equal to the first largest number. More precisely the program searches the second greatest value among entered values.
Here is a demonstration program that solves the problem.
#include <stdio.h>
int main( void )
{
unsigned int n;
if (scanf( "%u", &n ) == 1)
{
int first_max, second_max;
int second_max_exists = 0;
int value;
for (unsigned int i = 0; i < n && scanf( "%d", &value ) == 1 && value > 0; i++)
{
if (i == 0)
{
first_max = value;
}
else if (first_max < value)
{
second_max = first_max;
second_max_exists = 1;
first_max = value;
}
else if (( value != first_max ) && ( ( second_max_exists == 0 ) || ( second_max < value ) ))
{
second_max = value;
second_max_exists = 1;
}
}
if ( second_max_exists ) printf( "2nd largest no.= %d\n", second_max );
else puts( "There is no 2nd largest no." );
}
}
The program output might look like
10
1 1 1 2 3 3 4 5 7 7
2nd largest no.= 5
Indeed the first largest number is equal to 7
and the second largest number is equal to 5
.
As you are expecting only positive numbers then you could allow the user to enter a sequence of positive numbers of any length until zero or a negative number will be entered.
In this case the program can look simpler as for example.
#include <stdio.h>
int main( void )
{
int first_max, second_max;
int first_max_exists = 0, second_max_exists = 0;
puts( "Enter a sequence of positive numbers (0 or negative number - exit)" );
int value;
while ( scanf( "%d", &value ) == 1 && value > 0 )
{
if (first_max_exists == 0)
{
first_max = value;
first_max_exists = 1;
}
else if (first_max < value)
{
second_max = first_max;
second_max_exists = 1;
first_max = value;
}
else if (( value != first_max ) && ( ( second_max_exists == 0 ) || ( second_max < value ) ))
{
second_max = value;
second_max_exists = 1;
}
}
if (second_max_exists) printf( "2nd largest no.= %d\n", second_max );
else puts( "There is no 2nd largest no." );
}