Gettysburg College

CS 111
Computer Science I

Fall 2025

Assignment 10

Due: Thu, Nov 20, by 11:59pm

Part I - Recursive Code Trace

Do this on paper and submit on Wednesday in class.

Consider the recursive method below:

int mystery( int a, int b )
{
    if( a <= b )
    {
        return mystery( a, b - 1 ) + mystery( a + 2, b );
    }
    else 
    {
        return a;
    }
}


Part II - Recursive Algorithms

Create a project RecAlgosApp and write the methods for the problems listed below:

Problem 1 (Description)

Consider the following sequence of integers:

1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th
-2,  0, -1, -3,  3, -5, -1,  7,-17,  ?    ?    ?    ?   -65
This sequence is generated in the following way:

Problem 1 (Recursive sequence)

Write a recursive method   sequence(n)   that computes the n-th integer in the sequence for n >= 1.

Draw the execution of the computation of sequence(6). It is fine to abbreviate in the drawing as seq(...):

For example:
System.out.println("the 6-th number is: " + sequence( 6 ));    // displays: ... is -5
System.out.println("the 8-th number is: " + sequence( 8 ));    //           ... is 7
  

Problem 1 (Testing)

The above test cases are not sufficient.


Problem 2 (Description)

The greatest common divisor of two numbers a and b, denoted by gcd(a, b), is the largest integer that divides evenly both a and b.

The greatest common divisor can be computed efficiently using a recursive algorithm based on the following observations:

For example, to compute gcd(24, 18):
gcd(24, 18)    // the original task
  |
gcd(18, 6)     // by 1st rule; 6 is remanider of 24/18
  |
gcd(6, 0)      // by 1st rule; 0 is remanider of 18/6
  |
  6            // by 2nd rule since b is 0

Note: In Java the remainder from integer division can be computed using the % (percent) operator. That is, 6%4 will equal 2, since 6 divided by 4 leaves a remainder of 2; 24%9 will equal 5, since 24 divided by 9 leaves a remainder of 5.

Problem 2 (Recursive GCD)

Write a recursive method gcd(a, b) which takes two non-negative integers a and b and returns their greatest common divisor.

Draw the execution of the computation for gcd(58, 32):

Problem 2 (Loop-based GCD)

Write a non-recursive (i.e. with loop) method gcdLoop(a, b) which takes two non-negative integers a and b and returns their greatest common divisor.

Hint repeatedly update the given a,b according to the 1st rule and stop when 2nd rule reached.

Problem 2 (GCD Test Cases)

Recursive version:
System.out.println("gcd(24, 18) = " + gcd(24, 18));    // displays: gcd(24, 18) = 6
System.out.println("gcd(8, 13) = " + gcd(8, 13));      //           gcd(13, 8) = ?
Loop version:
System.out.println("gcdLoop(24, 18) = " + gcdLoop(24, 18));    // displays: gcdLoop(24, 18) = 6
System.out.println("gcdLoop(8, 13) = " + gcdLoop(8, 13));      //           gcdLoop(13, 8) = ?

Problem 2 (Testing)

The above test cases are not sufficient.


Part III - Recursive Drawing

Create a project called RecDrawApp and write the following recursive methods:

Part IV - Binary Search

Create a project called BinarySearchApp and write the methods binary search methods described in this section.

Problem 3 (Binary Search Description)

The Binary Search algorithm is an efficient way to find a value in an sorted array.

Here is a set of slides that show how Binary Search works:

Binary Search Algorithm (start the slideshow and slowly go through the animation)

Here is a high-level description of Binary Search:

The task is to search in a "big book" between pages [i,j]. The solution is to check the middle page and if the item is not there, then search in a "smaller book" between pages [?,?] (hint: the words in the book are soted).

What are the Easy/Base Cases? What is the Hard Case?

Problem 3 (Binary Search Test Cases)

Show evidence of thorough testing for the two methods below: binarySearch (the given method) and binarySearchLoop. Specifically, show results for: If the code is written correctly, no special handling should be needed for empty array and one-element array.

Problem 3 (Recursive Binary Search)

Write a recursive method that implements the Binary Search algorithm for finding a value in a sorted list of numbers between the given indices/pages i,j (inclusive):

binarySearch(numbers, value, i, j)

To simplify the code do not check whether the given indices are valid. The method returns true if the value is found in the array; otherwise returns false. For example:

//                   0  1  2  3  4   5   6   7   8     the page numbers    
double[] numbers = { 1, 4, 5, 8, 9, 10, 23, 35, 47 };

System.out.println("Is 5 between pages 0..8? " + binarySearch(numbers, 5, 0, 8));    // displays: ... true
System.out.println("Is 5 between pages 1..6? " + binarySearch(numbers, 5, 1, 6));    //           ... ?
System.out.println("Is 5 between pages 2..6? " + binarySearch(numbers, 5, 2, 6));    //           ... ?
System.out.println("Is 5 between pages 3..7? " + binarySearch(numbers, 5, 3, 7));    //           ... ?     

System.out.println("Is 4 in the array? " + binarySearch(numbers, 4, 0, 8));          //           ... true
System.out.println("Is 25 in the array? " + binarySearch(numbers, 25, 0, 8));        //           ... false
To make it convenient for the user to search in the whole array we provide the following method (copy in your project and fill in):

boolean binarySearch( double[] numbers, double value )
{
    // call the recursive version that takes four parameters
    // we fill in *first_page* and *last_page*

    return binarySearch( numbers, value, _first?_, _last?_ );
}

The method returns true if the value is found in the array; otherwise returns false. For example:

double[] numbers = { 1, 4, 5, 8, 9, 10, 23, 35, 47 };

System.out.println("Is 4 in the array: " + binarySearch(numbers, 4));       // displays: ... true     
System.out.println("Is 25 in the array: " + binarySearch(numbers, 25));     //           ... false

Problem 3 (Loop-based Binary Search)

Implement the Binary Search algorithm without recursion (use a while loop instead).

The idea is similar to the recursive version:

We keep track of two indices, i and j, that represent the first and last "page", respectively, where the item of interest can possibly be. Then, as long as we have not run out of pages we open the book in the middle, check if we have found the item, and if not, update either the first or the last page (index) to eliminate half of the items in the array.

Write a non-recursive (i.e. with a loop) method that implements the Binary Search algorithm for finding a value in a sorted list of numbers:

binarySearchLoop(numbers, value)

For example:

double[] numbers = { 1, 4, 5, 8, 9, 10, 23, 35, 47 };

System.out.println("Is 4 in the array: " + binarySearchLoop(numbers, 4));       // displays: ... true     
System.out.println("Is 25 in the array: " + binarySearchLoop(numbers, 25));     //           ... false

What to turn in

Turn in the Java code for the app in the Assignment 10 dropbox: