Lab 7: Pointers and Structures

Due: Wednesday, August 11, 2021 (11:59 PM PDT)

Introduction

The goal of this lab is to demonstrate how we can abstract “things” in the world (such as geometric objects) into program constructs. We also hope that you will get a lot more practice with using pointers, passing pointers to functions and using pointers along with structs. You will use the TDD process to develop high quality and well-tested code in a systematic manner. Lastly, we would like you to delve deep and have fun! When you run into problems with your code, remember to use the skills we have been learning in class, such as code tracing and drawing pointer diagrams, to understand the dynamics of your program and how it is interacting with memory. This lab is more involved than previous ones so start early!

We will also be grading for meeting requirements, using “class legal” code, and plagiarism. So, it is not enough for your lab to just pass the Gradescope autograder! Please read the instructions herein carefully.

It is highly recommended that you develop the algorithms for each program first and then develop the C++ code for it.

Step 1: Getting ready

Either through a graphical file system explorer or through the terminal:

  1. Navigate to your cs16 directory:

    $ cd cs16
  2. Create and navigate to the lab7 directory:

    $ mkdir lab7
    $ cd lab7
  3. Download the starter files to your lab7 directory. Either:

    1. Download these starter files from the course website next to the Lab 7 instructions link: http://sites.cs.ucsb.edu/~zsisco/cs16/#labs

    2. Or, if you are using CSIL, run the following commands from your lab7 directory:

    $ cp ~zsisco/public_html/cs16/lab7-files.zip . 
    
    $ unzip lab7-files.zip

Step 2: Create and edit your C++ programs

Here is a brief description of each of the files and expected implementation. Note that the .h/.cpp files with the same name are a pair. All the function declarations should be placed in the .h files. The definition of the functions that contains your implementation should go in the corresponding .cpp file.

Here is a list of your tasks for this lab:

distanceBetween()

  1. Run make and see the given code being compiled.
  2. Run ./distanceBetweenTest and see it fail.
  3. Edit the distanceBetween function in shapeFuncs.cpp to replace with correct code.
  4. Run ./distanceBetweenTest and see it pass.
  5. Commit and push your code to GitHub.

pointsApproxEqual()

  1. Run ./pointsApproxEqualTest and see it pass.
  2. Look at the code in pointsApproxEqualTest.cpp and shapeFuncs.cpp and understand how it works. Notice how the pointsApproxEqual() function uses the distanceBetween() function that you just wrote and tested, rather than writing new code within pointsApproxEqual() that repeats the logic of distanceBetween(). The takeaway here is that you want to keep your code as DRY as possible (DRY == Don’t Repeat Yourself). You also want to only reuse code that has already been tested. You’ll need to understand pointsApproxEqual() to get ./boxesApproxEqual to pass.

initPoint()

  1. Run ./initPointTest and see it fail.
  2. Looking at the test code in initPointTest.cpp figure out what the initPoint function is supposed to do and add preconditions and postconditions as comments to the start of that function. See page 275 of the book for more information on writing pre and post conditions.
  3. Edit the initPoint function in shapeFuncs.cpp to replace the stub with correct code.
  4. Run ./initPointTest and see it pass.
  5. Now reason about why your code works. Do this by drawing a pointer diagram that shows the state of memory right before the initPoint function returns when it is called for the very first time by the test code. Your pointer diagram should show the value of member variables x and y of the struct object p1 in initPointTest.cpp as well as the relationship between p1 and the formal parameter p of the function initPoint. You should also show the formal parameters xVal and yVal in memory and indicate whether or not they are colocated in memory with any other variables (such as x and y). Make the drawing on a piece of paper or as ASCII art in a text file and upload it to your git repo and Gradescope with the filename: pointer-diagram-initPoint. The diagram will be graded manually by us.

boxesApproxEqual()

  1. Run ./boxesApproxEqualTest and see it fail.
  2. Edit the boxesApproxEqual function in shapeFuncs.cpp to replace the stub with correct code. As you do, consider adding an approxEqual function that takes two double values into utility.h and utility.cpp, as this will make your coding job easier, and keep you code “DRYer”. Also, consider reusing the pointsApproxEqual function in your boxesApproxEqual solution. Remember that the && operator is the symbol for “logical and” in C++.
  3. Run ./boxesApproxEqualTest and see it pass.
  4. Reason about why your code worked, draw a diagram to show the relationship between the formal and actual parameters. You don’t need to submit the diagram but you may be asked to draw such a diagram on an exam!
  5. Commit and push your code to GitHub.

initBox()

  1. Run ./initBoxTest and see it fail.
  2. Edit the initBox function in shapeFuncs.cpp to replace with correct code. As you do, remember that you use -> to access members of a struct through a pointer, but simply . to access members of a struct directly. You may need both in your answer.
  3. Run ./initBoxTest and see it pass.
  4. Commit and push your code to GitHub.

areaOfBox()

  1. Run ./areaOfBoxTest and see it fail.
  2. Edit the areaOfBox function in shapeFuncs.cpp to replace with correct code.
  3. Run ./areaOfBoxTest and see it pass.
  4. Commit and push your code to GitHub.

pointToString()

  1. Run ./pointToStringTest and see it it pass.
  2. Copy pointToStringTest.cpp to boxToStringTest.cpp and make tests for the boxToString function. Look in shapeFuncs.cpp at the boxToString function stub for an example of the format you need for boxToString’s return values. Make tests for different precisions, just like pointToString has.
  3. Add code to the Makefile so that boxToString runs. Just follow the model-adding code for boxToStringTest everywhere you see code for pointToStringTest.
  4. Run make.
  5. Commit and push your code to GitHub.

boxToString()

  1. Run ./boxToStringTest and see the tests fail.
  2. Fix the definition of boxToString in shapeFuncs.cpp.
  3. See the test ./boxToStringTest pass.
  4. Commit and push your code to GitHub.

YOU ARE READY TO CHECK YOUR WORK.

Step 3: 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:

$ make clean
/bin/rm -f distanceBetweenTest initPointTest pointsApproxEqualTest boxesApproxEqualTest initBoxTest areaOfBoxTest pointToStringTest *.o
$ 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)

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)

At that point, you are ready to try submitting on Gradescope.

Step 4: Submit your programs for grading

Once you are satisfied your programs are correct, then it’s time to submit them. While working with others is OK, you still must submit your own lab. Even if you’re working with another person, do not copy each other’s code.

Log into Gradescope and select CMPSC 16 under Summer 2021, and navigate to the Lab 7 assignment. Then click on the “Upload Submission” button on the bottom right corner to make a submission.

You will be given the option of uploading files from your local machine or submitting code from a GitHub repo. Follow the steps to upload all .h/.cpp files to Gradescope. Also, please submit your pointer diagram file to Gradescope along with your code. You can resubmit your files as many times as you like before the assignment deadline.