[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci201] >> [Lab01] >> index
[Index] [Schedule] [Syllabi] [Glossary] [Labs] [Projects] [Resources] [Grading] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Tue Jan 8 14:55:39 PST 2008

Contents


    CS201 Laboratory 2 -- Writing, compiling, and running simple C++ programs

      Introduction

      In this lab we will use continue to use some user-friendly short cuts that are part of the Linux KDE (Kommon Desktop Environment) and UNIX to develop some new C++ programs. But in this lab we start to use a helpful tool called
       	make
      and the UNIX "shell". These can be used in our laboratories and remotely to compile and run programs.

      Logging in and out

      Please see [ ../labs.html ]

      Setting up a cs201/lab02 directory

      This is just like the last lab... but we need a new lab because you can only have one "Makefile" in each directory.
      1. Open your home directory with Konqueror.
      2. Double click the "cs201" folder and it should open.
      3. Choose Edit -> Create New -> Folder and type in the name "lab02", and click OK. The new folder(directory) will appear.
      4. Double Click it.

      We now use your Browser to put some files into this directory so you can do some experiments. A file called "Makefile" makes compilation and running programs easier. It describes how to compile programs and run tests -- among other things. Download [ Makefile ] and save (as source) in your cs201/lab02. Make sure it's name is Makefile -- with no extemsion.

      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

      		make
      into 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 Bernardino
      until 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


      1. Move your Browser window to one side and then use the "Tool" menu to open a terminal window
      2. Double click on the .cpp file to edit it.
      3. Always save before you make!
      4. You compile and test using the make command:
         		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

      1. Start by copying one that works.
      2. Change the file names.
      3. Make sure that you indent instructions with the TAB key.
      4. The makefile recipes are in a file called "Makefile" or "makefile".

      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!

       		pwd
      ttands for Print Working Directory and
       		ls
      mean 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.cpp
      which 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>>

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. TBA::="To Be Announced", something I have to do.
  3. TBD::="To Be Done", something you have to do.

End