lab04 : Odds and primes: Fun with arrays and makefiles

num ready? description assigned due
lab04 true Odds and primes: Fun with arrays and makefiles Mon 04/29 10:15PM Tue 05/07 11:59PM

CS16: Programming Assignment 04

Introduction -- Important: Read this!

The mentors will be looking for (and grading) your programming style, such as proper use of comments, tab indentation, good variable names, and overall block and function designs. So, it is not enough for your lab to pass Gradescope tests! Please read the instructions herein carefully.

You should now have a familiarity with:

  • Creating a git repo on github
  • Cloning your github to your local machine
  • Integrating git command-line tools into your workflow (git add…, git commit.., git push …)

Be sure to commit and push or work to github at the end of EVERY work session.

Step 1: Log on to CSIL and bring up a terminal window.

Log into your account to make sure it works. As a reminder to get to the terminal go to Application Menu, then System Tools, then Terminal Window.

In the steps below, and in most future labs, you will create files on your own account.

Step 2: Create a new repo and clone it to your local directory

In lab02, we have done the same thing. So if you don’t know to how to do that, please refer to lab02 for details. The basic steps are as follows:

  • Create a git repo on github following the correct naming convention, e.g., if your github username is jgaucho, you should name your repo lab04_agaucho. Remember to make it ‘PRIVATE’.

  • Go to your CS16 directory and clone the repo locally using git clone.

Step 3: Get the starter code from a local directory

