Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

second largest number in array

+1 vote
asked Jul 15, 2018 by anonymous

5 Answers

0 votes
answered Jul 16, 2018 by Naveen Gowda
#include<stdio.h>
#include<stdlib.h>
#define INT_MIN 0

int main()
{
    int n, first, second, i;
    int arr[50];
   
    printf("Enter the number elements\r\n");
    scanf("%d", &n);

    printf("Enter the ARRAY elements\r\n");   
    for(i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
   
    if(n < 2)
    {
        printf("Invalid input!!!: Array should contain atleast two elements\r\n");
        return 0;
    }

    printf("The Array elements are:");
    for(i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }
   
   
    printf("\r\n");

   
    first = second = INT_MIN;
    for(i = 0; i < n; i++)
    {
        if(arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
        else if(arr[i] > second && arr[i] != first)
        {
            second = arr[i];
        }
    }
   
    if(second == INT_MIN)
    {
        printf("There is no second largest elements in the ARRAY\r\n");
    }
    else
    {
        printf("The second largest element in the ARRAY is: %d\r\n", second);
    }
   
   
    return 0;
   
}
commented Jul 16, 2018 by anonymous
#include <stdio.h>

int main()
{
    int a[100];
    int i,j,n;
    printf("Enter the number elements\r\n");
    scanf("%d", &n);

    printf("Enter the ARRAY elements\r\n");   
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    
    int big = a[0],second_big = a[0];
    
    for (i = 1; i < n; i++)
    {
        if (big < a[i])
        {
            big = a[i];
        }
        if (second_big < a[i] && (a[i] < big))
        {
           second_big= a[i];
        }
    }
    
    printf("The  big number in array is %d\n",big);
    
    printf("The  second big number in array is %d\n",second_big);
}
0 votes
answered Jul 16, 2018 by anonymous
#include <iostream>

using namespace std;

int main()
{
int a[5],max,median;
for(int i=0;i<5;i++)
cin>>a[i];
max=a[0];
median=a[0];
for(int i=0;i<5;i++)
{if(a[i]>max)
max=a[i];
}
for(int i=0;i<5;i++)
{if(a[i]>median && a[i]<max)
median=a[i];
}
cout<<max<<endl<<median;

    return 0;
}

مع تحيات طلاب انظمة الحاسوب...العراق بغداد معهد ادارة
0 votes
answered Jul 16, 2018 by anonymous
#include <stdio.h>

int main()
{
    int a[100];
    int i,j,n;
    printf("Enter the number elements\r\n");
    scanf("%d", &n);

    printf("Enter the ARRAY elements\r\n");   
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    
    int big = a[0],second_big = a[0];
    
    for (i = 1; i < n; i++)
    {
        if (big < a[i])
        {
            big = a[i];
        }
        if (second_big < a[i] && (a[i] < big))
        {
           second_big= a[i];
        }
    }
    
    printf("The  big number in array is %d\n",big);
    
    printf("The  second big number in array is %d\n",second_big);
}
0 votes
answered Jul 25, 2018 by ABARNA RAVI (140 points)
#include<stdio.h>
void main(int argc,char *argv[])
{
int a,b,c;
if(argc==4)
{
a=atoi(argv[1]);
b=atoi(argv[2]);
c=atoi(argv[3]);
printf("Entered values for A, B and C %d\t%d\t%d\n",a,b,c);
if((a>b) && (a>c))
{
printf("A is largest value \n");
}
else if(b>c)
{
printf("B is largest value \n");
}
else
{
printf("C is largest value \n");
}
}
else{
printf("enter three argument");
}
}
0 votes
answered Jul 25, 2018 by flore
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main()
{
    int     num;            //number of elements
    int     *arr;           //array of elements
    int     first_big;      //biggest element
    int     second_big;     //2nd biggest element
    int     i_second = 0;   //initialization of the second elemnt
    int     i;
    
    
    printf("enter how many elements in the array: ");
    scanf( "%d", &num);
    printf( "you enter %d\n", num);
    
    if( num <= 1)
    {
        printf( "Bad value %d ; number of elements should be at least 2 \n", num );
        return 0;
    }
    
    if( (arr = (int *)malloc( num *sizeof ( int ))) == NULL)
    {
        printf( "allocation failed for %d elements\n", num );
        return (0);
    }
    
    for ( i = 0; i < num; i ++)
    {
        printf( "element %d : " , i+1);
        scanf( "%d", &arr[i]);   
        if( !i )
        {
            //initialization of biggest element
            first_big = arr[i];
        }
        else if ( arr[i] == first_big )
        {
            //nothing to do
            continue;
        }
        else if( arr[i] > first_big )
        {
            //we switch
            if( i_second)
            {
                second_big = first_big;
            }
            //we save the new biggest one
            first_big = arr[i];
        }
        else if( !i_second )
        {
            //initialization of 2nd biggest element
            second_big = arr[i];
            i_second = 1;
        }
        else if( arr[i] > second_big )
        {
            //value of 2nd biggest element
            second_big = arr[i];
        }
        
    }
    //list of elements
    printf( "***your data**** \n");
    for ( i = 0; i < num; i ++)
    {
        printf( "element %d  %d\n", i+1, arr[i]);
    }
    //and the result
    if( first_big && !i_second )
    {
        printf( "all the values entered are the same %d\n", first_big );
    }
    else
    {
        printf( "the biggest is : %d\n", first_big );
        printf( "the second biggest is : %d\n", second_big );
    }
    
    return 1;

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...