HW1: Pseudocode and Recursion Solved

$24.99 $18.99

Q1: Find Missing Number (2 points) Convert the following pseudocode algorithm into Java. You’ll find example tests within the main method to ensure you’re headed in the right direction. Make sure you understand each example. In order to receive full credit for this problem, you must use recursion. I.e. using =, for, while, etc. is…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

Q1: Find Missing Number (2 points)

Convert the following pseudocode algorithm into Java. You’ll find example tests within the main method to ensure you’re headed in the right direction. Make sure you understand each example.

In order to receive full credit for this problem, you must use recursion. I.e. using =, for, while, etc. is prohibited.

Hint: recall the % and / operators:

123 % 10 // evaluates to 3

123 / 10 // evaluates to 12

Q4: pickTrees (3 points)

You build homes out of wood and you need material from a nearby forest. However, you want to avoid deforestation, so you decide for each tree you cut down, you’ll leave its neighbors alone, giving the forest time to recover. However, you still need as much wood as possible, so you have to be careful about which trees you pick to cut down.

Write pickTrees, which takes in an array of N trees arr where arr[i] represents how much wood you can harvest by cutting down tree i. It should return the max amount of wood you can harvest while following the rule of skipping neighbors:

  • Pick tree 0, tree 2, and tree 4 => 1 + 3 + 5 = 9 wood total int testResult5 = pickTrees(new int[] {1, 2, 3, 4, 5}); System.out.println(testResult5); // should output 9

  • Pick tree 1 and tree 3 => 3 + 3 = 6 wood total

int testResult6 = pickTrees(new int[] {1, 3, 4, 3}); System.out.println(testResult6); // should output 6

  • Pick tree 0 and tree 3 => 5 + 9 = 14 wood total int testResult7 = pickTrees(new int[] {5, 1, 4, 9}); System.out.println(testResult7); // should output 14

In order to receive full credit for this problem, you must use recursion. I.e. using =, for, while, etc. is prohibited.

HW1: Pseudocode and Recursion Solved
$24.99 $18.99