lab03 - Fun with shapes: Pointers and Structs

Collaboration policy

This lab must be completed individually

Goals of this lab

The goal of this lab is to demonstrate how we can abstract “things” in the world (such as geometric objects) into program contructs. We also hope that you will get a lot more practice with using pointers, passing pointers to functions and using pointers along with structs. Y

You will continue to use the test-driven development (TDD) process to develop high quality and well-tested code in a systematic manner. When you run into problems with your code, remember to use the skills we have been learning such as code tracing and drawing pointer diagrams, to understand the dynamics of your program and how it is interacting with memory.

Step by Step Instructions

Step 1: Getting Ready

  1. Go to github and find a repo for lab03 assigned to your GitHub id.

  2. Log on to your CSIL account.

  3. Change into your ~/cs16 directory

  4. Clone your empty lab03 repo into your ~/cs16 directory.

  5. In your empty repo, do git checkout -b main to establish that you are on the main branch as your default branch.

Step 2: Obtain the starter code

The starter code is in this repo:

The URL for cloning this repo is this: git@github.com:ucsb-cs16-f23/STARTER-lab03.git

Previous labs contain instruction for the process of:

Please do those steps now, and then do a git push origin main to populate your own repo with the starter code.

If you need help with these steps:

Once you’ve populated your repo, typing the ls command should show you the following files in your current directory

$ ls
Makefile			pointsApproxEqualTest.cpp
README.md			shapeFuncs.cpp
areaOfBoxTest.cpp		shapeFuncs.h
boxesApproxEqualTest.cpp	shapes.h
distanceBetweenTest.cpp		tddFuncs.cpp
initBoxTest.cpp			tddFuncs.h
initPointTest.cpp		utility.cpp
pointToStringTest.cpp		utility.h
$ 

Step 3: Reviewing the files and what your tasks are

Here is a brief description of each of the files and expected implementation.

Note that some of the files are organized in pairs of matching .h/.cpp files:

The shapes.h file is different; it isn’t paired with any .cpp file. That’s because it contains struct declarations used in the other files.

Finally, there are also a variety of .cpp files that have the suffix Test in their names. Each of these contains a main function, and code that runs our tests. These are the same tests that run on Gradescope.

Here’s an overview of all of the files and their purpose:

Here are some further notes about the files that end in Test.cpp:

The rationale behind this approach is that each function in shapeFuncs.cpp can be developed and tested independently as much as possible.

Tasks for this lab

Here is a list of your tasks for this lab. You will see that we are working on one function at a time, seeing the tests fail, then editing code to see the tests pass.

(1) Get distanceBetween working

There is no code to commit for this step; this is just a step for you to read some code and understand it.

(3) Get initPoint working

(4) Understand your code by drawing a picture

Now, as preparation for homework and exam questions, reason about why your code works.

(5) Get boxesApproxEqual working

(5) Get initBox working

(6) Get areaOfBox working

(7) Get boxToString tests set up

(8) Get boxToString working

Even if you now got 100 on Gradescope, please now check your work before submitting.

Step 4: Checking your work before submitting

When you are finished, you should be able to type make clean and then make tests and see the following output:

