In this lab, we will develop C++ programs that demonstrate two algorithms:
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.
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.
cp ~pconrad/public_html/cisc181/05F/work/labs/lab02/* ~/cisc181/lab02
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.
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.
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:
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
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:
Total: 100 points