[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]
Tue Jan 31 11:31:43 PST 2012


    CSci201 Laboratory 05 -- decisions and loops

      Previous [ ../lab04/ ] Next [ ../lab06/ ]


      You can do this lab using the graphic environment or in a terminal using vi as you wish.

      One experiment needs the CCC graphic library and can not be run remotely.

      Set up

      Login. Change directory to cs201. Make a new directory lab05 and change directory to it. Perform all the downloading and exercises for this lab in this directory.

      A Nested loop

      Here [ mtable.cpp ] is a completed multiplication table program. Download it, compile it (use "g++"), and test it.

      Can you change it so that instead of a square table it outputs a triangle:

       	   2   4
       	   3   6   9
       	  10  20  30 ... 100
      Hint: change just one const identifier to a variable...

      A Simulation that miscalculates Pi

      Download [ piman.cpp ] and compile and test it. Use "~dick/bin/Q" becasue it uses graphics.

      Your task is to make it try out 10 times as many point to see if the circle is filled in.

      Fixing ifs, whiles and conditions

      Download this program [ largest.cpp ] , Compile it The following should find a mistake I planted in the code:
       		g++ -o largest largest.cpp
      Try to figure out what I have deleted from it.

      Fix it, compile, and test it by running this command

      inputting some numbers, and (on a blank line) after the data send a End-Of_Transmission signal: hold down the Control key and tap the letter D.

      Make sure it handles missing data and a series of numbers.

      Otherwise fix it!

      Modifying the program

      Make a copy of largest.cpp called smallest.cpp
       		cp largest.cpp smallest.cpp
      (or use the GUI desktop)

      Change smallest.cpp so that it outputs the smallest of the input numbers rather than the largest.

      Calculate the mean of some numbers

      Copy "smallest.cpp" to "mean.cpp". Edit "mean.cpp" so that it reads in all the numbers and calculates their mean.

      The mean of a list of numbers is found by adding them up and dividing by the number of numbers:

    1. mean = sum(input) / size(input)

      So you need to both count the size of the sample -- the number of numbers and also add them up.

      (Hint: Start by writing some examples of what you want by hand. Then it often helps to draw a picture of how a program behaves or to describe the inputs and what happens to them. Then work on the source code. )

      First fix the syntax errors.

      Then test with as many test inputs that you can think of. Make sure your program is error free.

      Getting this far by the end of the lab gets you an A for the lab.

      To earn credit

      Either show the lab instructor the programs or print them and hand the printouts in.

      You can work on your project when you get this far


      Kay Zemoudeh 12/9/1997
      Dick Botting 02/02/2010

      (Close Table)

    . . . . . . . . . ( end of section CSci201 Laboratory 05 -- decisions and loops) <<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.