Results 1 to 15 of 15

Thread: Print 2nd largest value from array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Print 2nd largest value from array

    Write a function to get the values in array but it souldnt get any negative values and print the second largest value from the values in the array?


  2. #2
    Junior Member
    Join Date
    Mar 2008
    Answers
    2

    Re: Print 2nd largest value from array

    func()
    {
    int a[10];
    int b;
    int j;
    clrscr();
    for(int i=0;i<10;i++)
    scanf("%d",&a[i]);
    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    if(a[j]>a[j+1])
    {
    b=a[j];
    a[j]=a[j+1];
    a[j+1]=b;
    }
    }
    printf("%d\t",a[i-2]);
    }

    Last edited by cssprasad; 03-08-2008 at 01:25 PM.

  3. #3

    Re: Print 2nd largest value from array

    Quote Originally Posted by cssprasad View Post
    func()
    {
    int a[10];
    int b;
    int j;
    clrscr();
    for(int i=0;i<10;i++)
    scanf("%d",&a[i]);
    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    if(a[j]>a[j+1])
    {
    b=a[j];
    a[j]=a[j+1];
    a[j+1]=b;
    }
    }
    printf("%d\t",a[i-2]);
    }
    sorry to say this i think its not right bcz its work as a swao my freind!


  4. #4

    Re: Print 2nd largest value from array

    Quote Originally Posted by cssprasad View Post
    func() { int a[10]; int b; int j; clrscr(); for(int i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) { for(j=0;j<10;j++) { if(a[j]>a[j+1]) { b=a[j]; a[j]=a[j+1]; a[j+1]=b; } } printf("%d\t",a[i-2]); }
    //program to find second largest number from an array
    #include<stdio.h>
    #include<conio.h> void main() { int i,j,m,a[10]; clrscr(); printf("enter your ten numbers plz\n"); for(i=0;i<=9 && i>=0;i++) scanf("&#37;d",&a[i]); for(i=0;i<=9;i++) for(j=0;j<=9;j++) if(a[i]>a[j]) { m=a[i]; a[i]=a[j]; a[j]=m; } printf("\n\nthe second largest number in the array is=%d",a[1]);
    getch(); }


  5. #5
    Junior Member
    Join Date
    Oct 2009
    Answers
    1

    Talking Re: Print 2nd largest value from array

    #include<iostream>

    using namespace std;

    int main()
    {
    int a[6] = { 7, 5, 9 , 4, 8 ,0};
    int n1,n2;
    n1 = n2 = a[0];

    for (int i = 0 ; i < 6 ; i++)
    {
    if( n2 < a[i])
    {
    if( a[i] > n1)
    {
    n2 = n1;
    n1 = a[i];
    }
    else
    {
    n2 = a[i];
    }
    }
    }

    cout << "Second Largest number : " << n2 << endl;
    return 0;
    }

    Last edited by cybersandipan; 10-20-2009 at 04:15 PM.

  6. #6
    Junior Member
    Join Date
    Mar 2008
    Answers
    24

    Thumbs up Re: Print 2nd largest value from array

    Hai,
    Here is the Query in SQL Server 2005, to get the Second highest Salary from a EMP Table.

    select max(sal)<(select max(sal) from emp)


    Regards,
    Prashanth Chenna.


  7. #7
    Junior Member
    Join Date
    Mar 2008
    Answers
    2

    Smile Re: Print 2nd largest value from array

    #include<stdio.h>
    void main()
    {
    int i,j,n,a[20],first_max,second_max;
    printf("Enter how many numbers");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter a positive number");
    scanf("%d",&a[i]);
    }

    if(a[0]>a[1])
    first_max=a[0];
    second_max=a[1];
    else
    first_max=a[1];
    second_max=a[0];

    for(j=2;j<n;j++)
    if(a[j]>second_max)
    if(a[j]>first_max)
    {
    first_max=a[j];
    second_max=first_max;
    }
    else
    second_max=a[j];
    printf("The second largest number is %d", second_max);
    }


  8. #8
    Junior Member
    Join Date
    Sep 2009
    Answers
    1

    Smile Re: Print 2nd largest value from array

    Quote Originally Posted by sreenivasuluyc View Post
    #include<stdio.h>
    void main()
    {
    int i,j,n,a[20],first_max,second_max;
    printf("Enter how many numbers");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter a positive number");
    scanf("%d",&a[i]);
    }

    if(a[0]>a[1])
    first_max=a[0];
    second_max=a[1];
    else
    first_max=a[1];
    second_max=a[0];

    for(j=2;j<n;j++)
    if(a[j]>second_max)
    if(a[j]>first_max)
    {
    first_max=a[j];
    second_max=first_max;
    }
    else
    second_max=a[j];
    printf("The second largest number is %d", second_max);
    }
    this is definitely the best answer.. thank you


  9. #9
    Junior Member
    Join Date
    Mar 2009
    Answers
    16

    Re: Print 2nd largest value from array

    use simple concept......
    swap the largest element at first position a[0]....second largest at second position at a[1].......smallest at last ...then print a[1]...
    *******************************
    #include<stdio.h>
    #include<conio.h>
    void main(){
    int a[100],n,i,t;
    clrscr();
    printf("enter the value of n:\n");
    scanf("%d",&n);
    printf("enter the elements\n");
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);}
    for(i=0;i<n-1;i++){
    if(a[i+1]>a[i]){t=a[i];
    a[i]=a[i+1];
    a[i+1]=t;}
    }
    printf("the second largest %d",a[1]);
    getch();
    }
    *************************************
    sumitsolution@gmail.com


  10. #10
    Junior Member
    Join Date
    Mar 2009
    Answers
    16

    Re: Print 2nd largest value from array

    sort the numbers in decending order.......so second element will be at a[1]......
    *********************************************************
    #include<stdio.h>
    #include<conio.h>
    void main(){
    int a[100],n,i,t;
    clrscr();
    printf("enter the value of n:\n");
    scanf("%d",&n);
    printf("enter the elements\n");
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);}
    for(i=0;i<n-1;i++){
    if(a[i+1]>a[i]){t=a[i];
    a[i]=a[i+1];
    a[i+1]=t;}
    }
    printf("the second largest %d",a[1]);
    getch();
    }

    ****************************************************
    sumitsolution@gmail.com


  11. #11
    Junior Member
    Join Date
    Jan 2010
    Answers
    1

    Re: Print 2nd largest value from array

    how about?

    void func(int * array, int length)
    {
    int i;
    int max=array[0],max2=array[0];
    for(i=0;i<length;i++)
    {
    if(array[i]>max)
    {
    max2=max;
    max = array[i];
    }
    else if(((array[i]<max)&&(array[i]>max2))||(max==max2))
    {
    max2 = array[i];
    }
    }
    printf("Second largest value is &#37;d\n",max2);
    }

    Last edited by maddjango; 01-29-2010 at 03:05 AM. Reason: shortening the code

  12. #12
    Junior Member
    Join Date
    Feb 2010
    Answers
    18

    Re: Print 2nd largest value from array

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[15];
    int i,j,n;
    scanf("&#37;d",&n); /* size of the array */
    for(i=0;i<n;i++)
    scanf("%d",a[i]);
    for(i=0;i<=-1;i++)
    for(j=i;j<=n-1;j++)
    if(a[i]>a[j])
    a[i]^=a[j]^=a[i]^=a[j];

    printf("second largest number: %d" ,a[n-2]);
    getch();
    }


  13. #13

    Re: Print 2nd largest value from array

    Solution 1:

    int arr[6] = {5,3,11,2,6,40};

    int large,second_large,i;

    large = second_large = arr[0];

    for(i=1;i<6;i++)
    {
    if(arr[i] > second_large && arr[i] < large)
    second_large = arr[i];

    if(arr[i] > large)
    {
    second_large = large;
    large = arr[i];
    }
    }
    printf("\n Large = %d",large);
    printf("\n Second Large = %d",second_large);


    The time complexity is O(n).


    Solution 2:
    Do bubble sort; execute loop only two times. Bubble sort finds the Highest numbers first. If size of an array is n then index of second largest number is n-2 and Arr[n-2] will be the second largest number.

    Time Complexity of this will be -
    The first loop will be executed n times hence O(n)
    Second loop will be executed n-1 times hence O(n-1)

    Total complexity - n + n-1 = O(2n-1)


    Solution 3:
    1. Build a max heap - O(n)
    2. Remove 2 elements - O(logn)

    Total Complexity = O(nlogn)


    Solution 3 is optimum.


  14. #14
    Junior Member
    Join Date
    Jun 2011
    Answers
    1

    Re: Print 2nd largest value from array

    here's a simple way to do it :P
    Code:
    int findmin(int array[], int max){
        int minNum = array[0];
        for(int i = 0; i < max; i++){
            if(array[i] < minNum){
                minNum = array[i];
            }
        }
        return minNum;
    }
    
    int findsecondhighest(int array[], int arraysize, int maxNum){
        int temp = findmin(array, arraysize);
        
        for(int i = 0; i < arraysize; i++){
            if(array[i] > temp && array[i] < maxNum){
                temp = array[i];
            }
        }
        return temp;
    }
    of course you'll need a findmax function too... so here's the code also :P
    Code:
    int findmax(int array[], int max){
        int maxNum = array[0];
        for(int i = 0; i < max; i++){
            if(array[i] > maxNum){
                maxNum = array[i];
            }
        }
        return maxNum;
    }



  15. #15
    Junior Member
    Join Date
    Aug 2011
    Answers
    2

    Re: Print 2nd largest value from array

    this one is good!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact