find second largest element in array w/o using sorting
techniques? use onle one for loop.

Answer Posted / paresh thorat

package pkg;

public class Number {
public static void main(String [] args){
//int arr[]={6,4,5,2};
int arr[]={1,2,3,4,5};
int max,min=0,i;
max=arr[0];

try {
for(i=1;i<arr.length;i++){
System.out.println("Loop start");
if(arr[i]>max){
min=max;
max=arr[i];
System.out.println("Large->array "+max+"->"+arr[i]);
}
System.out.println("min-> array "+min+"->"+arr[i]);

if(max>arr[i] && arr[i]>min){
System.out.println("I am in second if loop");
min = arr[i];
System.out.println("Now min is "+min);
}

}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Second Large"+min);
}
}

Is This Answer Correct ?    5 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does c mean in basketball?

561


How can a program be made to print the line number where an error occurs?

646


What is array of structure in c?

594


What is echo in c programming?

555


general for is %wd,f-d; in this system "w" means a) 'w' represent total width of digits b) 'w' represent width which includes the digits before,after decimal place and the decimal point c) 'w' represent width which includes the digits before only d) 'w' represent width after decimal place only

584






Was 2000 a leap year?

625


What is the use of gets and puts?

599


What is the size of array float a(10)?

651


How can I use a preprocessorif expression to ?

598


What kind of structure is a house?

551


Can we change the value of static variable in c?

556


Write a program to input the price of 1 burger and the number of burgers eaten by a group of friends .print the total amount to be paid by the group?

574


In c language can we compile a program without main() function?

573


Does free set pointer to null?

555


What is pass by reference in c?

605