second biggest in an array

This is a discussion on second biggest in an array within the C Programming forums, part of the General Programming Boards category; input array elements:5,6,3,1,7 output:biggest number:7, second biggest=7 can anyone help me out what is wrong??? Code: /*c program to find ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    26

    second biggest in an array

    input array elements:5,6,3,1,7
    output:biggest number:7, second biggest=7

    can anyone help me out what is wrong???

    Code:
    /*c program to find out the maximum and the second maximum number from an
    array of integers*/
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	 int arr[10];
    	 int i;
    	 int n;
    	 int big;
    	 int sec_big;
    	 printf("\n How many:");
    	 scanf("%d",&n);
    	 for(i=0;i<n;i++)
    	 {
    			printf("\n Enter %d number:",i+1);
    			scanf("%d",arr[i]);
    	 }
    	 big=arr[0];
    	 for(i=0;i<n;i++)
    	 {
    			if(arr[i]>big)
    			{
    				 big=arr[i];
    			}
    	 }
    	 sec_big=arr[0];
    	 for(i=0;i<n;i++)
    	 {
    			if((arr[i]>sec_big)&&(sec_big<big))
    			{
    				 sec_big=arr[i];
    			}
    	 }
    	 printf("\n Biggest number=%d,second biggest=%d",big,sec_big);
    	 getch();
    }

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Pulock2009 View Post
    input array elements:5,6,3,1,7
    output:biggest number:7, second biggest=7

    can anyone help me out what is wrong???

    Code:
    /*c program to find out the maximum and the second maximum number from an
    array of integers*/
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	 int arr[10];
    	 int i;
    	 int n;
    	 int big;
    	 int sec_big;
    	 printf("\n How many:");
    	 scanf("%d",&n);
    	 for(i=0;i<n;i++)
    	 {
    			printf("\n Enter %d number:",i+1);
    			scanf("%d",arr[i]);
    	 }
    	 big=arr[0];
    	 for(i=0;i<n;i++)
    	 {
    			if(arr[i]>big)
    			{
    				 big=arr[i];
    			}
    	 }
    	 sec_big=arr[0];
    	 for(i=0;i<n;i++)
    	 {
    			if((arr[i]>sec_big)&&(sec_big<big))
    			{
    				 sec_big=arr[i];
    			}
    	 }
    	 printf("\n Biggest number=%d,second biggest=%d",big,sec_big);
    	 getch();
    }
    You left off the '&" in scanf S/B
    Code:
    scanf("%d",&arr[i]);
    Edit:
    Having fixed that, your code still has problems. Try to think it through. :-)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    i am unable to find out the mistake. please help.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pulock2009 View Post
    i am unable to find out the mistake. please help.
    You need to work with two variables...

    Code:
    Int Biggest = 0;
    Int Second = 0;
    
    for (x = 0; x < Elements; x++)
      { if (Array[x] > Biggest)
           { Second = Biggest;
              Biggest = Array[x]; } }

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by Pulock2009 View Post
    i am unable to find out the mistake. please help.
    I don't think it's good practice to use void main().

    Code:
    /*c program to find out the maximum and the second maximum number from an
    array of integers*/
    #include<stdio.h>
    
    int main(void)
    {
         int arr[10];
         int i;
         int n;
         int big;
         int sec_big;
         printf("\n How many:");
         scanf("%d",&n);
         for(i=0;i<n;i++)
         {
                printf("\n Enter %d number:",i+1);
                scanf("%d",&arr[i]);
         }
         big=arr[0];
         sec_big = 0;
         for(i=0;i<n;i++)
         {
                if(arr[i]>big)
                {
                    sec_big = big;
                    big=arr[i];
                }
         }
         printf("\n Biggest number= %d ,second biggest= %d\n",big,sec_big);    
        return 0;
    }

  6. #6
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    try

    if
    ((arr[i]>sec_big)&&(arr[i]<big))

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    many many thanks to all of you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 10:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21