• C program to find second largest in the given set of numbers

Please tell me the code for the above question
• Hi Friend,

Please find the code to find the second highest number among the given set of numbers.

The logic being used is first we will enter the amount of numbers we have. Now for loop will begin and ask us to enter the number one by one till we enter all the number which we will store in num.

But side by side it is comparing values in variable a and b, if the value is greater than a, then we will store the value of a in b, and value of num in a, else if the value is greater than b but less than a then we will store the value in b only. In all other cases we will discard the value.

Finally once all the numbers are entered and when we exit the for loop, the number in variable b is the second highest and we display that value.

#include<conio.h>
#include<stdio.h>
void main()
{
int i, n, a, b, num;
a = b = 0;
clrscr();
printf("Enter the Maximum amount of Numbers :: ");
scanf("%d", &n);
printf("\n\n");
for(i=0; i<n; i++)
{
printf("\nEnter the Number :: ");
scanf("%d", &num);
if (num > a)
{
b = a;
a = num;
}
else if (num > b)
b = num;
}
printf("\n\n Second Highest Number is :: %d",b);
getch();
}

Thanks,
Arvind
Learn SQL/PL-SQL

• The above program returns a valid output, But if all the input numbers are negative, It will not produce an output.
So I want to introduse an another logic for impliment the required output.
#include
#include
void main()
{
int n,a[20],i;
clrscr();
printf("\n Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i{
scanf("%d",&a[i]);
}
/*sorting */
for(i=0;i {
for(j=i+1;j {
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("The 2nd largest element in the given array is %d",a[n-2]);
getch();
}
This program sorted the array in ascenting order first and then print the 2nd largest element ie a[n-2]:
by the concept of array 1st element is in a[0]& last in a[n-1], therefore 2nd last is in the n-2 th position.

• double max1 = maxFirst(myArray, arraySize);

double max2 = maxSecond(myArray, arraySize, max1);

cout << endl << "First biggest number is: " << max1;
cout << endl << "Second biggest number is: " << max2;

system("PAUSE");
getch();
return 0;

}

double maxFirst(const double data[], int index)
{

double maxNumber1 = data[0];
for (int i = 0; i < index; i++)
{

if (maxNumber1 < data[i])

{

maxNumber1 = data[i];

}

}

return maxNumber1;

}

double maxSecond(const double data[], int index, double maxNumber1)
{

double maxNumber2 = data[1];
for (int j = 0; j < index; j++)
{

if (data[j] == maxNumber1)
{

continue;

}
else if (maxNumber2 < data[j])
{

maxNumber2 = data[j];

}

}

return maxNumber2;

}

• #include
#include
void main()
{
int n,a[20],i;
clrscr();
printf("\n Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i{
scanf("%d",&a[i]);
}

for(i=0;i {
for(j=i+1;j {
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("The 2nd largest element in the given array is %d",a[n-2]);
getch();
}

• #include
#include
void main()
{
int n,a[20],i;
clrscr();
printf("\n Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i{
scanf("%d",&a[i]);
}

for(i=0;i {
for(j=i+1;j {
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("The 2nd largest element in the given array is %d",a[n-2]);
getch();
}

• The second largest number in a set can be found using many ways. Some of them are:

1) Remove the largest number of the list to form an intermediate list. Now, find the largest number of this intermediate list which is the second largest number of the original list.
#include<stdio.h>
#include<conio.h>
int max(int a[],int n)
{
int i,max;
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
int min(int a[],int n)
{
int i,min;
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
void main(void)
{
int n,a[25],i;
clrscr();
printf("Enter the size : ");
scanf("%d",&n);
printf("Enter the list :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
minimum=min(a[i]);
maximum=max(a[i]);
for(i=0;i<n;i++)
{
if(a[i]=maximum)
a[i]=minimum-1;
}
printf("\n Second largest number : %d",max(a[i]);
getch();
}
This program finds the minimum and maximum numbers of the list. Then, it replaces the maximum number with a number less than the minimum number. Therefore, the current largest number will be the second largest number of the list.

2) Sort the entire list in ascending (descending) order and display the second number from last (first) which is the second largest number of the original list.
#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j,n,temp,a[25];
clrscr();
printf("Enter number of elements : ");
scanf("%d",&n);
printf("Enter the list before sorting :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The second largest number is : %d",a[n-2]);
getch();
}
This program performs bubble sort on the list. Then, it displays the second largest number (second from last in ascending order and second from first in descending order). This program can be used to display largest number, second largest number, third largest number … and smallest number, second smallest number, third smallest number …