Week 05 Tutorial Questions

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

  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);