Solved

JAVA arrays and methods

Posted on 2009-05-18
5
312 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:Valisha
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24416924
That's looking ok, apart from the array being empty as shown. What's the problem?
0
 
LVL 92

Expert Comment

by:objects
ID: 24416957
>                         for(i =0; i < data.lenght(); i++)

should be:

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

0
 
LVL 92

Expert Comment

by:objects
ID: 24416969
> 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



0
 

Author Comment

by:Valisha
ID: 24417387
System.out.println(smallestindex(alpha));
      {
           public static int smallestIndex(int  alpha[])
is this part correct
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24417444
try this:

public class Smallest {

      public static void main(String[] args)
      {
            int[] alpha = new int[] { 1, 4, 1, 9, -3, 5};
            System.out.println(smallestIndex(alpha));
      }

      public static int smallestIndex(int[] data) {
            int index = 0;
            int smallest;
            smallest = data[0];
            for (int i = 0; i < data.length; i++)
                  if (smallest < data[i]) {
                        smallest = data[i];
                        index = i;
                  }
            return index;
      }

}


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Impossible to extract MSI from new JAVA releases 2 47
Cipher Configuration on Apache HTTPD 4 50
eclipse shortcuts 9 45
object oriented programming comparison 5 52
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now