[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] >> [CSE201] >> [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]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Thu Jan 12 12:09:39 PST 2012


    CS201 Laboratory 2 -- Compiling, Running, and Writing simple C++ programs

      Previous [ ../lab01/ ] Next [ ../lab03/ ]


      In this lab we will use our lab machines to test and develop C++ programs. We will use the operating file browser to make directories for your work In the lab we will use a simple editor called "gedit" to change C++ files. We will use a command line terminal window to do most of the compilation in a "Terminal" window. Command line methods will not change if the operating system is updated. They can be used in our laboratories and remotely to compile and run programs.

      Wait for the teacher to demonstrate the procedures at the start of the class.

      Logging in and out

      This is the same as in the first laboratory. Please see [ ../labs.html ]

      You can open a Web Browser and point it at this page


      Setting up a cs201/lab02 directory

      You must first create a "cs201" directory for your work in this class, and within that a "lab02" directory for the work today.

      First, click on activities and then on the image of a file cabinet in the favorites. This opens a window and you can use a Right Click to in it to open a menu and select "Create New Folder" to add a folder. You can change its name to "cs201" and keep you CSCI201 work in.

      Then Double click the new folder icon so that it opens -- it should be empty. Use the same menus to create a directory called "lab02". Double Click it. It will open -- it should empty. We will put files into it from the cs201/lab02 web page.

      The Hello World Program

      We start with a version of the "Hello World" program on page 15 in Chapter 1.

      Right click this link [ hello.cpp ] and select the "Download..." option. Ask the browser to put the file your cs201/lab02 directory (demo in class).

      Testing with g++

      Start by opening a terminal window (Applications->System Tools->Terminal -- demo in class) and changing directory to the cs201/lab02 directory Type into the terminal window:
       		cd cs201/lab02
      and tap the Return/Enter key. You can list the files with this command
      (and tap Return).

      We will use the Gnu compiler. Try the commands as shown below in your terminal window. The prompts will be different since they depend on your "Working directory".

      Testing with Q

      I have created a command Q that compiles, checks, and runs most programs -- in most languages. Try this:
       	~dick/bin/Q hello.cpp

      Editting a program

      Start by making a backup copy of "hello.cpp"
       		cp hello.cpp hello.cpp.bak
      before you start to play with the code.

      Your task is to try out the errors shown on page 19-21 of our text.

      Your tool is the "gedit" editor. Double clicking the "hello.cpp" file should start it up. Or

       		gedit hello.cpp &

      The "&" above lets you pop up the gedit window and then input terminal commands.

      Use the editor to make a change suggested in the book and save it.

      Click in the terminal window and repeat the test.... use the up-arrow key to go back to a previous command and then tap the "Enter" key to repeat it.

      Does the compiler object to your code? It gives a line number in front of the error look on that line, and the lines above it. Look for broken syntax rules. Fix the error and try again until the program runs...

      You can always get the old copy back

       		cp hello.cpp.bak hello.cpp
      typed in your terminal!

      Make the program say Happy New Year

      Copy the original into "greeting.cpp"
       		cp hello.cpp.bak greeting.cpp
       		gedit greeting.cpp

      Make the "greeting" program display "Happy New Year" when compiled and run.

      Change the comments to match -- your name and what you have done.

      Credit (Grade=B)

      If you get this far before the end of the lab session you have earned an B ( 9 points out of 10).

      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.
       		~dick/bin/Q div.cpp
      But if you try dividing 3 by 6 you get the answer 0. This is because the numbers are defined as "int"s -- whole numbers or integers. You should also find out what happens when you input numbers like 2.1 and 0.7.

      This is clearly not a good program.

      You task is to make the program work with real numbers -- "double"s rather than integers. Hint: look at pages 58-59 and guess the one word you need to change in "div.cpp". Process: Guess, edit, save, test, and repeat if not right.

      Credit (Grade=A)

      If you get this far before the end of the lab session you have earned an A.

      Spare time

      Now would be a good time to create a new folder for your first project and download [ ../project.cpp ] into it as a "fill in the blank" starter file.

      UNIX Commands for fun and productivity

      By the way, you can easily create, list, and change directories in a terminal window. These commands can be done remotely or in a Terminal window in the lab. Computer majors are Wise to master these commands. The Club runs special training sessions (highly praised by students in the past) on UNIX commands.

      You can create a new directory by typing in the "mkdir" command:

       		mkdir example
      "mkdir" means Make Directory under the current "Working directory".

      You can change a terminal/command window to another directory very easily by typing in the "cd" command:

       		cd example
      "cd" means Change Directory to the "example" in this directory!

      The command

      always takes you back to your home directory.

      These commands show you where you are and what files and directories you have.

      stands for Print Working Directory and
      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 example.cpp old.example.cpp
      which CoPies the file example.cpp and creates a new old.example.cpp with the same data in it.

      To change the name of a file you can use the "MoVe" command:

       		mv mistook.cpp mistake.cpp

    . . . . . . . . . ( end of section CS201 Laboratory 2 -- Compiling, Running, and Writing simple C++ programs) <<Contents | End>>


  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.