Copy the skeleton to your local lab04 repo using the following command, REMEMBER to change the directory name in the commands below to your own directory’s name, in this lab we simply use lab04_agaucho as a example for your local git directory:

  cp /cs/faculty/ykk/cs16/labs/lab04/* ~/cs16/lab04_agaucho/

Typing the list (ls) command should show you the following files in your current directory

[-bash-4.2]$ ls
arrayBoundsDemo.cpp    maxOfArray.cpp           sumOfArray.cpp
arrayFuncs.h           maxOfArrayErrorTest.cpp  sumOfArrayTest.cpp
arrayToString.cpp      maxOfArrayTest.cpp       tddFuncs.cpp
arrayToStringTest.cpp  minOfArray.cpp           tddFuncs.h
countEvens.cpp         minOfArrayErrorTest.cpp  utility.cpp
countEvensTest.cpp     minOfArrayTest.cpp       utility.h
countPrimes.cpp        README.md                utilityTest.cpp
countPrimesTest.cpp    sumOdds.cpp              
Makefile               sumOddsTest.cpp
[-bash-4.2]$

Push the initial version of the code to github before making any changes by typing the following commands

git add .
git commit -m "Initial version of Lab 4"
git push origin master

Step 4: Reviewing Separate Compilation

The files in your directory this week use separate compilation, that is each program is not necessarily taking all of its code from a single .cpp source file.

In Lecture, we have introduced the idea of separate compilation, where your C++ program may be divided among multiple source files. The following web page explains more about separate compilation, dividing your program up among multiple C++ and .h files, and using a Makefile: Separate Compilation and Makefiles. I strongly encourage you to read over it briefly before you proceed with the lab.

Step 5: Writing isOdd(), isEven() and isPrime()

We will be looking at both, the separate compilation and C++ coding.

To get started, do the following steps:

Step 5a: make clean

In your working directory, type ls and make note of the different files therein: some are .cpp types, some are .o (short for “object file”), some are .h (short for “header file”), and others do not have extensions (they are binary executables). Now, type make clean. This command cleans out any .o files and executables from your directory.

Running the command should look like this:

-bash-4.2$ make clean
/bin/rm -f arrayToStringTest arrayBoundsDemo countEvensTest minOfArrayTest minOfArrayErrorTest
countPrimesTest maxOfArrayTest maxOfArrayErrorTest sumOddsTest sumOfArrayTest utilityTest *.o

-bash-4.2$

Take a look at the Makefile file to understand why this happened (specifically, find the clean target and examine it’s command).

Step 5b: make utilityTest

This command makes the executable for a main program, defined in utilityTest.cpp, which tests the functions defined in utility.cpp. For functions defined in a file such as utility.cpp that has no main(), the function prototypes are defined in the file utility.h. Look at the source code for both utility.cpp and utility.h to see what they contain. Recall that a “stub” is place-holder code that allows an incomplete function to compile. It is designed to fail all the tests, although, for a Boolean function, since there are only two possible values (true and false), any stub value you choose is going to pass at least some of the tests.

Compile utilityTest executable by running the make utilityTest command:

-bash-4.2$ make utilityTest
g++ -Wall -Wno-uninitialized   -c -o utilityTest.o utilityTest.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  utilityTest.o tddFuncs.o utility.o -o utilityTest

-bash-4.2$

Step 5c: Run ./utilityTest

Next, type ./utilityTest.

This runs the utilityTest program that tests the three functions isOdd, isEven and isPrime. As we noted above, some of the tests will pass, even though the implementation of the three functions is totally bogus (i.e., hard coded to return false always).

Here is what that looks like (some output truncated):

-bash-4.2$ ./utilityTest
	FAILED: isEven(2)
		Expected: 1 Actual: 0
	PASSED: isEven(3)
	FAILED: isEven(4)
		Expected: 1 Actual: 0
	PASSED: isEven(55)

[...      Some output omitted here... ]

PASSED: isPrime(64507)
	FAILED: isPrime(69997)
		Expected: 1 Actual: 0
PASSED: isPrime(-55)
PASSED: isPrime(-80)
PASSED: isPrime(0)
PASSED: isPrime(1)

-bash-4.2$

Step 5d: Repeat: edit, compile, run, until all tests pass

Now do these steps, repeatedly, until all tests pass:

  • edit utility.cpp using a text editor
  • make utilityTest
  • run utilityTest (e.g., ./utilityTest)

Save working versions of your code on github using the commands:

git add *.cpp *.h
git commit -m "implemented utility function - <insert name of function()>"
git push origin master

You must only proceed with the rest of the lab after you have implemented (i.e., written code for) all the utility functions and pass the provided test cases. This is because for other files that you will be editing later, you will NEED functions isOdd, isEven and isPrime. Once you get them working, you will be able to call them in other files and KNOW that they work properly. You will not have to repeat the function definition.

When all the tests for utlityTest pass, do a final push to github (repeat the above 3 git commands) and move on to the next step.

DO NOT proceed to the next step until you finished the steps above.


Step 6: Reviewing the rest of the files and your tasks

Now, let us look at the files you have in your directory, and what you need to do with them. You have the following .cpp files. This table indicates what you must do with each one to get full credit on this lab.

Filename Your task Details
arrayBoundsDemo.cpp NOTHING TO CHANGE OR SUBMIT. This is here as example code only. You are encouraged to run it, study it, and learn about how array bounds work in C++
arrayToString.cpp NOTHING TO CHANGE OR SUBMIT. This code is part of your solution, but you do not have to submit it - we will use our own version, which matches the one in your sample directory. This file just has utiltiy funcitons for printing arrays as strings.
arrayToStringTest.cpp NOTHING TO CHANGE OR SUBMIT. This code is part of your solution, but you do not have to submit it - we will use our own version, which matches the one in your sample directory. This file is an example of how to test cases to determine whether the output of arrayToString works correctly.
countEvens.cpp REPLACE STUB WITH CORRECT CODE. You must replace the code in this file with correct code that returns the number of even integers in each array passed in. You will want to #include "utility.h" at the top of your file to use your completed function from step 5.
countEvensTest.cpp NO MODIFICATIONS NEEDED This tests the changes you made in countEvens.cpp.
countPrimes.cpp REPLACE STUB WITH CORRECT CODE. You must replace the code in this file with correct code that returns the number of prime integers in each array passed in. Treat negative numbers, 0 and 1 as “not prime”. You will want to #include "utility.h" at the top of your file to use your completed function from step 5.
countPrimesTest.cpp NO MODIFICATIONS NEEDED This tests the changes you made in countPrimes.cpp.
maxOfArray.cpp REPLACE STUB WITH CORRECT CODE. You can look at minOfArray.cpp for hints. This one should be easy.
maxOfArrayErrorTest.cpp REPLACE EMPTY MAIN WITH TESTS. Insert code to call maxOfArray with zero length array. Use minOfArrayErrorTest.cpp as a model.
maxOfArrayTest.cpp REPLACE EMPTY MAIN WITH TESTS. Insert code to call assertEqual exactly seven times testing whether maxOfArray returns correct values. Use minOfArrayTest.cpp as a model. It must be exactly “seven” calls to assertEquals to pass the Gradescope tests. You should call your arrays the same things that they are called in minOfArrayTests, and the lengths should be the same. So the messages you get out for passed tests should match the messages from minOfArrayTests except that the name of the function is maxOfArray instead of minOfArray. You MAY change the values in the arrays themselves, though, to make the tests better tests, if you need to. (Note that just hard coding a program that prints “PASSED” seven times with the apprpriate messages is not sufficient to get credit–you need to really have actual tests. Any attempt to “game the system”, i.e. to get Gradescope tests to pass without a bona-fide attempt to actually solve the problem will get zero credit.)
minOfArray.cpp NOTHING TO CHANGE. This is a model of correct code that can serve as a hint for how to write maxOfArray.cpp
minOfArrayErrorTest.cpp NOTHING TO CHANGE. This is an model of correct code for how to test whether a function behaves as expected when given input that should print a message to cerr and exit the progrm.
minOfArrayTest.cpp NOTHING TO CHANGE. This is a model of how to do unit testing on a function that returns an integer.
sumOdds.cpp REPLACE STUB WITH CORRECT CODE. You must replace the code in this file with correct code that returns the number of sum of the odd integers in each array passed in. Negative odd integers count as odd integers.
sumOddsTest.cpp REPLACE EMPTY MAIN WITH TESTS Insert code to call assertEqual exactly seven times testing whether sumOdds returns correct values. Use sumOfArrayTest.cpp as a model. It must be exactly “seven” calls to assertEquals to pass the Gradescope tests. You should call your arrays the same things that they are called in sumOfArrayTests, and the lengths should be the same. So the messages you get out for passed tests should match the messages from sumOfArrayTests except that the name of the function tested is sumOdds instead of sumOfArray. You MAY change the values in the arrays themselves, though, to make the tests better tests, if you need to. (Note that just hard coding a program that prints “PASSED” seven times with the apprpriate messages is not sufficient to get credit–you need to really have actual tests. Any attempt to “game the system”, i.e. to get Gradescope tests to pass without a bona-fide attempt to actually solve the problem will get zero credit.)
sumOfArray.cpp REPLACE STUB WITH CORRECT CODE. You must replace the code in this file with correct code that returns the sum of the integers in each array passed in.
sumOfArrayTest.cpp NOTHING TO CHANGE. This is a set of tests to verify whether sumOfArray() works correctly.
tddFuncs.cpp NOTHING TO CHANGE. These are two functions that can be used to test functions that return either int or string values.
utility.cpp ADD FUNCTIONS HERE AS NEEDED. If you need to write your own helper functions, e.g. isPrime, isOdd, isEven, to use in other files, here is where you can put those definitions.

Step 7: Actually Getting Started

I suggest you start by typing: make

You should see a lot of activity as programs are compiled. You then will have a lot of executables you can run. Here is a list. Run each one and see what happens.

Note these are the programs listed under BINARIES in the Makefile.

file Anything to do? explanation
arrayToStringTest no Run this and all tests should pass. Nothing to do here.
arrayBoundsDemo no Run this, and look at the code. This is an opportunity to learn something about how we pass arrays to functions in C++, but there is nothing you have to turn in from this program for the lab. It is just here as an example for you to learn from.
countEvensTest YES Run this, and you will see all the tests fail. YOU NEED TO FIX THE countEvens function and then get all these tests to pass. You will want to #include "utility.h" at the top of your file to use your completed functions.
minOfArrayTest no Just run this and see the tests pass. You can use the .cpp file minOfArrayTest.cpp as a model for writing maxOfArrayTest.cpp
minOfArrayErrorTest no Just run this and see the output. It should be ERROR: minOfArray called with size < 1 printed on cerr (the standard error output stream). The Gradescope system will check this as one of the acceptance tests for this lab, and it will also check that maxOfArrayErrorTest does the same thing. You can use the .cpp file minOfArrayErrorTest.cpp as a model for writing maxOfArrayErrorTest.cpp
countPrimesTest YES Run this, and you will see all the tests fail. YOU NEED TO FIX THE countPrimes function and then get all these tests to pass.
maxOfArrayTest YES Run this, and you will see that initially there is no output. That is because the main is empty. YOU NEED TO REPLACE THIS MAIN with code that tests maxOfArray. Use minOfArrayTest as a model. Initially, just put in the tests, and keep maxOfArray returning the stub vaue -42. See all the tests fail. Then get maxOfArray to return the right values and see all the tests pass.
maxOfArrayErrorTest YES Run this, and you will see that initially there is no output. That is because the main() is empty. YOU NEED TO REPLACE THIS MAIN with code that tests maxOfArray. Use minOfArrayTest as a model. Initially, just put in the tests, and keep maxOfArray returning the stub vaue -42. See all the tests fail. Then get maxOfArray to return the right values and see all the tests pass. You will need to use assertTrue() and assertEquals() functions from Step 6.
sumOddsTest YES Run this, and you will see that initially there is no output. That is because the main() is empty. YOU NEED TO REPLACE THIS MAIN with code that tests sumOdds. Use minOfArrayTest as a model. Initially, just put in the tests, and keep sumOdds returning the stub vaue -42. See all the tests fail. Then get sumOdds to return the right values and see all the tests pass.
sumOfArrayTest YES Run sumOfArrayTest and you will see that all the tests fail. Getting them to pass is probably the easiest step in this lab. Just look at the sumOfArray function, which is almost correct - it just needs you to initialize sum correctly. Note that in C/C++ variables are NOT automatically initialized, and failing to initialize them does not always result in an error message or warning unless you specifically ask the compiler to tell you about those. For this lab, the Makefile deliberarly turns that warning OFF so that we have to catch that ourselves.


So, if you go through this list, and do all the things indicated, you are finished with the lab and ready to submit.

Step 8: Checking your work before submitting

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

-bash-4.2$ make tests
./arrayToStringTest
PASSED: arrayToString(fiveThrees,5)
PASSED: arrayToString(zeros,3)
PASSED: arrayToString(empty,0)
PASSED: arrayToString(primes,10)
PASSED: arrayToString(meaning,1)
PASSED: arrayToString(mix,10)
./countEvensTest
PASSED: countEvens(fiveThrees,5)
PASSED: countEvens(zeros,3)
PASSED: countEvens(fiveInts,5)
PASSED: countEvens(empty,0)
PASSED: countEvens(primes,10)
PASSED: countEvens(meaning,1)
PASSED: countEvens(mix,10)
./countPrimesTest
PASSED: countPrimes(fiveThrees,5)
PASSED: countPrimes(zeros,3)
PASSED: countPrimes(fiveInts,5)
PASSED: countPrimes(empty,0)
PASSED: countPrimes(primes,10)
PASSED: countPrimes(meaning,1)
PASSED: countPrimes(mix,10)
./maxOfArrayTest
PASSED: maxOfArray(fiveThrees,5)
PASSED: maxOfArray(zeros,3)
PASSED: maxOfArray(fiveInts,5)
PASSED: maxOfArray(fiveInts,2)
PASSED: maxOfArray(fiveInts,3)
PASSED: maxOfArray(meaning,1)
PASSED: maxOfArray(mix,10)
./minOfArrayTest
PASSED: minOfArray(fiveThrees,5)
PASSED: minOfArray(zeros,3)
PASSED: minOfArray(fiveInts,5)
PASSED: minOfArray(fiveInts,2)
PASSED: minOfArray(fiveInts,3)
PASSED: minOfArray(meaning,1)
PASSED: minOfArray(mix,10)
./sumOddsTest
PASSED: sumOdds(fiveThrees,5)
PASSED: sumOdds(zeros,3)
PASSED: sumOdds(fiveInts,5)
PASSED: sumOdds(fiveInts,3)
PASSED: sumOdds(fiveInts,2)
PASSED: sumOdds(meaning,1)
PASSED: sumOdds(mix,10)
./sumOfArrayTest
PASSED: sumOfArray(fiveThrees,5)
PASSED: sumOfArray(zeros,3)
PASSED: sumOfArray(fiveInts,5)
PASSED: sumOfArray(fiveInts,3)
PASSED: sumOfArray(fiveInts,2)
PASSED: sumOfArray(meaning,1)
PASSED: sumOfArray(mix,10)
./utilityTest
PASSED: isEven(2)
PASSED: isEven(3)
PASSED: isEven(4)
PASSED: isEven(55)
PASSED: isEven(-55)
PASSED: isEven(-80)
PASSED: isOdd(2)
PASSED: isOdd(3)
PASSED: isOdd(4)
PASSED: isOdd(55)
PASSED: isOdd(-55)
PASSED: isOdd(-80)
PASSED: isPrime(2)
PASSED: isPrime(3)
PASSED: isPrime(4)
PASSED: isPrime(55)
PASSED: isPrime(859)
PASSED: isPrime(861)
PASSED: isPrime(863)
PASSED: isPrime(1337)
PASSED: isPrime(1373)
PASSED: isPrime(64507)
PASSED: isPrime(69997)
PASSED: isPrime(-55)
PASSED: isPrime(-80)
PASSED: isPrime(0)
PASSED: isPrime(1)

-bash-4.2$

And, you should be able to type make errorTests and see the following output:

-bash-4.2$ make errorTests
./minOfArrayErrorTest
ERROR: minOfArray called with size < 1
make: [errorTests] Error 1 (ignored)
./maxOfArrayErrorTest
ERROR: maxOfArray called with size < 1
make: [errorTests] Error 1 (ignored)

-bash-4.2$

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

Step 9: Turn in your code on Gradescope

  • Navigate to your lab04 directory, the one containing your code for this week’s lab.
-bash-4.2$ cd ~/cs16/lab04_agaucho
  • Use the ls command to list your files and to be sure that you have all .cpp files (that you received as starter files) in your directory. It is ok if there are other files ( *.txt, *.o, etc.) along with the executables. You only have to submit *.cpp files
-bash-4.2$ cd ~/cs16/lab04_agaucho_alily
-bash-4.2$ ls
arrayBoundsDemo.cpp    maxOfArray.cpp           sumOfArray.cpp
arrayFuncs.h           maxOfArrayErrorTest.cpp  sumOfArrayTest.cpp
arrayToString.cpp      maxOfArrayTest.cpp       tddFuncs.cpp
arrayToStringTest.cpp  minOfArray.cpp           tddFuncs.h
countEvens.cpp         minOfArrayErrorTest.cpp  utility.cpp
countEvensTest.cpp     minOfArrayTest.cpp       utility.h
countPrimes.cpp        README.md                utilityTest
countPrimesTest.cpp    sumOdds.cpp              utilityTest.cpp
Makefile               sumOddsTest.cpp
-bash-4.2$

Submit the modified cpp files to lab04 assignment on Gradescope. Then check that you have a correct score. If you did not receive the full score, modify your code to fix the issue(s) and re-submit the files again.

  • You must check that you have followed these style guidelines:
  1. Indentation is neat, consistent and follows good practice. 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.
  2. Variable name choice: variables should have sensible names.
  • Your submission should be on-time. If you miss the deadline, you are subject to getting a zero

Grading Rubric

Points from automated Gradescope system tests

Passed Tests

Test Group Test Name Value
countEvens countEvensTest 25 pts
countPrimes countPrimesTest 25 pts
maxOfArray maxOfArrayTest 25 pts
maxOfArrayErrorTest maxOfArrayErrorTest 25 pts
sumOdds sumOddsTest 25 pts
sumOfArray sumOfArrayTest 25 pts
utilityTest utilityTest 50 pts

Style (5 pts): Good choice of variable names, code indented in ways that are consistent, and in line with good C++ practice. Where applicable, common code is factored out into functions (added to utility.h and utility.cpp as needed).

This last point may or may not arise, but if it does, utility.h and utility.cpp is a place where functions needed in multiple files can be put -— prototypes in utility.h and function definitions in utility.cpp.

Step 10: Done!

Remember that we will check your code for appropriate comments, formatting, and the use of required code, as stated earlier, based on your github submission.

If you are in the Phelps lab or in CSIL, make sure to log out of the machine before you leave. Also, make sure to close all open programs before you log out. Some programs will not work next time if they are not closed. Remember to save all your open files before you close your text editor.

If you are logged in remotely, you can log out using the exit command:

$ exit