Faheem
Faheem

Reputation: 42

How to print array if the output is 0?

I solve this question and it's working good but a small problem that if the ray contain only one number or same number it return 0 but i want that number. My code:

public class RaySmallest
{
    public static int go(int[] ray)
    {
          int smallest = 0;
      for(int i =0;i<ray.length;i++) 
      {
            if(smallest > ray[i]) 
            {
                smallest = ray[i];
            }
      } 
      return smallest;
 }
}

The Runner for code

class Main
{
  public static void main(String[] args)
  {
    RaySmallest rt = new RaySmallest();

    System.out.println( rt.go( new int[]{-99,1,2,3,4,5,6,7,8,9,10,12345} ) );
    System.out.println( rt.go( new int[]{10,9,8,7,6,5,4,3,2,1,-99} ) );
    System.out.println( rt.go( new int[]{10,20,30,40,50,-11818,40,30,20,10} ) );
    System.out.println( rt.go( new int[]{32767} ) );
    System.out.println( rt.go( new int[]{255,255} ) );
    System.out.println( rt.go( new int[]{9,10,-88,100,-555,1000} ) );
    System.out.println( rt.go( new int[]{10,10,10,11,456} ) );
    System.out.println( rt.go( new int[]{-111,1,2,3,9,11,20,30} ) );
    System.out.println( rt.go( new int[]{9,8,7,6,5,4,3,2,0,-2,-989} ) );
    System.out.println( rt.go( new int[]{12,15,18,21,23,1000} ) );
    System.out.println( rt.go( new int[]{250,19,17,15,13,11,10,9,6,3,2,1,-455} ) );
    System.out.println( rt.go( new int[]{9,10,-8,10000,-5000,1000} ) );

  }
}

Like in line four my code give me 0 but i need there output as 32767.

I need this answers.

-99
-99
-11818
32767
255
-555
10
-111
-989
12
-455
-5000

Thank you

Upvotes: 0

Views: 62

Answers (2)

Nate
Nate

Reputation: 15

Ok, here is part of your code:

 int smallest = 0;
  for(int i =0;i<ray.length;i++) 
  {
        if(smallest > ray[i]) 
        {
            smallest = ray[i];
        }
  } 
  return smallest;

Essentially this tells me, smallest is 0. You compare if 0 > ray[i], which on line 4 is 32767. 0 is not greater than 32767, so smallest is still equal to 0 and returned as 0.

int smallest = 0;
int[] numArray = new int[ray.length];
 for(int i=0;i<ray.length;i++)
 { 
       if(ray.length == 1)
             return ray[i];
       if(smallest > ray[i])
             smallest = ray[i];
       if(ray[i] == ray[ray.length - 1] && i-ray.length > 0 && i==ray.length-2){
             int x = 0;
             for(int x=0;x<ray.length-1;x++){
                  if(ray[i] != ray[x])
                    break;
             }
             if(ray[i] == ray[x])
                  return ray[i];
       }

}
return smallest;

This solution should take care of having a single value in the array, or if all values in the array are equal.

Upvotes: 0

Maxdola
Maxdola

Reputation: 1650

If the smallest number is greater than zero the number isn't actually set and this is why you aren't getting the smallest number if you have only one, which is probably below zero.

public static int go(int[] ray) {
    int smallest = ray.length > 0 ? ray[0] : -1;
    for (int value : ray) {
        if (smallest > value) {
            smallest = value;
        }
    }
    return smallest;
}

Upvotes: 1

Related Questions