Lab 2: C++ Bug Hunt (4 pts)

Chris Tralie

Due Tuesday 2/8

Overview / Logistics

The purpose of this lab is to give you practice in debugging programs in C++, which is much harder than debugging programs in Java because of all of the issues that can arise with memory. You will have to think like a detective and to use the forensic tools you have available to you (print statements or lldb) to help you. Correct each program and submit the corrected versions on Canvas. You will receive 1 point for each corrected program, for a total of 4 points for this lab.

Learning Objectives

  • Work with pointers in C++
  • Use debuggers and print statements to analyze the behavior of a buggy program.

Buggy Programs

To obtain the 4 buggy programs, type

git clone https://github.com/ursinus-cs174-s2022/Lab2_BugHunt.git

All 4 programs will compile, but they all have runtime errors. You will have to tweak them to get them to perform the desired behavior without crashing or giving an incorrect result.

When you are finished, submit the corrected programs to canvas, as well as a brief description of the bugs that you found in each one.

NOTE: There may be more than one bug in a single program!

Bug1.cpp

This program is supposed to compute and print the average of the numbers 1 through 10, which is 5.5.

Bug2.cpp

This program should filter out the multiples of 6 in the array

5, 6, 12, 20, 18, 24, 48, 58, 60, 68

which should leave the array

6, 12, 18, 24, 48, 60

Bug3.cpp

This program initializes the first 20 numbers counting by 2, starting at 2, so

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40

Then, it's supposed to take the average of every pair of adjacent numbers and create a new array

3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39

Bug4.cpp

This program takes one command line argument, K. It starts counting from K in increments of K until there are 10 numbers. For instance, if you call this program with a 4

./Bug4 4

Then you should get the following numbers

4, 8, 12, 16, 20, 24, 28, 32, 36, 40