Week 04 Tutorial Sample Answers

  1. The tutorial will start with a code review.

    Your tutor has asked a lab pair to present their week 3 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 is Minesweeper? Have you played the game before?
  3. How does the Assignment version of Minesweeper differ from the game?
  4. How does Minesweeper store a minefield and how can you access a single square?
    It's a two dimensional array of integers. You access a square by using its row coordinate as the first index and then its column coordinate as the second. Eg: minefield[row][column].
  5. Draw a 2D grid on a piece of paper or the whiteboard and draw where mines would be for the following coordinates:
    • 6 7
    • 4 4
    • 3 6

      Can you think of how a computer would check for how many mines there are in a particular row or column of the minefield?

      Remember with these questions that the top left corner is 0,0 and that coordinates are given as row,column pairs rather than x,y coordinates.

      If we were to write a program to check for mines in a row or column, it would make sense to loop along all the rows in a column or all the columns in a row and check each square to see if it has a mine in it.

      1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 1
      1 1 1 1 1 1 2 1
      1 1 1 1 2 1 1 1
      1 1 1 1 1 1 1 1
      1 1 1 1 1 1 1 2
      1 1 1 1 1 1 1 1
      
  6. Scanf is a function that can have a result. This result will be the number of values it has read and assigned to variables.

    For example: int result = scanf("%d", &variable);

    The variable result will have the value 1 if one integer was read from the user. We call input from the user "standard input".

    Discuss how we could create a loop that would continually read values in from standard input until the user inputs anything other than a number (or the input ends). This question will be continued in one of the Lab exercises this week.

    If scanf can be used to give a result . . . then it can be used as the tested expression in an if statement or while loop. In that case, if we put scanf in the while loop condition . . . we will read input every time we try to loop, and then we will only enter the loop's code if the scanf gives us a 1. The code might look a bit like this:
    int inputVar;
    while (scanf("%d", &inputVar) == 1) {
        // successfully read an integer into inputVar
        printf("Read %d", inputVar);
    }
    
  7. What is an array?
    An array is a collection of elements with the same data type. Each element is accessed providing the name of the array and an index. The index range is from 0 through to N-1, where N is the number of elements in the array. This is also known as zero-based indexing.
  8. Write a function called firstPlusThird that sums the first and third element of an array called numbers and returns the answer.
    int firstPlusThird(int numbers[]) { return numbers[0] + numbers[2]; }
    Note third element is accessed using numbers[2] since C uses zero based indexing.
  9. If an array is declared as int numbers[20]; and your program assigns a value to each element in the array, what is the problem with the statement x = numbers[20]; ?
    Because arrays use zero-based indexing, accessing the element indexed 20 is accessing the 21st item in the array. This will access a value that is not within the boundaries of the array.

    Behaviour of a program that does this is undefined and it is possible, for example, that it will cause the program to terminate. Sometimes it will retrieve the value of another variable.

  10. How would you declare a variable squares to be an array of integers with 15 elements?
        int squares[15];
    

    Write a C code function to store, in each element of this array, the square of the index of that element, e.g., squares[5] would contain the value 25.

    Note that arrays are very special in that, if they are given as the input to a function, the function will have access to the array and can change it. We'll cover why this is and the difference between that and variables later!

    Here is a complete program that uses the function.
    // Squares Array - writing the square of the indexes
    // into an integer array.
    
    // Modified by Marc Chee, March 2020
    
    #include <stdio.h>
    
    #define ELEMENTS    15
    
    void write_squares(int squares[ELEMENTS]);
    
    int main(void) {
        int squares[ELEMENTS];
    
        write_squares(squares);
    
        int i = 0;
        while (i < ELEMENTS) {
            printf("squares[%d] has the value %d\n", i, squares[i]);
            i = i + 1;
        }
        return 0;
    }
    
    void write_squares(int squares[ELEMENTS]) {
        int i = 0;
        while (i < ELEMENTS) {
            squares[i] = i * i;
            i = i + 1;
        }
    }