Lab02, CISC181,
Fall 2005, P. Conrad

Overview

In this lab, we will develop C++ programs that demonstrate two algorithms:

  1. An algorithm to determine whether two circles overlap or not.
  2. An algorithm to calculate a square root.

You will have to devise these algorithms for yourself. You are encouraged to use only your brain and your problem solving ability, rather than "looking up the algorithm in a book, or online".

You should refer to lab01 for instructions on how to edit, compile, and script C++ programs. You can also refer to your Anderson book for help, and ask your TA if you have questions.

Getting started, and copying the sample code

  1. Create a subdirectory with the command

    mkdir ~/cisc181/lab02

    Now, change directory into ~/cisc181/lab02 with the command:

    cd ~/cisc181/lab02

    For the next couple of labs, the instructions will remind you to create a new subdirectory for that lab, and change directory into it at the start of the lab. For example, next week, we'll do "mkdir ~/cisc181/lab03" to create the directory, and "cd ~/cisc181/lab03" to change directory into it before we start work. However, eventually, you'll just be expected to know to do that on your own.
  2. Do a "pwd" command to be sure that you are in the ~/cisc181/lab02 subdirectory. Then, use the following command to copy a program from Prof. Conrad's directory into your current working directory. BEFORE YOU TYPE THIS COMMAND, look over it carefully. You will notice that there is a space between the "cp" and the "~". There is a space between the "lab02a.cc" and a period (.) which is at the end of the command. Those spaces are very important, and the period at the end is especially important. Be sure you type the command exactly as it appears here.

    cp ~pconrad/public_html/cisc181/05F/work/labs/lab02/* .

    The "cp" command in Unix is used to copy a file. In this case, the * is a "wildcard", meaning "copy all files". The files are copied from a directory under Prof. Conrad's home directory.

    The contents of this directory correspond to the contents http://copland.udel.edu/~pconrad/cisc181/05F/work/labs/lab02 Note that the "public_html" part is not included when you specify this location as a web address (a URL).

    Also note that ~ by itself refers to "your home directory", but "~pconrad" refers to "pconrad's home directory". Similar, you can refer to any user's home directory on strauss if you know that user's login name; e.g. jsample's home directory can be accessed via "ls ~jsample".

    The period by itself at the end is the "target" of the copy command, i.e. the place we are copying the file "to". A period, all by itself, refers to the "current working directory", the same one that comes up when you type "pwd" (print working directory). So, since the current working directory is ~/cisc181/lab02, we could also have done the same copy command using the following:

    cp ~pconrad/public_html/cisc181/05F/work/labs/lab02/* ~/cisc181/lab02

    but the command was already pretty long, so using the "." is a nice shortcut.

  3. Now, do an "ls" command, and you should see some new files in your directory, including lab02b.cc and lab02c.cc (for historical reasons, there is no file lab02a.cc this week.)

Algorithm 1: An algorithm to determine whether two circles overlap or not

In this part of the lab, you will work with the sample code called lab02b.cc.

This C++ program contains the outline of a program that determines whether two circles overlap. There is a function that takes 6 parameters: (the x,y coordinates of the center and radius of a circle, and the same three values for a second circle). Both circles are assumed to lie in the same plane; "overlap" is defined as sharing one or more points on that plane.

Note added 09/20/05: The above definition of "overlap" is sufficient to precisely define what overlap means. However, in case you need a reminder of some things that may be hazy from Geometry or Algebra I/II, here are some hints:

Hopefully that helps clear up any confusion about what overlap means; this is not a change in the definition; it just adds a set of examples.

The sample code contains the outline of a circle overlap function. However that function currently always returns 0 (i.e., "false" , indicating that the circles do not overlap. It is your job to add a correct algorithm to the function.

Note also that function currently computes the square root of the radius of the sum of the two radii (plural of radius), and stores this in a variable called "sampleSquareRoot". As it turns out, that calculation is probably not very useful in a correct algorithm for determining whether the circles overlap. The purpose of putting that in the sample code is to give you an idea of how to calculate a square root in C++, using the built in cmath library (cmath stands for "C Math", i.e. the math library built into the C programming language. In many cases, C++ sort of rides of the coattails of the C programming language; rather than developing a whole new math library for C++, C++ just borrows the math library developed for C.)

You may decide you want to declare a different variable inside the circleOverlap function as an intermediate result, or you may decide that you don't need any such variables. In any case, in your final program, you should remove any variable declarations for variables that you are not using in your calculation.

 

Algorithm 2: An algorithm to calculate square root

In this part of the lab, you will work with the sample code called lab02c.cc.

As you now know (from your work with the lab02b.cc program), C++ already has a built in function to calculate square root. However, as an exercise in algorithm development, I want you to develop your own algorithm for square root, and compare the results to those that come from the built in square root function. Your algorithm should use only +, *, - and / operators, plus if / else tests, and while loops (or if you prefer, for loops). You may need to read ahead in the Deitel/Deitel textbook to learn about if tests and loops.

I have supplied you with a sample program, lab02c.cc, that shows how to test your function.

The program contains the code needed to access the built-in square root function, plus a "place holder" where you can fill in your square root function (initially, that function always returns 1.0, which is course is only correct if the number passed in is 1.0). Your square root function should use "successive approximation"; that is, making an initial guess at the answer, and then moving closer and closer to the answer. Stop when your guess (g), when squared, is within 0.01% of the true answer (the number passed into the function).

Keep the name lab02c.cc for your program. After you type it in and compile it, test it.

What to turn in (scripting, printing, and submitting to WebCT):

When you have completed all the steps above (or decided you have come as close as you can to completing them), take the following steps:

  1. Start a script file called lab02.txt (cd into your ~/cisc181/lab02 subdirectory, and type "script lab02.txt").
  2. For the lab02a.cc, lab02b.cc and lab02c.cc programs that you developed in this lab, do the following steps:

    pwd
    cat lab02a.cc This lists out the content of the C++ source file
    CC lab02a.cc This compiles with the Sun C++ compiler
    g++ lab02a.cc This shows that it also compiles with the GNU C++ Compiler

    ./a.out This executes the program.

    Supply whatever input is required for the program, then run the program several more times, as needed to show that the program works properly.

    cat lab02b.cc Now repeat the steps above for the next program in this assignment...

    ...
    exit When done with all three programs, this finishes the script

  3. Now, send your script to the printer with the following command:

    qpr -q whlps lab02.txt
  4. Finally, upload lab02.txt, lab02a.cc, lab02b.cc and lab02c.cc to WebCT, and submit.

Note that at some point this semester you might just be told to "script, print, and submit your work" At that point, you will be expected to follow a similar pattern as listed above, without being told in detail what to do at every step. That will certainly be the case more and more as you move into more advanced CISC courses. As you gain more knowledge and confidence with using Unix, and what the expectations are in a college-level programming course, I'll gradually try to wean you of the details of how to do every little step.

Grading of lab02:

  1. Mechanics of scripting (lab02.txt), printing and submitting : 20 points
  2. Correctness of lab02b.cc (Circle Overlap): 30 points
  3. Correctness of lab02c.cc (Square Root): 30 points
  4. Overall programming style (comments, formatting, neatness of code, appropriate variable names, clarity of code, etc.): 20 points

Total: 100 points