Lab01, CISC181 honors,
Fall 2004, P. Conrad

Overview

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

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

You should refer to lab00 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.

Other Goals

In this lab, you will also accomplish the following:

  1. You'll start to learn about subdirectories and file management in Unix. (Chapters 6, 7 and 8 in Anderson will be helpful here, so you may want to read over those before coming to lab, if you have time, and have been able to purchase the book). In particular, under your home directory, you will learn how to create the following subdirectories:

    ~/cisc181
    ~/cisc181/lab00
    ~/cisc181/lab01

    You will also learn how move the files you created in lab00 (including lab00.dat, lab00.cc and lab00.txt) into your ~/cisc181/lab00 subdirectory, and how to put yourself in your ~/cisc181/lab01 subdirectory before creating any files for this lab.

    This way, you can keep your directory nice and tidy: everything for cisc181 is all in one subdirectory (folder) called cisc181, and under that subdirectory (folder), there will be a folder for each lab (one for lab00, one for lab01, etc.)



  2. You'll learn how to change directory into your ~/cisc181/lab01 directory, copy a C program from Prof. Conrad's web site, compile it, and run it. The program you copy will be one that converts Fahrenheit to Celsius. You'll then use emacs (or vi) to change that program into a C program that converts Celsius into Fahrenheit (that will be your Algorithm one program.) At the end of this lab, you'll script the results, and submit them to your TA via WebCT and on paper, along with the other two C++ programs you develop in this lab. All the files you create in this step will be stored in your newly created ~/cisc181/lab01.

Goal 1: Managing files and directories

  1. Just as we organize documents in a "real world" filing cabinet into folders, when we create files on a computer disk, we organize them into files and folders. You may be familiar with the idea of "files" and "folders" from using Windows or Macintosh. On Unix, folders are called "directories". Chapters 6, 7, and 8 of your Anderson text ("Just Enough Unix") contain details about files and directories under Unix, and I recommend that you look over those chapters soon. You may find them to be a useful reference for this step.


  2. On the unix command line, type "cd" by itself. "cd" stands for "change directory". If you type "cd" by itself, this command will always return you to what is called your "home directory". When you type in the command, there will not be any output; it will appear that "nothing happened". But if you now type "pwd" on the command line (which stands for "print working directory"), you will see the name of your home directory. On the composers (strauss, copland, etc.) the name of your home directory will be some series of letters and numbers, such as "/home/usra/d9/55560".


  3. Now type "ls" to list the files in your home directory. Notice what files are listed.


  4. Next, we will create a directory for your cisc181 files. Type in

    mkdir cisc181

    This will create a directory called cisc181, in which you can store your files for this course. If you type "ls" immediately after doing this, you will see the new directory listed among your files. Since this directory is "under" the home directory, we call it a "subdirectory" of your home directory.



  5. To move into this subdirectory, type "cd cisc181". If you then type "pwd", you will see that you have moved into the cisc181 subdirectory. If you now type "ls", you will see no files at all. This is because when you create a subdirectory, it is initally empty.


  6. Now type "cd" again, and then "pwd", and "ls". You will see that you have moved back up to your home directory. Then, type "cd cisc181" to again move down into your new subdirectory, and type "pwd" and "ls". You should see the same files you saw back at step 3 above, except now you see the cisc181 subdirectory as well. (In Unix, a subdirectory is just a special kind of file).


  7. Now, type the following two commands:

    mkdir lab00
    mkdir lab01

    These two commands will create two new subdirectories under the cisc181 subdirectory. We call these subdirectories "cisc181/lab00" and "cisc181/lab01". These subdirectories are "under" cisc181, which is in turn, is "under" your home directory.

    To show more specifically that it is under your home directory, we can use the symbol ~ to stand for your home directory. So, your new directory would be referred to as ~/cisc181/lab00. (The ~ is sometimes called "squiggle", though the proper name for it is "tilde", prounounced "till-duh")


  8. You can use the tilde in cd commands. "cd ~" will change to your home directory (though in this case the ~ is redundant, since "cd" all by itself does the same thing.)

    The command "cd cisc181" changes to the cisc181 directory under the "current" working directory, however cd "~/cisc181" will change to cisc181 under your home directory (regardless of what the current working directory happens to be).


  9. During last weeks lab, you created files called lab00.dat, lab00.cc and lab00.txt. You probably created these in your top level home directory. We are now going to move them into your newly created ~/cisc181/lab00 directory.

    Type "cd" all by itself, to change to the directory where you created this file (probably your home directory). Then, to move each file into the directory ~/cisc181/lab00, you can use the following sequence of commands ("mv" stands for "move"):

    mv lab00.dat ~/cisc181/lab00
    mv lab00.cc ~/cisc181/lab00
    mv lab00.txt ~/cisc181/lab00

    (Note that there are several shorter ways to accomplish this same task that involve less typing. After reading over Chapters 6, 7 and 8 in Anderson, you may be able to come up with some of these.)

