Week 05 Tutorial Questions

    Valid Functions

  1. Which of the following functions would be possible to write? If they are not possible to write, discuss what you could do to make them work.

    1. int array_length(int nums[]);
      

      which returns the number of elements in the array nums.

    2. int test_all_positive(int nums[]);
      

      which returns 1 if all elements of array nums are positive, otherwise returns 0.

    3. int test_all_initialized(int length, int nums[]);
      

      which returns 1 if all elements of array nums are initialized, otherwise returns 0.

    4. int test_all_positive(int length, int nums[]);
      

      which returns 1 if all elements of array nums are positive, otherwise returns 0.

  2. 2D Arrays

  3. What is a 2D array?

  4. The code square.c prints a 7×7 grid of 1's

    #include <stdio.h>
    
    #define SIZE 7
    
    int main(void) {
        int row, column;
    
        row = 0;
        while (row < SIZE) {
            column = 0;
            while (column < SIZE) {
                printf("1");
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    

    How would you change it so that a 2D array of ints was created and each element was given the value of 1?

    How would you then print out this array so it looked the same as the original output?

    What are the benefits of storing the values of the grid in a 2D array?

  5. 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);
    
  6. Pointers

  7. What is a pointer? How can you declare and initialise a pointer?

  8. Why did we have to always include the & symbol in our arguments given to scanf?

  9. What will happen when each of the following statements is executed (in order)?

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

    Fill in the values of each variable in the below visual at each point in the code execution.

                
    01: int n = 42;
    02: int *p;
    03: int *q;
    04: p = &n;
    05: *p = 5;
    06: *q = 17;
    07: q = p;
    08: *q = 8;
    Type:
    Name:
    Value:
    Note: Address lengths have been reduced for brevity.
  10. 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);
    
  11. Write an int *function which returns the address of the greater of two integers passed by reference.

    It should have the following prototype:

    int *max(int *a, int *b);
    
  12. CSE Valley

  13. How are you going? Are there any questions or lessons learned from the first week of it being out?

  14. What do you think are some common style mistakes seen in COMP1511 assignments?