CPSC 225 Intermediate Programming Spring 2025

Lab 4: Arrays and Debugging

Due: Thu Feb 27 at the start of lab

Introduction

This lab deals with arrays storing collections. It provides opportunities to practice working out code for manipulating arrays and to practice debugging as well as providing an illustration of how code correctness techniques can be utilized in practice.

About Due Dates

Labs are due at the start of lab on the date listed. It is OK to hand in your files at the very beginning of lab, but you should not be spending part or all of a lab period finishing up the previous week's lab.

To be eligible for revise-and-resubmit, you must turn in something by the due date. Late work and extensions are generally not accepted/granted; see the posted late policy for more information.

About Collaboration and Getting Help

You may work with a partner if you wish. Both partners must actively contribute to the solution! You only need one handin for the group — be sure to put both teammates' names in every file.

Otherwise you may get help but you may not use other resources (friends, neighbors, websites, ChatGPT, etc) to produce answers. See the posted policy on academic integrity for more information.

Handin

To hand in your work:


Exercises

Setup

Two classes have been provided: StringSet, an incomplete and buggy implementation of a set of strings, and StringSetTester, containing a collection of test cases for some of StringSet's methods.

A set is a collection of elements where the key operation is membership — whether or not an element belongs to the set. Duplicates are not allowed in a set, so inserting an element that is already in the set has no effect. Two other operations on sets are common: the union of two sets is a set containing every element present in one or both sets (without duplicates), and the intersection of two sets is a set containing every element present in both sets (without duplicates). For example, for sets { 1, 2, 3, 4 } and { 2, 4, 5 }, the union is { 1, 2, 3, 4, 5 } and the intersection is { 2, 4 }.

Internally StringSet uses an array to hold the elements in the set, and the elements are stored in lexicographic (alphabetical) order within the array to make the contains operation more efficient. This sorted order must be maintained when elements are inserted and removed.


Array Manipulations

Your task in this section is to implement the add operation for StringSet, following the methodology demonstrated in class:


Debugging

The provided StringSet contains bugs, and your task in this part is to find and fix the bugs in the intersection method:

The first step in debugging is to identify the immediate problem:

The next step is to identify the root cause — why is the problem occurring? The goal is to figure out where what is actually happening in the code diverges from what is supposed to be handling. There are several strategies here: reading through the code and carefully reasoning about it, tracing through the code by hand with an example, and printing out values to assist with tracing through an example. Reading and reasoning is a good start, but that's not always enough to locate the problem. So, start with tracing through an example by hand:

This should reveal what is actually happening and why the exception occurs. But what is supposed to happen? intersection is based on a merge operation: imagine two fingers, each starting pointing at the first element of their respective arrays. Then repeatedly compare the pointed-at elements, possibly take one of the elements as the next thing in the intersection, and move one or both of the fingers one spot to the right. Making sure the right things get added to combo and the fingers are moving along properly is the task of debugging.


If You Have Time (Optional)

If you have time, work on the following (in any order):