Link to home
Start Free TrialLog in
Avatar of Valisha
Valisha

asked on

JAVA arrays and methods

I need help with fixing up  the code that test the method in the main method.
And finding  the index first  occurance of the smallest index. My program  finds the index of the smallest value in the entire array.


I am writing a program that takes the smallestindex that takes the parameters, a int array and it size  and returns the index of the first ocuurene of the smallest value.
{
double[] alpha = new double[50];
                 System.out.println(smallestindex(alpha));
      {
           public static int smallestIndex(int  alpha[])
			int index = 0;
			int  smallest;
			smallest  = data[0];
			for(i =0; i < data.lenght(); i++)
			if(smallest < data[i])
			{
			smallest = data[i];
			index = i;
			}
			return index;

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That's looking ok, apart from the array being empty as shown. What's the problem?
>                         for(i =0; i < data.lenght(); i++)

should be:

                        for(i =0; i < data.length; i++)

> double[] alpha = new double[50];

you also don't initialize your array

double[] alpha = new double[] { 1.2, 4.5, 1.7, 9.0, -3.4, 5.6};

or you could use a loop



Avatar of Valisha
Valisha

ASKER

System.out.println(smallestindex(alpha));
      {
           public static int smallestIndex(int  alpha[])
is this part correct
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial