CIS 181 Intro to Comp Sci
Class 4, February 17, 2005


UNIX TIP OF THE DAY:

Redirecting I/O -- you may have a program read data from either the terminal or a file by using the redirection facilities in unix.

For example, suppose you had a program that read 5 numbers from the terminal (using standard cin statements). You could put the 5 numbers is a file called input (for example). Then, in running the program say:

a.out < input

and the numbers from the file will be read! You may also redirect the output from a program into a file by saying:

a.out > out-file

All output that you generally see on the terminal will go to the file instead!



ASSIGNMENT due Tuesday, February 21

Reading: D&D pp. 170-208 (Starting Chapter 3)

Exercises: p.165,  #2.47, #2.47 (Extra Credit Part for extra credit), #2.49



TODAY'S TOPICS
  1. More on sentinel controlled while loops and program design. A case study: calculating gas mileage. See the file $CLASSHOME/examples/03-miles-per-gallon.cc

    Problem 2.16 from the book. Drivers are concerned with the mileage obtained by thier automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.

  2. Increment and decrement operators
  3. Abbreviated assignment statements
  4. The for repetition statement

        General form

                for ( statement1 expression1; expression2 )
                        statement2;

  5. Flow of control in a for statement
  6. Practice writing for statements
  7. Using the for statement -- case study: printing a hollow square.
    See file $CLASSHOME/examples/04-print-hollow-square.cc

  8. Logical Operators: and (&&), or (||), and not (!)
  9. The switch statement. General form:
    1.  
      switch syntax
      switch ( expression )
      {
      case constant-expression_1:
      case constant-expression_2:

      ...

      statements
      case constant-expression_3:
      case constant-expression_4:

      ...

      statements
      default:
      statements
      }
       

    EXAMPLE
     

      char c; int a, b, n;
      ...
      switch ( c )
      {
      case '+': n = a + b; break;
      case '-': n = a - b; break;
      case '*': n = a * b; break;
      case '/': n = a / b; break;
      case '%': n = a % b; break;
      default: cout << "Bad operator: " << c << endl;
      }

  10. Using the switch statement -- flow control example -- the 12 days of Christmas. See file $CLASSHOME/examples/07-12-days.cc