Anon Dog
Anon Dog

Reputation: 25

Smallest value in an array

I have to find the smallest value in an array and print it, I have the code but I can't connect it with my runner file, its telling me "go(int[]) in RaySmallest cannot be applied to (int,int,int,int,int,int,int,int,int,int,int,int)" for all of my 12 samples.

public class RaySmallest
{
 public static int go(int[] ray)
 {
   {
        int[] numbers = {};
     int smallest = Integer.MAX_VALUE;
        for(int i =0;i<numbers.length;i++) 
        {
        if(smallest > numbers[i]) 
        {
            smallest = numbers[i];
        }
        }
    System.out.println("Smallest number in array is : " +smallest);
    }
  return 0;
 }
}

Thats my runner file

public class SmallestRunner
{
 public static void main( String args[] )
 {
  RaySmallest.go(-88,1,2,3,4,5,6,7,8,90,10,5);
  System.out.println();

  RaySmallest.go(10,9,8,7,6,5,4,3,2,1,-99); 
  System.out.println();

  RaySmallest.go(10,20,30,40,50,-11818,40,30,20,10);
  System.out.println();

  RaySmallest.go(65478);
  System.out.println();

  RaySmallest.go(578,578);
  System.out.println();

  RaySmallest.go(6,13,-98,100,-987,7);
  System.out.println();

  RaySmallest.go(9,9,9,13,567);
  System.out.println();

  RaySmallest.go(-222,1,5,6,9,12,29,1);
  System.out.println();

  RaySmallest.go(9,8,7,6,5,4,3,2,0,-2,6);
  System.out.println();

  RaySmallest.go(12,15,18,21,23,1000);
  System.out.println();

  RaySmallest.go(250,19,17,15,13,11,10,9,6,3,2,1,0);
  System.out.println();

  RaySmallest.go(9,10,-8,10000,-5000,-3000);
  }
 }

Upvotes: 0

Views: 409

Answers (1)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236140

Your method expects an int[] as a parameter: int go(int[] ray). An easy way to pass it would be to do this:

RaySmallest.go(new int[] {6, 13, -98, 100, -987, 7});

Alternatively, you can define the go method in such a way that it expects multiple parameters, like this:

int go(int... ray)

Now you can call it as you were doing before, without problems:

RaySmallest.go(6, 13, -98, 100, -987, 7);

Also, there's another bug in your code: the go() method is not using the ray parameter, instead it's traversing the numbers array, which is empty. For a quick fix, do this:

int[] numbers = ray;

Upvotes: 2

Related Questions