Results 1 to 4 of 4

Thread: How to find second largest value from array

  1. #1
    Join Date
    Jan 2009
    Posts
    48

    How to find second largest value from array

    Hi friends,

    I am beginner is programming field,May be because of that I have troubles while writing complex and large programs. One of them is below.

    I have to write the program which will display the second highest value from the array values.But there is also one condition that the this number should not be negative value.



    Can you able to help me to code this program.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to find second largest value from array

    Hi,

    Good thing for you is that I have coded the program to print second highest value from the all values from the array. see below:

    #include<stdio.h>
    void main()
    {
    int i,j,n,a[20],first_max,second_max;
    printf("Enter how many numbers you want to enter");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter only 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);
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to find second largest value from array

    Something below can help you. I used sorting method to find the 2nd highest number.

    Code:

    public static void main(String[] args) {
    int array[]={13,12,34,56,73,21,232,234,235,240};
    int max ,secndmax;
    max = secndmax= array[0];
    System.out.println("Initial value is "+ secndmax);
    for (int i=1;i<array.length;i++){
    if (array[i]>max ){
    secndmax=max;
    max=array[i];
    }else if(array[i]>secndmax){
    secndmax = array[i];
    }
    }

    System.out.println("Second Max element is "+ secndmax);

    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to find second largest value from array

    I am not giving you the all the answer. But according to me you have to use 'if loop' to verify condition inside 'for loop'.

    For reference take look on the below loop:

    max = second_max= array[0];
    for (i=0;i,length;i++)
    {
    if (array[i]>max)
    {
    2ndmax=max;
    max=array[i];
    }
    }
    return second_max

Similar Threads

  1. Unable to find elements of a given type in an array in C#
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 06:46 PM
  2. program to find the largest value in java.
    By Luis-Fernando in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 06:12 PM
  3. How to find a mode in an Array set
    By Quattro in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 11:55 AM
  4. To find key in array for Javascript.
    By Austinage in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 08:13 PM
  5. Find Largest No
    By kelfro in forum Software Development
    Replies: 1
    Last Post: 10-01-2009, 07:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,274,185.35422 seconds with 17 queries