Week 06 Tutorial Questions

  1. The tutorial will start with a code review.

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

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

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

  2. What does the following function do?
    int secret_function(char *word) {
        int i = 0;
        int result = 0;
        while (word[i] != '\0') {
            if(word[i] >= 'a' && word[i] <= 'z') {
        return result;
  3. Can you explain the stopping case in the while loop? Why does it work and what is the significance of the '\0'?
  4. What does the char *word input mean? What's the relationship between an array and a pointer?
  5. Observe the following code, modified from lectures:
    /* A Dice checking app
    Marc Chee September 2019
    This will take dice roll numbers from a user
    and add them together.
    It will then check if the roll total is higher
    or lower than a secret target number
    #include <stdio.h>
    #define EXIT_SUCCESS 0
    #define SECRET_VALUE 7
    int main(void) {
        int dieOne;
        int dieTwo;
        int diceSize = 6;
        // Ask the user for the size of the dice
        printf("How many sides are on the dice?\n");
        scanf("%d", &diceSize);
        // Get user input for dice rolls
        printf("Please type in the result of the first die:\n");
        scanf("%d", &dieOne);
        if (dieOne < 1 || dieOne >= diceSize) {
            printf("%d was invalid, ", dieOne);
            // Use mod to give a value inside the range 1-dice size
            dieOne = dieOne % diceSize;
            if (dieOne == 0) {
                dieOne = diceSize;
            printf("and has been corrected to %d.\n", dieOne);
        printf("Please type in the result of the second die:\n");
        scanf("%d", &dieTwo);
        if (dieTwo < 1 || dieTwo > diceSize) {
            printf("%d was invalid, ", dieTwo);
            // Use mod to give a value inside the range 1-dice size
            dieTwo = dieTwo % diceSize;
            if (dieTwo == 0) {
                dieTwo = diceSize;
            printf("and has been corrected to %d.\n", dieTwo);
        printf("You rolled %d and %d\n", dieOne, dieTwo);
        // create total and check against the secret number
        int total = dieOne + dieTwo;
        printf("The total is %d\n", total);
        if (total > SECRET_VALUE) {
            // total is higher than SECRET_VALUE
            printf("You succeeded!\n");
        } else if (total == SECRET_VALUE) {
            // total is tied with SECRET_VALUE
            printf("An exact tie!\n");
        } else {
            // total is lower than SECRET_VALUE
            printf("You failed!\n");
        return EXIT_SUCCESS;
    Discuss how we could make this code easier to read and safer to modify using functions. Suggest a function that would help and write that function.
  6. Write a program sum_digits.c which reads characters from its input and counts digits.

    When the end of input is reached it should print a count of how many digits occurred in its input and their sum.

    The only functions you can use are getchar(), printf() and a very useful function called isdigit() from the ctype.h library which you should include.

    For example:

    1 2 3 o'clock
    4 o'clock rock
    Input contained 4 digits which summed to 10
    12 twelve 24 twenty four
    thirty six 36
    Input contained 6 digits which summed to 18