-bash-4.2$ make clean
/bin/rm -f distanceBetweenTest initPointTest pointsApproxEqualTest boxesApproxEqualTest initBoxTest areaOfBoxTest pointToStringTest *.o
-bash-4.2$ make tests
g++ -Wall -Wno-uninitialized   -c -o distanceBetweenTest.o distanceBetweenTest.cpp
g++ -Wall -Wno-uninitialized   -c -o tddFuncs.o tddFuncs.cpp
g++ -Wall -Wno-uninitialized   -c -o utility.o utility.cpp
g++ -Wall -Wno-uninitialized   -c -o shapeFuncs.o shapeFuncs.cpp
g++ -Wall -Wno-uninitialized  distanceBetweenTest.o tddFuncs.o utility.o shapeFuncs.o -o distanceBetweenTest
g++ -Wall -Wno-uninitialized   -c -o initPointTest.o initPointTest.cpp
g++ -Wall -Wno-uninitialized  initPointTest.o tddFuncs.o utility.o shapeFuncs.o -o initPointTest
g++ -Wall -Wno-uninitialized   -c -o pointsApproxEqualTest.o pointsApproxEqualTest.cpp
g++ -Wall -Wno-uninitialized  pointsApproxEqualTest.o tddFuncs.o utility.o shapeFuncs.o -o pointsApproxEqualTest
g++ -Wall -Wno-uninitialized   -c -o boxesApproxEqualTest.o boxesApproxEqualTest.cpp
g++ -Wall -Wno-uninitialized  boxesApproxEqualTest.o tddFuncs.o utility.o shapeFuncs.o -o boxesApproxEqualTest
g++ -Wall -Wno-uninitialized   -c -o initBoxTest.o initBoxTest.cpp
g++ -Wall -Wno-uninitialized  initBoxTest.o tddFuncs.o utility.o shapeFuncs.o -o initBoxTest
g++ -Wall -Wno-uninitialized   -c -o areaOfBoxTest.o areaOfBoxTest.cpp
g++ -Wall -Wno-uninitialized  areaOfBoxTest.o tddFuncs.o utility.o shapeFuncs.o -o areaOfBoxTest
g++ -Wall -Wno-uninitialized   -c -o pointToStringTest.o pointToStringTest.cpp
g++ -Wall -Wno-uninitialized  pointToStringTest.o tddFuncs.o utility.o shapeFuncs.o -o pointToStringTest
./distanceBetweenTest
PASSED: distanceBetween(p1,p2)
PASSED: distanceBetween(p2,p1)
PASSED: distanceBetween(p3,p4)
PASSED: distanceBetween(p4,p5)
PASSED: distanceBetween(p5,p3)
./initPointTest
PASSED: pointsApproxEqual(p1,p1Expected)
PASSED: pointsApproxEqual(p2,p2Expected)
PASSED: pointsApproxEqual(p3,p3Expected)
PASSED: pointsApproxEqual(p4,p4Expected)
./pointsApproxEqualTest
PASSED: pointsApproxEqual(p1,p1)
PASSED: pointsApproxEqual(p1,p2)
PASSED: assertFalse(pointsApproxEqual(p2,p1)
./boxesApproxEqualTest
PASSED: boxesApproxEqual(b0,b0)
PASSED: boxesApproxEqual(b1,b0)
PASSED: boxesApproxEqual(b0,b1)
PASSED: boxesApproxEqual(b0,b2)
PASSED: boxesApproxEqual(b0,b3)
PASSED: boxesApproxEqual(b0,b4)
PASSED: boxesApproxEqual(b5,b6)
PASSED: boxesApproxEqual(b6,b5)
./initBoxTest
PASSED: boxesApproxEqual(b1,b1Expected)
PASSED: boxesApproxEqual(b2,b2Expected)
PASSED: boxesApproxEqual(b1,b2)
./areaOfBoxTest
PASSED: areaOfBox(r)
PASSED: areaOfBox(s)
PASSED: areaOfBox(t)
PASSED: areaOfBox(u)
./pointToStringTest
PASSED: pointToString(p1)
PASSED: pointToString(p2)
PASSED: pointToString(p2,1)
PASSED: pointToString(p2,4)
PASSED: pointToString(p2,5)
-bash-4.2$

Plus, some output at the end with the output of your boxToStringTest

./boxToStringTest
PASSED: boxToString(b1,1)
PASSED: boxToString(b1,2)
PASSED: boxToString(b1,3)
PASSED: boxToString(b1,4)
PASSED: boxToString(b1,5)
PASSED: boxToString(b1,6)
-bash-4.2$

At that point, if you haven’t already, submit on Gradescope

Step 5: Checking your code style

Please check that you have followed these style guidelines:

  1. Indentation is neat, consistent and follows good practice (see below)
  2. Variable name choice: variables should have sensible names. More on indentation: Your code should be indented neatly. Code that is inside braces should be indented, and code that is at the same “level” of nesting inside braces should be indented in a consistent way. Follow the examples from lecture, the sample code, and from the textbook.

Your submission should be on-time. If you miss the deadline, you are subject to getting a zero. The instructor may or may not extend the deadline for late submissions at their discretion.