Ask Experts » Computers » Software »

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


Date: 06 Jan 2010   Posted By: Charu     Group: Computers    Category: Software   

Please tell me the code for the above question



Author: Arvind Kumar Chauhan    07 Jan 2010      Member Level: Silver     Points : 4  (Rs 2)    Voting Score: 0

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


Author: Ambili    10 Jan 2010      Member Level: Silver     Points : 5  (Rs 4)    Voting Score: 0

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.


Author: mustaqeem Ali    12 Jan 2010      Member Level: Silver     Points : 2    Voting Score: 0


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;

}



Reference: http://www.IndiaStudyChannel.com?ref=mustaqeem_2002

Author: mustaqeem Ali    12 Jan 2010      Member Level: Silver     Points : 2    Voting Score: 0

#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();
}


Reference: http://www.IndiaStudyChannel.com?ref=mustaqeem_2002

Author: mustaqeem Ali    29 Jan 2010      Member Level: Silver     Points : 4  (Rs 2)    Voting Score: 0

#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();
}





Post Answer



You must Sign In to post a response.


Post Answer        





Related Questions
  • Return to Question Index
  • Ask Question to Experts

  • Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.