Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 18-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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:

Quote:
#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);
}
Reply With Quote
  #3  
Old 18-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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:

Quote:
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);

}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 18-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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:

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

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:33 PM.