Solved

JAVA arrays and methods

Posted on 2009-05-18
5
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 262
going to wrong jsp page 2 66
Glassfish admin console not working 1 85
Running JavaFX on the Raspberry Pi 27 274
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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