Week 07 Tutorial Sample Answers

  1. Well done everyone for such a great effort in the first assignment.

    Take a moment to think about how you found the assignment, and what you learned in the process of working on it.

    If you could go back in time to the very start of the assignment, and give one piece of advice to your past self, what would you say?

    You tutor will talk about how you will get feedback from the marking of assignment 1.

    Once the assignment has been marked, students will be able to see their assignment grade through the "Grades" column chart icon near their name at the bottom of the left menu on WebCMS3. This page will also allow students to see the annotations their marker has put in their code for feedback.

  2. So far in this course we have learnt about two data types - int and double. What about a char variable? What is the size of this variable, and what possible values can it have? What do these values mean?

    A char variable in C is 8 bits or 1 byte long. It's possible values are either -128 to 127 or 0 to 255. This depends on the compiler and computer achitecture being used. You can not guarantee that a char can store negative values such as EOF. The value a char holds usually represents a character on the ASCII table.

  3. So far in this course we have only used scanf and printf to scan input and print output in our programs.

    How do we use getchar and putchar? How does getchar signal to us that it has reached the end of the input?

    Why does getchar return an int instead of a char?

    How do we use fgets? How does fgets signal to us that it has reached the end of the input?

    When fgets scans in a line of text, will it include a '\n' at the end of the line? Is this always the case?

    getchar will return EOF if it reaches the end of the input. Because EOF is a #define for a negative number, we can not guaranteed this value can be stored in a char, so getchar must return an int.

    fgets will return NULL if it reaches the end of the input without scanning in any further characters.

    fgets generally will place a '\n' provided there is a newline at the end of the line of characters in the input (if the user presses Enter). If the user doesn't press Enter and only presses CTRL+D then fgets will not include a '\n' at the end of the string.

    More thorough descriptions of these functions and their behaviour can be found in the man pages.

  4. 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') {
                result++;
            }
            i++;
        }
        return result;
    }
    

    It will return the number of lower case letters in a given word. Comparing the letters to 'a' and 'z' allows us to treat them like numbers, in essence saying: "All letters between 'a' and 'z' inclusive."

  5. Can you explain the stopping case in the while loop? Why does it work and what is the significance of the '\0'?

    The stopping case is relying on a string (char * or char[]) being finished by a null terminator, which is what the character '\0' is. This means that no matter how long the array is, the loop will finish at the point of the word ending instead of the end of the array.

  6. What does the char *word input mean? What's the relationship between an array and a pointer?

    char *word means that the function takes in a pointer to a character as its input. A pointer is a variable which contains a memory address, but so is an array. This function in particular is expecting the character pointer to be telling us the address of the first character in an array of characters. Since this array of characters ends with a '\0', it is a string.

    There are some subtle differences between arrays and pointers in C. A pointer can have an address directly assigned to it but an array can not. You can only directly assign values to elements inside an array. Also, the sizeof operator will return the size of the pointer itself for a pointer, while the sizeof operator will return the total size of the array for an array.

  7. What is a pointer? How do pointers relate to other variables?

    Pointers are variables that store a memory address.

    They are usually "aimed" at a variable of a particular type.

    They're able to read the value of that variable and also are handy because they can be passed into functions, giving the function write access to a variable.

    There's a useful set of diagrams and in depth description on the blog of an ex Subject Admin and Lecturer of COMP1511. AndrewB's COMP1511 blog

  8. What is a struct? What are the differences between structs and arrays?

    Arrays and struct are both compound data types, formed from other data types.

    Array are homogeneous - formed from a single data type.

    Structs can be heterogeneous - formed from multiple data types.

    Array elements are accessed with integer array indices.

    Struct members are accessed by name.