Guided Practice 8.3

Modify 08-2-binary-search.java to design the following method:

    static int count_occurrences (int[] A, int tgt) {

        // GIVEN: A non-decreasing array A of integers and an integer
        // target 'tgt'
        // RETURNS: the number of occurrences of tgt in A.

Remember that your invariants, halting measure, and your justification of the halting measure are at least as important as your code.

[ANSWER]

NOTE: To solution uses the PdpTestSuite class. We will be using this class, or something like it, for testing in the Java portion of the course. To run the solution, you will need to have an up-to-date JDK installed. Download solution.java and PdpTestSuite.java into the same directory. Then compile and run solution.java according to the instructions at the top of that file.


Last modified: Fri Oct 27 08:51:57 Eastern Daylight Time 2017