Week 03 Tutorial Questions

    Variable Naming

  1. 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 lowercase, with words separated by underscores.
    • #defines names must be in uppercase.
    • Variable names must start with a letter.
    • Variable names that contain vowels must be avoided.
    • Variable names must be relevant and descriptive.
  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^
    • MAX_LENGTH
    • _MEMLIMIT
    • return
  3. Structs

  4. What is a struct? How is it different from integers, doubles and characters?

  5. We want to hold the information about a particular time in the day. We need to know the hour, minute and whether or not it is AM or PM.

    1. How do we define the struct?
    2. How do we declare and initialise it?
    3. How do we print the hour of the time?
    4. How do we print it in the format: hh:mm [ap]m?

    Hint: For part d, you may want to try using %02d instead of %d in a printf, and see what it does.

  6. While Loops

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

    int i = 0;
    
    while (i < 10) {
        printf("hello\n");
        i = i + 1;
    }
    
  8. 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;
    
  9. Write a C program count_up.c which reads an integer n and then prints the integers 1..n, one per line.

    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
    
  10. How could we build on our code from count_up.c to use an increment of the user's choosing?

    dcc -o count_inc count_inc.c
    ./count_inc
    Enter finish: 3
    Enter increment: 2
    1
    3
    ./count_inc
    Enter finish: 17
    Enter increment: 5
    1
    6
    11
    16
    
  11. 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
    
  12. Two-Dimensional Loops

  13. 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
    *
    *
    *
    *
    *
    
  14. How would you convert it to printing out a square rather than a line?

    The square program written above might behave like this:
     ./square
    Enter size: 5
    *****
    *****
    *****
    *****
    *****
    
  15. Modify the program so that it prints out a triangle like this:

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