makeand the UNIX "shell". These can be used in our laboratories and remotely to compile and run programs.
Here is the initial program for this lab: [ lab02.cpp ] Download it into the lab02 directory.
This is an obfuscated version of one of the examples in chapter 2.
Your mission is to edit it so it is understandable again.... but
start by checking that it works.
Compile and run the lab02.cpp program
The lab02.cpp program
interacts with the user. So we must run it in a window
that lets us supply input and see the output. For historical reasons these
are called "Terminal Windows".
Move to the directory window cs201/lab01 and select the "Tools" -> "Open Terminal" menu item. Wait for a terminal window to pop up... and close the Hint window if that appears. This Terminal Window gives you control of your computer by typing in commands and tapping "Enter/Return". Type into the terminal window:
make(All lower case letters and no spaces!), and tap "Enter/Return".
Notice that make tells you what it is doing and then does it.
When the program runs it asks for input data... supply it, one value
at a time (tap Enter after each one) until it outputs an answer.
Improve the code
Double click the lab02.cpp icon in the cs201/lab02 directory
and wait for the code to pop up in KWrite.
Take a little time, and perhaps using the book, see what you can guess/figure out about the code.
Your task is to change it so that it still works but it is clear how it does it. You should end up with something like one of the examples in chapter 2. Notice you should also change the comment at the start of the program.
Hint: Make a small change, save the file by clicking the little Disk icon at the top of the KWrite window. Then re-enter
makeinto your terminal window.... and test to see if the program still works.
Compilation errors?
Does the compiler object to your code? If you can't work out why
call me over... Fix the error and try again until the program runs...
Credit (Grade=D or C)
When the program is beautiful again... show them to the lab instructor...
you have earned either a D or C depending how beautiful the code
is. If you have time you can turn a D into a C. Or you can continue and earn an 'A'.
Unscrambling code
Download this totally scrambled program
[ csusb.cpp ]
it used to output
California State University, San Bernardinountil I shuffled the lines about.
Use KWrite fix it and the Makefile will handle the compilation and run like this
make test3
Credit (Grade=B)
If you get this far before the end of the lab session you have earned an
B ( 9 points).
Integer Division
Download and test the following straight forward program
[ div.cpp ]
which will input two numbers like 6 and 3 and output the result
of dividing 6 by 3. But if you try dividing 6 by 3 you get the answer 0.
This is because the numbers are defined as ints -- whole numbersor
integers.
You task is to make the program work with real numbers -- doubles rather than integers. Use KWrite fix it and the Makefile will handle the compilation and run like this
make test2
Credit (Grade=A)
If you get this far before the end of the lab session you have earned an
A.
Reminders of Lab01
make test3
What is a makefile
A makefile is a list of recipes:
popcorn: bag_of_corn
put bag_of_corn in microwave oven
push the popcorn button on the microwave
Take corn out of microwave
clean: oven cloth
Wipe cloth inside the oven
Each recipe starts with
result: ingredients...followed by statements that are tabbed in:
do_something
You can write your own using any editor as long as you
Hint: Don't use the mouse to copy and paste Makefiles: this can replace tabs by spaces:-(
UNIX Commands for fun and productivity
By the way, you can change a terminal/command window to another
directory very easily by typing in this command:
cd ../lab01"cd" means Change Directory to the lab01 under this directories parent directory!
pwdttands for Print Working Directory and
lsmean LiSt the files. These are good when you [ Change Directory ] and are not sure where you got to and what files are there.
Here is another useful command to use before playing with a file:
cp initials.cpp initials0.cppwhich CoPies the file intitials.cpp and creates a new intitials0.cpp with the same data in it.
To change a file you can use vi (the VIle editor) like this
vi initials.cpp
. . . . . . . . . ( end of section CS201 Laboratory 2 -- Writing, compiling, and running simple C++ programs) <<Contents | End>>