-3

Hello I am having a hard time with this assignment. Can someone please let me know why my test number 5 is still saying true when its suppose to be false. Thanks.

This is the question.

Arrays – has12 & linearIn

  1. Given an array of ints, return true if there is a 1 in the array with a 2 somewhere later in the array.

  2. Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order.

This is my output/problem

Test 1: true
Test 2: true
Test 3: false
Test 4: true
Test 5: true//needs to be false
Test 6: true

This is my code

public class Problems {
 public static void main(String[] args) {
  int[] a = {1,3,2};
  int[] b = {3,1,2};
  int[] c = {3,1,4,5};
  int[] d = {1,2,4,6};
  int[] e = {1,2,4,4,6};
  int[] f = {2,4};
  int[] g = {2,3,4};

  boolean test1 = has12(a);
  boolean test2 = has12(b);
  boolean test3 = has12(c);

  System.out.println("Test 1: " + test1); //should print true
  System.out.println("Test 2: " + test2); //should print true
  System.out.println("Test 3: " + test3); //should print false
  System.out.println("Test 4: " + linearIn(d, f)); //should print true
  System.out.println("Test 5: " + linearIn(d, g)); //should print false
  System.out.println("Test 6: " + linearIn(e, f)); //should print true

 }

 //has12 method goes here

 public static boolean has12(int[] array) {
  int i;
  int x;

  for(x=0;x<array.length;x++){
      if(array[x]==1){
      }
    for(i=x+1;i<array.length;i++){
        if (array[i]==2) return true;
    }      
  }
     return false;
 }
  //linearIn method goes here
  public static boolean linearIn(int[] outer, int[] inner) {
      int i;
      int j;
   for (i = 0; i < inner.length; i++) {
    for (j =0; j < outer.length; j++) {
     if (outer[j] == inner[i]) return true;
     
   }
   
  }
      return false;

  }
}
0

1 Answer 1

3

Problem come up this point:

  if (outer[j] == inner[i]) return true;

If there is any match, function returns true and it is not the thing which you want. Because you should check all variables of the inner array.

  • You should check isAppear boolean for every inner variable.
  • If there is no match you can return false.
  • If there is match, continue to check other variables.
  • If there is no variable and everything matches with outer. You can return true.

You can try this:

public static boolean linearIn(int[] outer, int[] inner) {
    boolean isAppear;
    for (int i = 0; i < inner.length; i++) {
        isAppear = false;
        for (int j = 0; j < outer.length; j++) {
            if (outer[j] == inner[i])
                isAppear = true;
        }
        if(!isAppear)
            return false;
    }
    return true;
}
2
  • Oh ok gotcha! Thank you very much for your help! Commented Mar 25, 2017 at 22:35
  • No problem, good to hear help you.
    – ssorfonos
    Commented Mar 25, 2017 at 22:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.