Week 05 Tutorial Questions

    Code Review

  1. 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!

  2. Assignment 1 - Slide

  3. Assignment 1 (Slide) -- how is your class going? Are there any questions, or lessons learned from the first week of it being out?

  4. Valid Functions

  5. Which of the following functions would be possible to write? If they are not possible to write, discuss what you could to 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.

  6. 2D Arrays

  7. What is a 2D array?

  8. 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 it instead filled a 2D array with 1's and printed it out? What are the benefits of storing the values of the grid in a 2D array?

  9. 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);
    
  10. Pointers

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

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

  13. 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.
  14. 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);
    
  15. 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);
    
  16. Revision questions

    The remaining tutorial questions are primarily intended for revision - either this week or later in session.

    Your tutor may still choose to cover some of the questions time permitting.

  17. This program has been left incomplete:

        void pointer_info(int a, int ptr) {
            scanf("%d", a);
            scanf("%d", ptr);
    
            if (a !=  ptr) {
                printf("a and ptr are different: a == %d and ptr == %d\n", a,  ptr);
            }
        }
        

    Fill in the blanks such that you have a C program that compiles.

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

  19. Write a C program occur.c which reads 6 numbers then reads another number and prints how many times that number occurred in the first 6.

    For example:

        ./occur
        Enter 6 numbers: 1 3 1 3 1 9
        Enter a number: 1
        1 occurred 3 times in the 6 numbers read
        

    Make sure you make you make it very easy to change how many numbers the program reads.

  20. Write a C program which reads numbers until end-of-input is reached and then prints the middle number.

    So if 9 numbers were entered the program should print the 5th number entered.

    If 27 numbers were entered the program should print the 14th number entered.

    If an even number of numbers is entered there are two middle numbers, print the second of them.

    For example if 8 numbers are entered, prints the 5th.

    You can assume at most 10,000 numbers will be entered.

  21. Write a C function that takes in an array of integers, and returns the average.

    double average(int numbers[], int num_elements);
    
  22. Consider:

        double ff[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
        double *fp = &ff[0];
        

    What are the similarities between ff and fp? What are the differences?

  23. What would be the output of the following code?

        int x = -9;
        int *p1 = &x;
        int *p2;
    
        p2 = p1;
        printf("%d\n", *p2);
        *p2 = 10;
        printf("%d\n",x);
        
/import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-valid-functions.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-valid-functions.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-valid-functions.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-valid-functions.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-2d-arrays.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-2d-arrays.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-pointers.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-pointers.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-pointers.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-revision-week-5.html /import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-revision-week-5.html