0

I have written a method that takes a boolean array as input, and returns the conjunction of all of the values in the array using a loop. However, I am trying to do the same thing except using recursion (no for loops allowed) and am having trouble. Any suggestions or hints? Thanks!

Here is what I have written for the iteration part:

public class LogicalOperators {
  public static void main(String[] args) {
    boolean[] queue = new boolean[] {true, false, true, true, true};
    System.out.println(conjunctionIter(queue));
  }

public static boolean conjunctionIter(boolean[] queue){
  boolean allArrayTrue = true;
  for(int i=0; i<queue.length; i++){
    if(queue[i] == false){
      allArrayTrue = false;
      break;
    }
  }
  return allArrayTrue;
}
1
  • Let me understand your question, you want to and all the values in a boolean array recursively, right? Commented Apr 3, 2016 at 22:45

3 Answers 3

1

An example of a recursive function that effective && all booleans in an array

public static boolean recurse(boolean[] ary)
{
    if (ary.length == 1) {
        return ary[0];
    }

    return ary[0] && recurse(Arrays.copyOfRange(ary, 1, ary.length));
}

Test driver:

public static void main(String[] args)
{
    boolean[] ary = { true, true, true, true, true};

    System.out.println(recurse(ary));

    boolean[] ary2 = { true, true, false, true, true};

    System.out.println(recurse(ary2)); 
}
  • true
  • false
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Kevin, when i try this code, I am getting a Cannot find symbol error. (variable Arrays). Any ideas as to why? Thanks!
@montomono, you need to import java.util.Arrays.
0

You might be looking for something in the line of this :

public class LogicalOperators {
public static void main(String[] args) {
boolean[] queue = new boolean[] {true, false, true, true, true};
System.out.println(conjunctionIter(queue,0));
}

public static boolean conjunctionIter(boolean[] queue,int i){
if(i==queue.length)
    return true;
if(queue[i] == false)
  return false;
  else
return conjunctionIter(queue, i+1);
}
}

Tweak it the way you like. Hope it helped !! :)

1 Comment

Thanks for the response, however, the only input that I am allowed is an array of boolean values but am allowed to have helper methods. Any ideas? thanks!
0

Here's one approach.

public static boolean conjunctionIter(boolean[] queue){
        boolean result = true;
        int index = 0;
        return conjunctionIter(queue, result, index);
    }

    private static boolean conjunctionIter(boolean[] queue, boolean result, int index){

        if(index == queue.length -1)
            return result = result && queue[index];

        return result = conjunctionIter(queue, result, ++index);

    }

Client code.

public static void main(String[] args) {
    // write your code here
        boolean[] queue = new boolean[] {true, false, true, true, true};
        System.out.println(conjunctionIter(queue));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.