Week 03 Tutorial Questions

    While Loops

  1. What is a While Loop?

    What are the three most important things that should happen in (almost) every while loop?

    Write a while loop that prints out "hello" ten times.

  2. 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;
    
  3. Variable Naming

    A common phrase in Computer Science asserts that: "there are only two hard problems in Computer Science: Cache Invalidation, and Naming Things". In this section, we will discuss the problem of naming variables, and how to name variables to make your program better.

  4. The following statements are either: Rules of the C language, Style Rules, or Not rules. Decide as a class which are which.
    • Variable names must start with either a letter or an underscore.
    • Variable names must be shorter than 8 characters.
    • Variable names can contain numbers, as long as they aren't the first character.
    • Variable names must be lower case, with words seperated by underscores.
    • #defines names must be in upper-case
    • Variable names must start with a letter.
    • Variable names that contain vowels must be avoided.
    • Variable names be relevant and descriptive.
  5. Which of the following are valid variable names in C?

    If they are valid, would they be a good name?

    • THX1138
    • 2for1
    • mr_bean
    • my space
    • event_counter
    • ^oo^
    • _MEMLIMIT
    • return
  6. Programming Exercise

  7. Write a C program count_up.c which reads an integer n and then prints the integers 1..n, one per line.

    Your tutor may do this exercise as a competition or game -- your tutor will try to make as many mistakes as possible while coding this exercise, and you should try and correct them. If this is a competition, your class might get split up into groups, and compete to find the most errors.

      dcc -o count_up count_up.c
      ./count_up
      Enter finish: 3
      1
      2
      3
      ./count_up
      Enter finish: 7
      1
      2
      3
      4
      5
      6
      7
      
  8. Two-Dimensional Loops

  9. 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
      *
      *
      *
      *
      *
      

    How would you convert it to printing out a square rather than a line?

  10. The square program written above might behave like this:
       ./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
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      -*-*-*-*-
      

    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.

  11. Write a C program range.c which reads integers n and m and then prints the integers n..m, one per line. For example:
    dcc -o range range.c
    ./range
    Enter start: 3
    Enter finish: 7
    3
    4
    5
    6
    7
    
  12. Write a C program range7.c which reads 2 integers n and m, and then prints the integers between n and m (including n and m) which are divisible by 7.

    Hint: if x is divisible by 7, then x % 7 == 0

    dcc -o range7 range7.c
    ./range7
    Enter start: 3
    Enter finish: 49
    7
    14
    21
    28
    35
    42
    49
    
  13. Write a C program range_divisible.c which reads 3 integers n, m and x then prints the integers between n and m (including n and m) which are divisible by x.
    dcc -o range_divisible range_divisible.c
    ./range_divisible
    Enter start: 20
    Enter finish: 100
    Enter divisor: 13
    26
    39
    52
    65
    78
    91
    ./range_divisible
    Enter start: 80
    Enter finish: 120
    Enter divisor: 5
    80
    85
    90
    95
    100
    105
    110
    115
    120
    
  14. 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
    *
    *
    *
    *
    *
    
  15. 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
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    
  16. 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
    *****
    *   *
    *****
    

  17. 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.
  18. 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
         *
        * *
       *   *
      *     *
     *       *
    *         *
     *       *
      *     *
       *   *
        * *
         *
    
  19. 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!
    
  20. 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++;
    }
    
    
  • What would be the output of the following code?

    int x = -9;
    int y = 0;
    
    while (x != 0){
        y = y - 1;
        x = x + 1;
    }
    
    printf("%d\n", x);
    printf("%d\n",y);
    
  • What would be the output of the following code?

    int i = -7;
    int j = 0;
    
    while (i != 0){
        j = j - i;
        i = i + 1;
    }
    
    printf("%d\n", i);
    printf("%d\n",j);
    
  • Write a C program count42.c which prints the integers 1 through to 42, one per line. For example:
    dcc -o count42 count42.c
    ./count42
    1
    2
    3
    .
    .
    .
    41
    42