C programming

Mary PW Chin 钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:

C lesson #1

Getting started

C lesson #2

Dice

C lesson #3

Circles

C lesson #4

More loops

Synopsis
  1. The printf function;
  2. Compiling and running;
  3. Variables, integers and floating-point numbers;
  4. Arithmetic operators;
  5. Auto-completion (TAB key) and command history (UP and DOWN arrow keys);
  6. for loops:
  7. Sequence of numbers;
  8. Line and block commenting.
Exercise
  1. Refer to the program on slide #34. What happens if we make five changes
    • the 'n' on line #20 to 'a';
    • the 'n' on line #25 to 'b';
    • the 'n' on line #30 to 'c';
    • the 'n' on line #35 to 'd';
    • the 'n' on line #40 to 'e',
    and then run? What changes on other lines do we need in order to make the program work?
  2. Write a program that prints the following with as few repetitions as you can:
    
                 ******************
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 *                *
                 ******************
              
  3. Write a program that prints the following with as few repetitions as you can:
                      *
                     * *
                    *   *
                   *     *
                  *       *
                 *         *
                *           *
               *             *
              *               *
               *             *
                *           *
                 *         *
                  *       *
                   *     *
                    *   *
                     * *
                      *
              
  4. Write a program that prints the following with as few repetitions as you can:
               *
               **
               ***
               ****
               *****
               ******
               *******
               ********
               *********
               **********
               *********
               ********
               *******
               ******
               *****
               ****
               ***
               **
               *
              
  5. Write a program that prints the following with as few repetitions as you can:
               *  *  *  *  *
                *  *  *  *  *
                 *  *  *  *  *
               *  *  *  *  *
                *  *  *  *  *
                 *  *  *  *  *
               *  *  *  *  *
                *  *  *  *  *
                 *  *  *  *  *
               *  *  *  *  *
                *  *  *  *  *
                 *  *  *  *  *
              
  6. Write a program that prints a multiplication table for 3:
    133
    236
    339
    12336

  7. Write a program that prints the multiplication tables for 2, 3, 4, 5, 6, 7, 8 and 9:
    122
    224
    326
    12224
    133
    236
    339
    12336
    14 4
    24 8
    34 12
    124 48
    15 5
    25 10
    35 15
    125 60
    16 6
    26 12
    36 18
    126 72
    17 7
    27 14
    37 21
    127 84
    18 8
    28 16
    38 24
    128 96
    19 9
    29 18
    39 27
    129 108
  8. Write a program that prints a combined multiplication table for 2, 3, 4, 5, 6, 7, 8 and 9 as follows:
    n22*n33*n 44*n 55*n 66*n 7 7*n 8 8*n 9 9*n
    122334455667 7 8 8 9 9
    224 36 48 510 612 7 14 8 16 9 18
    326 39 412 515 618 7 21 8 24 9 27
    2 3 4 5 6 7 8 9
    2 3 4 5 6 7 8 9
    12224 336 448 560 672 7 84 8 96 9 108

C programming

C lesson #1

Getting started

C lesson #2

Dice

C lesson #3

Circles

C lesson #4

More loops