CS 112 Project 1: More Control Structures

Objectives:

  1. Practice using for loops, while loops, and if statements.
  2. Practice creating functions and passing parameters.

Grading Rubric

This project is worth 20 pts:

Step 0. Prepare

Open Eclipse and create a new project called proj1 (not proj 1, not proj01, not Proj 1, not Proj1). In the project create a new source file called main.cpp.

Create a second file, a text file, called answers.txt. You will submit answers to each small coding step below into this file. Separate your answers with a line like this:

      Step x. -------- <the current time> -----------------
      your code here
      sample output from running your code
   
Please put the current clock time into the header for each step, so that I (your professor) can see how long each step takes you.

Step 1. function practice, 1

In main.cpp, above main(), create a function called constrain() that has 3 parameters: constrain() returns a double, which is the value constrained by low and high. I.e., if value is greater than high then, return high. If it is less than low, return low. Else, return the value itself unchanged.

In main(), write assert statements to test that your funcion is correct in all cases.
Add a line like cout << "All tests passed!\n" after your assert statements.

Submit your code when you have it working correctly, labeling the submission as "Step 1. --------- <the current time> -------------------------------"

Step 2. function practice, 2

Create a new Source File called utils.cpp and a new Header File called utils.h.

Move your constrain() function to utils.cpp and a write a prototype of the function in utils.h.
Include utils.h in main.cpp.
Compile your project and make sure everything works OK.
Now, make the default values for constrains() low parameter be 0.0, and the high parameter be 100.0.
Add more assert "tests" to your main code to make sure these default arguments work correctly.

When you have your code completed (and perfectly indented!), copy and paste your code from all files into answers.txt.

Step 3. Compute prime numbers

In your utils.cpp and .h files, write a function isPrime() that takes an unsigned integer and returns true if the value is a prime number (and false otherwise).
A prime number is a number that is evenly divisible by only itself and 1. That is,
for each integer "divisor" value from 2 through n / 2,
      if you divide the number by that divisor, and the remainder is 0, that number is not prime -- return false.
if you get through all possible divisors, the number is prime: return true.

Note that 1 is not a prime, for some reason. 2 is the first prime. Your code may have to handle this case specially (my code does);
In main(), remove the code to test constrain().
Add code to test isPrime() using asserts.
Make sure you test if 4 is a prime (it isn't).
When you have this working correctly, paste the code from the various files into your answers.txt file.

Step 4. Collecting prime numbers

In your main.cpp file, create a function above main() that:
Note that this is a little tricky -- if the size of the array is 100, do you loop up to 1000 to find the first 100 primes? Or 2000? You don't know... So, use a while loop keeps testing subsequent numbers as long as the array is not full.
In main(), write code to define an array of a certain size, call the function, and then assert that various correct values are in the array that the correct indices. You might find it useful to know that the 100th prime is 541.

Submit

For this project, submit only your answers.txt file into
/home/cs/112/current/yourUserId/proj1
Any code in the file should be indented correctly, as if it were in a .cpp file.

More info...

The directory /home/cs/112/current should contain a directory with your user-name. You can check by typing this command into a terminal window:

$ ls /home/cs/112/current
   
Each week's lab and project must be (separately) copied to your directory, so that the grader can grade it. If your lab is not copied to your directory within /home/cs/112/current, the grader will not be able to find it, and you will receive a zero!

To illustrate, suppose that your user-name is abc1, that you are currently in your home folder, that your Eclipse workspace is named cs112, that your cs112 folder is within your home folder, and that your lab01 folder is inside your cs112 folder (i.e., /home/abc1/cs112/proj1). Then you should enter:

$ cd cs112

to change directory to your workspace -- the folder "above" your proj1 folder. Then enter:

$ ls
to view the contents of your workspace. At this point, you should see folders for last week's lab (lab0) and this week's lab (lab1). Then enter:
$ cp -r lab1 /home/cs/112/current/abc1

where you replace abc1 with your user-name. The cp -r command will recursively copy lab1 and all of its subfolders into your folder inside /home/cs/112/current/. You can check that this worked by listing the contents of your folder:

$ ls /home/cs/112/current/abc1
lab01

and then verifying that your folder contains a copy of the today's files:

$ ls /home/cs/112/current/abc1/lab01
... files for lab01 should appear ...

We will be using these same commands to submit our labs and projects each week for the rest of the semester, so you should record them somewhere that you can easily find them (or maybe bookmark this page).

When your folder within current contains a copy of your lab work, you are all done. Congratulations!

Next, you should email all your files to your partner, so that she/he can work independently on the project.


CS > 112 > Projects > 01


This page maintained by Victor Norman.