COMP1511 19T2
COMP1511 19T2
    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. What is a function? Why would we use functions in our code?
    Functions are code that is "called" and "returns". Each function will have an output type (that could be void) as well as some input parameters. The input parameters will be declared just like variables and the function will receive values for these when it is called.

    It's important to remember that variables will be passed to a function by value, which means that the function itself will not be able to make changes to the variables that are passed to them and also that the input parameters are created when the function is called and will be destroyed when the function ends.

    We use functions to separate code so that we don't have to write it repeatedly and we also don't have long or deep nested code clogging up our main.

  3. 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.

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

    Write a C code fragment 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.

    Here is a complete program instead of a fragment.
    #include <stdio.h>
    
    #define ELEMENTS    15
    
    int main(void) {
        int squares[ELEMENTS];
    
        int i = 0;
        while (i < ELEMENTS) {
            squares[i] = i * i;
            i = i + 1;
        }
    
        i = 0;
        while (i < ELEMENTS) {
            printf("squares[%d] has the value %d\n", i, squares[i]);
            i = i + 1;
        }
        return 0;
    }
    
    
  5. 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).

  6. 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.

  7. Write a function with this prototype
    void print_array(int n, int array[n])
    
    which prints n integers form an array on a single line separated by a space.
    void print_array(int n, int array[n]) {
    
        int i = 0;
        while (i < n) {
            printf("%d", array[i]);
    
            if (i < n - 1) {
                printf(" ");
            }
    
            i = i + 1;
        }
        printf("\n");
    }
    
  8. Write a function with this prototype
    int scanf_array(int n, int array[n])
    
    which reads n integers into an array.

    Your function should return the number of integers it actually read.

    int scanf_array(int n, int array[n]) {
    
        int i = 0;
        while (i < n) {
            int items_read = scanf("%d", &array[i]);
    
            if (items_read != 1) {
                return i;
            }
    
            i = i + 1;
        }
    
        return i;
    }
    

    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.

  9. 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.
    Sample solution for occur.c
    #include <stdio.h>
    
    #define N_NUMBERS 6
    
    int main(void) {
        int array[N_NUMBERS];
        printf("Enter %d numbers: ", N_NUMBERS);
        int i = 0;
        while (i <  N_NUMBERS) {
            scanf("%d", &array[i]);
            i = i + 1;
        }
        int count = 0;
        int match;
        printf("Enter a number: ");
        scanf("%d", &match);
        int j = N_NUMBERS - 1;
        while (j >= 0) {
            if (array[j] == match) {
                count = count + 1;
            }
            j = j - 1;
        }
        printf("%d occurred %d times in the %d numbers read\n", match, count, N_NUMBERS);
        return 0;
    }
    
    
  10. 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 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.

  11. 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. And 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.

    Sample solution for middle.c
    #include <stdio.h>
    
    #define MAX_NUMBERS 10000
    
    int main(void) {
    
        int numbers[MAX_NUMBERS];
        int num_read = 1;
        int length = 0;
    
        while (num_read == 1 && length < MAX_NUMBERS) {
            num_read = scanf("%d", &numbers[length]);
            if (num_read == 1) {
                length = length + 1;
            }
        }
    
        printf("The middle number is %d\n", numbers[length / 2]);
        return 0;
    }
    
    
  12. Write a C function that takes in an array of integers, and returns the average.
    double average(int numbers[], int num_elements);
    
    The main thing to look out for here is the integer division, when the function wants you to return a double.
    double average(int numbers[], int num_elements) {
        int i = 0;
        int sum = 0;
        while (i < num_elements) {
            sum = sum + numbers[i];
            i = i + 1;
        }
        return (sum * 1.0) / num_elements;
    }