Week 03 Tutorial Questions

  1. Tuts from now on will start with code reviews.

    This week your tutor will show you how it is to be done in future weeks.

    Your tutor will nominate a lab pair this week to do next week's code review.

    How a code review works:

    • The reviewees show their code on the projector, and briefly walk through how it works.
    • The class and tutor (the reviewers) give feedback, ask questions, make suggestions. You tutor will show you how to do this at first but then will expect the other reviewers to take over.
    • Reviewers are NOT negative, a review is to be supportive and constructive and helpful to the reviewees.
    • The reviewees should speak very little, just give a brief overview of the code they want reviewed.
    • Let everyone have a turn to speak, don’t dominate the conversation.
    • Contribute and participate, don’t be silent. If you don’t understand anything then that doesn’t mean be silent - it means ASK for an explanation. By asking you are helping the coders to see how to be clearer.

    The program below determines if a student is in high school given their current year. What are the good qualities of the program? What could be improved?

    #include <stdio.h>
    
      int main(void) {
      int a = 7; int b = 12; int c;
      printf("What year are you in? ");
      int c = 20;
      scanf("%d", &c);
          if (c > a || c == a){
          if (c < b || c == b){
          printf("You are in high school\n");
          }
          }
          if ( c < 7 || c > 12) {
          printf("You are not in high school\n");
          }
          return 0;
                  }
    
  2. 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
  3. C is a typed language. What does this mean? Why is this useful?

  4. Write a C program count42.c which prints the integers 1..42, one per line. For example:
    dcc -o count42 count42.c
    ./count42
    1
    2
    3
    
  5. Write a C program count_up.c which reads an integer n and then prints the integers 1..n, one per line. For example:
    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
    
  6. 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;
    
  7. 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
    
  8. 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
    
  9. 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