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
Explain what is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
What is scope rule of function in c?
The difference between printf and fprintf is ?
Explain Function Pointer?
How many bytes is a struct in c?
Explain how does free() know explain how much memory to release?
‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.
Explain what happens if you free a pointer twice?
Explain the use of function toupper() with and example code?
What is meant by 'bit masking'?
Can we access the array using a pointer in c language?
Can a variable be both constant and volatile?
Why do we use c for the speed of light?
How many types of sorting are there in c?
write a C program: To search a file any word which starts with ?a?. If the word following this ?a? starts with a vowel.Then replace this ?a? with ?a? with ?an?. redirect with the output onto an output file.The source file and destination file are specified by the user int the command line.