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
Coconut77840Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.