[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 29 15:20:04 PST 2008

Contents


    CSci201 Laboratory 04 -- decisions and loops

      Note

      You can do this lab using the graphic environment (Konqueror+Kwrite) or in a terminal using vi as you wish. You can write a Makefile if you feel it will save you time in the lab.

      Set up

      Login. Change directory to cs201. Make a new directory lab04 and change directory to it. Perform all the exercises in this lab under this directory.

      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

       		./largest
      inputting some numbers, and (on a blank line) after the data send a End-Of_Transmission signal: hold down the Control key and tapping 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 Konqueror)

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

      If...

      Create a new file called "grade.cpp" containing this code (do not use copy/paste!):
       /* Programmers: Kay Zemoudeh & Dick Botting
          Program Name:    grade.cpp
      
      
          This program translates a percentage grade into a letter grade
          or else reports an error.
       */
      
      
       #include <iostream>
       #include <string>
       using namespace std;
      
      
       int main()
       {
          double percentage;
          cout <<"Enter a percentage (0..100): ";
          cin >> percentage;
      
      
      
      
          cout << "Letter grade is ";
          if (percentage > 100)
          {
             cout << "Percentage too large";
             return 2; /* signals error #2 to operating system*/
          }
          else if (percentage >=95)
             cout << "A";
          else if (percentage >= 85)
             cout << "B";
          else if (percentage >=75)
             cout << "C";
          else if (percentage >=65)
             cout << "D";
          else if (percentage > 0)
             cout << "F";
          else
          {
            cout << "Invalid Percentage: " << percentage << endl;
             return 1;/* to signal an error to the operating system*/
          }
          cout << endl;
      
      
          return 0;
       } // main
      Don't forget to save it.

      Check your file

      If you can spot any errors, correct them.

      Compile and run the program

      Make sure there are no syntax, semantic, or logic errors. Here is a suitable command
       		c++ -o grade grade.cpp
      Are any compile errors output? If not go to the next step.

      Test your grading program

      Try every grade in turn... and some bad data. Does the program do the right thing? If not, figure out why and fixit.

      Programming Exercise

      Modify the program grade.cpp to also handle letter grades followed by + or -. make the code match the grading in the syllabus [ ../../syllabus.html#grading ]

      (Hint: Try to minimize your changes.)

      (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.

      Authors

      WhoWhenWhat
      Kay Zemoudeh12/9/1997Author
      Dick Botting04/21/2005Improved
      Dick Botting01/22/2008Rewrote

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