Goal 2: More C programming on Strauss

  1. Now, change directory into ~/cisc181/lab01 with the command:

    cd ~/cisc181/lab01

    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/lab02" to create the directory, and "cd ~/cisc181/lab02" 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/lab01 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 "lab01a.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/cisc181h/04F/labs/lab01/lab01a.cc .

    The "cp" command in Unix is used to copy a file. The file is copied from a directory under Prof. Conrad's home directory. 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/lab01, we could also have done the same copy command using the following:

    cp ~pconrad/public_html/cisc181h/04F/labs/lab01/lab01a.cc ~/cisc181/lab01

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


  3. Now, do an "ls" command, and you should see the lab01a.cc file in your directory. Your next step is to list the contents of the file, then compile the program, and run the program. Before you look at the list of commands below (which do those three steps), see if you can figure out what you would type to accomplish this. Then scroll down and see if you were right.

    scroll down for the answer

    keep scrolling down for the answer

    still further

    almost there

    ok here's the answer:

    cat lab01a.cc
    g++ lab01a.cc
    ./a.out

    Did you get it right? Note: you could also have done "more lab01a.cc" in the first step (if the file is too large to fit on the screen all at once), and "CC lab01a.cc" for the second step.

    An aside about the "more" program: note that you should never use "more" inside a script file; only use "cat" inside a script file. The "more" program is only for when you are looking at the file in "real time", not for scripts that you are going to print or submit electronically.


  4. Run the program a few times with different values, and look it over to see how it works.


  5. Now, the actual thinking work starts. Continue on with the part marked "Algorithm 1" below.

Algorithm 1: An algorithm to convert Celsius to Fahrenheit

The program I've just supplied you with is an example C++ program that converts Fahrenheit to Celsius. Your task is to produce a program that converts Celsius to Fahrenheit Your program should follow the model given: that is, it should contain two functions: one to do the conversion, and a main program that calls the conversion function.

Here is the example code for the Fahrenheit to Celsius program. You will change the name of the function, the formula used, and some of the strings (strings are the things in double quotes, such as "Please enter a Fahrenheit temperature".) You will need to modify (at least) the following:

Once you have typed in your program, compile it, and run it to test it. Test it with several different values (e.g. freezing and boiling point of water, and some other values in between). Don't script your program yet, though; at the last step of this lab, you will script this program along with your other two.

// lab01a.cc Sample program to convert Fahrenheit to Celsius
// P. Conrad 9/7/2004 (note: substitute your own name and date in your
// celsius to Fahrenheit program)

// the following two lines allow stream input and output using namespace std; #include <iostream>
// ************ user defined functions *********** double fahrenToCelsius(double fTemp) { return ((fTemp - 32) / 9.0 * 5.0); } // ************ main program ************ int main(void) { // declare a variable for Fahrenheit temp double tempSuppliedByUser; // prompt for and read input cout << "Please enter a Fahrenheit temperature: " << flush; cin >> tempSuppliedByUser; // output result, and end program cout << "Celsius equivalent: " << fahrenToCelsius (tempSuppliedByUser); cout << endl; return 0; // ends the main program }

 

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

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

Where to find the sample code: Use the same command I gave you above for copying lab01a.cc, but substitute lab01b.cc in place of lab01a.cc. Copy this file into your ~/cisc181/lab01 subdirectory. Ask your TA if you need help.

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.

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 3: An algorithm to calculate square root

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

Where to find the sample code: Use the same command I gave you above for copying lab01a.cc, but substitute lab01c.cc in place of lab01c.cc. Copy this file into your ~/cisc181/lab01 subdirectory. Ask your TA if you need help.

As you now know, 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, lab01c.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 lab01c.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 lab01.txt (cd into your ~/cisc181/lab01 subdirectory, and type "script lab01.txt").
  2. For the lab01a.cc, lab01b.cc and lab01c.cc programs that you developed in this lab, do the following steps:

    pwd
    cat lab01a.cc This lists out the content of the C++ source file
    CC lab01a.cc This compiles with the Sun C++ compiler
    g++ lab01a.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 lab01b.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 lab01.txt
  4. Finally, upload lab01.txt, lab01a.cc, lab01b.cc and lab01c.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 lab01:

  1. Mechanics of scripting (lab01.txt), printing and submitting : 25 points
  2. Correctness of lab01a.cc (Celsius to Fahrenheit conversion): 15 points
  3. Correctness of lab01b.cc (Circle Overlap): 20 points
  4. Correctness of lab01c.cc (Square Root): 20 points
  5. Overall programming style (comments, formatting, neatness of code, appropriate variable names, clarity of code, etc.): 20 points

Total: 100 points