Lab 7: C++ Bug Hunt (2 pts)
Chris Tralie
Due Monday 11/16
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, lldb or gdb) to help you. Correct each program and submit the corrected versions on Canvas. You will receive 1/2 of a point for each corrected program, for a total of 2 points for this lab. NOTE: There may be more than one bug in a program.
Learning Objectives
- Work with pointers in C++
- Use debuggers and print statements to analyze the behavior of a buggy program.
Buggy Programs
Click here to download a zip file with the 4 buggy programs. 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.
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, calling this program with a 4 should yield the numbers
4, 8, 12, 16, 20, 24, 28, 32, 36, 40