• Register
0 votes
50 views
I am new at java programing, I have an assginment about how to find largest number in array and I start coding on my own and i wrote a program given bellow :
int size ;
int array[] = {10, 20, 25, 63, 96, 57};
      size = array.length;

for(int i = 0; i<size; i++ ){


  for(int j = i+1; j<size; j++){
    if(array[i]>array[j]){
size = array[i];
}

}
System.out.println("Third largest element is:: "+array[size])


}

but it is not running, what should I do now?

6.1k points

Please log in or register to answer this question.

1 Answer

0 votes
Compare the first two elements of the array
If the first element is greater than the second swap them.
Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.
Repeat this till the end of the array.
After sorting an array print the 1st element from the end of the array.
After sorting an array print the 1st element from the end of the array:
int temp, size;
      int array[] = {10, 20, 25, 63, 96, 57};
      size = array.length;

      for(int i = 0; i<size; i++ ){
         for(int j = i+1; j<size; j++){
            if(array[i]>array[j]){
               temp = array[i];
               array[i] = array[j];
               array[j] = temp;
            }
         }
      }
      System.out.println("Third largest element is:: "+array[size-1]);

 

35k points