Week 05 Tutorial Sample Answers

    The tutorial will start with a code review.

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

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

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

  1. New lab pairs - you have a new lab partner for weeks 5-7.
  2. Write a function with this prototype
    int array_length(int nums[])
    
    which returns the number of elements in the array nums.
    You can not write such a function in C.

    It is not possible for a C function to determine the length of an array it has been passed.

    Programmers usually do one of 3 things.

    1. Pass the array length as another parameter to the function.
    2. Use a special value in an array element to mark the finish of the array - e.g. 0 if the array need contain only positive ints
    3. Pass an array of a specific length to the function - e.g. always pass arrays of 20 elements

    For functions you write in this course, you should opt for option (a).

  3. Write a function with this prototype
    int test_all_initialized(int nums[])
    
    which returns 1 if all elements of array nums are initialized, otherwise returns 0.
    You can not write such a function in C.

    It is not possible at runtime in C to determine if a variable has been initialized

    C programmers must take care to ensure that all variables, including array elements, are appropriately initialized.

  4. Write a function that takes in a 2d array of ints and multiplies every value in the array by a given int.
    It will have this prototype:
    void scalar_multiply(int rows, int columns, int matrix[rows][columns],  int scalar)
    
    void scalar_multiply(int rows, int columns, int matrix[rows][columns],  int scalar){
        int i = 0;
        while (i < rows) {
            int j = 0;
            while (j < columns) {
                matrix[i][j] = matrix[i][j] * scalar;
                j++;
            }
            i++;
        }
    }
    
  5. What will happen when each of the following statements is executed (in order)?

    int n = 42;
    int *p, *q;
    p = &n;
    *p = 5;
    *q = 17;
    q = p;
    *q = 8;
    

    Filling out the follwing table may be helpful:

    Address int n = 42; int *p;
    int *q;
    p = &n; *p = 5; *q = 17; q = p; *q = 8;
    0xFF80
    0xFF84
    0xFF88
    0xFF8C
    0xFF90
    Note: Address lengths have been reduced for brevity.
    Address int n = 42; int *p;
    int *q;
    p = &n *p = 5; *q = 17; q = p; *q = 8;
    0xFF80
    0xFF84
    0xFF88
    0xFF8C
    0xFF90
  6. This C code:
    int x;
    int a[6];
    
    x = 10;
    a[3 * 2] = 2 * 3 * 7;
    printf("%d\n", x);
    
    mysteriously printed 42. How could this happen when x is clearly assigned only the value 10?

    How does the output change if we create a another int after the array?

    How can you easily detect such errors before they have mysterious effects?

    The C code assigns to 42 to a[6] an array element that does not exist.

    The result of this is undefined.

    In this case the value 42 has apparently been assigned to the variable x. Silently changing another variable is a common consequence of use of an illegal array index. Subtler and more confusing behaviour are also quite possible.

    If you are using a CSE machine you can compile the program with dcc.

    It produces a program which will stop immediately with a clear error message if an invalid array index is used.

  7. Write a void function which places the sum of two numbers inside a variable passed in by reference. It should have the following prototype:
        void sum_nums(int a, int b, int *sum);
    
        void sum_nums(int a, int b, int *sum) {
            *sum = a + b; 
        }