• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

IndexOfLargest that returns the largest element

I am to write rite a method indexOfLargest(int[] x) which returns the index of the largest element of x, and a main program to show that it works.  
For
example, if

      int[] x = {2, 1, 5, 3};
      
then indexOfLargest would return 2, since x[2] is 5, the largest element.

This is what I have gotten so far:

<code>
public class indexOfLargest {
      public static void main(String[] args) {
            int[] x = {2, 1, 5, 3};
            
            
            static void selectionSort(int[] x) {
                   for(int i = x.length - 1; i >= 1; i--) {
                     int indexOfLargest = x[0];
                     int currentLargest = 0;
                    
                     for(int j = 1; j <= i; j++) {
                           if(currentLargest < x[0]) {
                                 currentLargest = x[j];
                                 indexOfLargest = j;
                           }
                     }
               
            
             int L = IndexOfLargest(a, Last+1);
             swap(L, Last);
             }
         }
      }
}
</quote>

am I on the right track? I am getting 2 error messages that say:

indexOfLargest.java:6: illegal start of expression
                static void selectionSort(int[] x) {
                ^
indexOfLargest.java:23: ';' expected
        }
        ^
2 errors

Thank you
0
Coconut77840
Asked:
Coconut77840
3 Solutions
 
imladrisCommented:
HMMmmm.

As for the errors, the selectionSort function is being declared *inside* the main function. That is not legal. Overall the layout should be something like:

public class indexOfLargest {
     public static void main(String[] args) {
          int[] x = {2, 1, 5, 3};

           // call other methods to run program
           return;
     }
         
         
     static void selectionSort(int[] x) {
           // perform method activities
     }
}

The other error ( ";" expected) seems to originate from beyond the code posted.
As for whether you're on the right track. It depends on what you're after. For an indexOfLargest method perse I would only expect to need a single loop. It would initialize the "memory values" currentLargest and indexOfLargest, then, in a single loop, scan through the array, and whenever it found something larger than currentLargest, would update both of those. At the end of the loop you would have the largest value and its index.

A selectionSort (which is the name of the method there) is, of course, more complicated than that.
0
 
petmagdyCommented:
code should be:

public class indexOfLargest {
         
          static void selectionSort(int[] x) {
                for(int i = x.length - 1; i >= 1; i--) {
                  int indexOfLargest = x[0];
                  int currentLargest = 0;
                 
                  for(int j = 1; j <= i; j++) {
                       if(currentLargest < x[0]) {
                            currentLargest = x[j];
                            indexOfLargest = j;
                       }
                  }
             
         
           int L = IndexOfLargest(a, Last+1);
           swap(L, Last);
  put the method         static void selectionSort(int[] x)  out of the main method
 
0
 
zzynxSoftware engineerCommented:
Don't know why you sort/swap that array?

/*
 * Test.java
 *
 * Created on 5 november 2004, 16:53
 */

/**
 *
 * @author  zzynx
 */
public class Test {
   
    public static int getIndexOfLargest(int[] array) {
        int max = Integer.MIN_VALUE;
        int indexOfMax = -1;
       
        for (int i=0; i<array.length; i++) {
            if (max < array[i]) {
                max = array[i];
                indexOfMax = i;
            }
        }
        return indexOfMax;
    }
   
    public static void main( String[] args) {
        int[] x = {2, 1, 5, 3};

        System.out.println( Test.getIndexOfLargest(x) );
    }
   
}

Result: 2
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Coconut77840Author Commented:
Thanks guys.
0
 
Coconut77840Author Commented:
zzynx,

Did you sort it recursively?
0
 
zzynxSoftware engineerCommented:
>> Did you sort it recursively?
I told you
>> Don't know why you sort/swap that array?
So, the answer is: no. You just asked for the index of the largest element.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now