COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)
  1. The tutorial will start with a code review.

    Your tutor has asked a lab pair to present their week 3 work.

    Discuss the good, the bad and the ugly aspects of their code.

    Please be gentle in any criticism - we are all learning!

  2. New lab pairs - you will have a new lab partner for weeks 5-8.

    Your tutor will facilitate the formation of new pairs.

    Please cooperate with them.

  3. Discuss the errors in these while loops
    int i;
    
    while (i < 100) {
        printf("%d\n", i);
        i = i + 1;
    }
    

    int i = 0;
    int j = 0;
    
    while (j = 1 || i < 100) {
        printf("%d\n", i);
        i = i + 1;
    }
    

    int i = 0;
    int n = 10;
    while (i < n) {
        printf("%d\n", i);
        n = n + i;
        i = i + 1;
    }
    

    int i = 0;
    while (i < 10)
        printf("%d\n", i);
        i = i + 1;
    

  4. Write a program that reads in an integer and prints out that many asterisks, each on a new line.
    ./asterisks
    Please enter an integer: 5
    *
    *
    *
    *
    *
    

  5. Consider the following program square.c
    #include <stdio.h>
    
    int main(void) {
        int number;
        int row, column;
    
        // Obtain input
        printf("Enter size: ");
        scanf("%d", &number);
    
        row = 1;
        while (row <= number) {
            column = 1;
            while (column <= number) {
                printf("*");
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
    The output if the user types in the number 5 is:
     ./square
    Enter size: 5
    *****
    *****
    *****
    *****
    *****
    
    Modify the program so that it prints out a triangle like this:
     ./triangle
    Enter number: 5
    ----*
    ---**
    --***
    -****
    *****
    

    Now modify so it prints the following pattern:
    ./diagonal
    Enter an integer: 10
    *---------
    -*--------
    --*-------
    ---*------
    ----*-----
    -----*----
    ------*---
    -------*--
    --------*-
    ---------*
    

    Now modify so it prints the following pattern:
    ./bars
    Enter an integer: 9
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    

  6. Write a C program log10.c which reads a positive integer, and calculates the integer part of its base 10 logarithm without using functions from the maths library.

    Hint: repeatedly divide by 10.

  7. Write a C program multiple_of_ten.c which reads 2 integers and then prints all of the multiples of ten between those numbers.
    ./multiple_of_ten
    Enter start: 12
    Enter finish: 42
    20
    30
    40
    ./multiple_of_ten
    Enter start: -1
    Enter finish: 7
    0
    

  8. Your lab partner worked too late last night and wrote some unusual C statements:
    #include <stdio.h>
    
    int main(void) {
        printf("%d\n", 13 < 42);
        printf("%d\n", 13 < 42 || 42 > 50);
        printf("%d\n", 13 < 42 && 42 > 50);
        printf("%d\n", 13 && 42);
        printf("%d\n", 13 || 42);
        return 0;
    }
    
    
    Obviously your partner's code has to be completely rewritten, but first figure out what it will print and why?

    Revision questions

    The remaining tutorial questions are primarily intended for revision - either this week or later in session.

    Your tutor may still choose to cover some of the questions time permitting.

  9. Write a program that reads two integers (height x length) and prints a rectangular, asterisk outline with the specified dimensions, for example:
    ./rectangle
    Enter rectangle height and length: 3 5
    *****
    *   *
    *****
    

  10. For the program above consider and discuss the ways in which you would test their correct behaviour. Then extend the program to include error checking for, and handling of, invalid input.

  11. Design and write a program that reads an integer n and prints a diamond asterisk outline with side length n, for example:
    ./diamond
    Enter side length: 3
      *
     * *
    *   *
     * *
      *
    ./diamond
    Enter side length: 6
         *
        * *
       *   *
      *     *
     *       *
    *         *
     *       *
      *     *
       *   *
        * *
         *
    

  12. Write a program that repeatedly reads in integers until a non-integer is read in and then prints the number of integers read in. For example:
    ./read_ints
    Please enter some integers:
    10 -90 100 999 78hello
    You entered 5 integers
    
    ./read_ints
    Please enter some integers:
    1 2 3 4 5 6 7 8 9
    10 11 12 hello
    You entered 12 integers
    

  13. Write a program that reads in an integer n, and then prints a nxn multiplication table. For example:
    ./multiplication_table
    Enter multiplication table size: 5
     1|   1    2    3    4    5
     2|   2    4    6    8   10
     3|   3    6    9   12   15
     4|   4    8   12   16   20
     5|   5   10   15   20   25
    

  14. Write a program (a small game), which generates a random number for the program-user to guess. For example:
    ./guess_number
    Random number is between 1 and 100.
    Enter your guess: 50
    Random number is between 1 and 50.
    Enter your guess: 40
    Random number is between 1 and 40.
    Enter your guess: 37
    
    Yay, you guessed the number 37 correctly!
    
    
    ./guess_number
    Random number is between 1 and 100.
    Enter your guess: 80
    Random number is between 80 and 100.
    Enter your guess: 90
    Random number is between 80 and 90.
    Enter your guess: 85
    Random number is between 80 and 85.
    Enter your guess: 83
    Random number is between 80 and 83.
    Enter your guess: 82
    
    Yay, you guessed the number 82 correctly!
    

  15. What does precedence of an operator mean? Make the precedence explicit in the following expressions by adding parentheses:
    1. 3 + 5 * 10 - 12

    2. a > 5 || b < 3

    3. a = b > c && b || d

    4. !a || a && c

Figuring this out can be a bit tedious and prone to errors. The moral? In complex expressions make your intention explicit by using parentheses.

Note: The precedence rules of arithmetic operators in C follow the conventions used in mathematics.

  • This is a revision question, to make the difference between if and while statements clear. Consider the two code snippets below, what will be the output after execution of each of the programs for input_num = 3.
    int input_num, i;
    
    printf("Enter a number: ");
    scanf("%d", &input_num);
    
    i = input_num;
    
    if (i <= 5) {
      printf("%d\n", i * i);
      i++;
    }
    
    
    int input_num, i;
    
    printf("Enter a number: ");
    scanf("%d", &input_num);
    
    i = input_num;
    
    while (i <= 5) {
      printf("%d\n", i * i);
      i++;
    }