• Register
0 votes
15 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?

10 7 2
6,050 points

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]);

 

11 6 4
34,930 points

Related questions

1 vote
1 answer 12 views
12 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
asked Mar 29 Gavin 15.3k points
0 votes
2 answers 85 views
85 views
Problem: I am new at java programming and I used a method: java.io.File>list( which is used to obatain the list of files and and directories in the specified directory defined by its path name. This list of files are stored in a string array. If the length of this string ... .out.println("The directory " + directory.getPath() + " is empty"); } } But it does not seems to work can anyone help?
asked Mar 21 LizzyM 6.1k points
1 vote
1 answer 12 views
12 views
Problem: I am trying to finding the largest number in an array c++ Every time I am trying my code is printing all the values of the array instead of the largest one. I might be doing something wrong inside my iteration. Do you have any idea to do it more easily and efficiently? I&rsquo;d love to hear from you guys. Please shar your ideas about this. Thanks.
asked Mar 29 Gavin 15.3k points
3 votes
1 answer 19 views
19 views
Problem: Hello good souls, I am a new student of Java. I just started learning and trying to solve java problems. Right now, I am working with the arrays. I can do some basic things with an array. I am finding something more advance problems of an array. Thus I thought to find the ... in java? Let&rsquo;s say I have an array Integer[] num = { 2, 4, 6, 5, 8 }; Thanks in advance for your effort.
asked Mar 23 Gavin 15.3k points