COMP1511 17s1 Introduction to Programming
    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-8.
  2. Did you blog last week? What is this week's blogging theme?
    See the class home page.
  3. How is the game of Farnakle played?
    See the lab description.
  4. Calculate the number of farnarkles and arkles for each pair of these sequences:
    Hidden SequenceGuess
    4 4 4 44 4 4 4
    1 1 1 14 4 4 4
    1 4 1 14 4 1 4
    1 1 4 14 4 1 4
    1 2 3 44 3 2 1
    1 2 3 42 1 3 4
    5 6 7 86 6 7 7
    1 8 2 77 8 1 2
    1 1 2 34 1 1 1
    1 2 2 12 2 2 2
    Hidden SequenceGuessFarnarklesArkles
    4 4 4 44 4 4 4 4 0
    1 1 1 14 4 4 4 0 0
    1 4 1 14 4 1 4 2 0
    1 1 4 14 4 1 4 0 2
    1 2 3 44 3 2 1 0 4
    1 2 3 42 1 3 4 2 2
    5 6 7 86 6 7 7 2 0
    1 8 2 77 8 1 2 1 3
    1 1 2 34 1 1 1 1 1
    1 2 2 12 2 2 2 2 0
  5. What does this code print and why:
    srand(time(NULL));
    printf("%d\n", rand() % 6 + 1);
    

    A pseudo-random integer in the range 1..6 inclusive.

    srand(time(NULL)) initialized the random number generation with the current time

    rand() returns a pseudo-random integer

  6. What is an array?
    An array is a collection of elements with the same data type. Each element is accessed providing the name of the array and an index. The index range is from 0 through to N-1, where N is the number of elements in the array. This is also known as zero-based indexing.
  7. Give an expression that sums the first and third element of an array called numbers
    numbers[0] + numbers[2]
    Note third element is accessed using numbers[2] since C uses zero based indexing.
  8. 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.

  9. 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;
    
        // The next two lines is what the question asks you to do
        i = 0;
        while (i < ELEMENTS) {
            squares[i] = i * i;
            i = i + 1;
        }
        // Let's print the array to check
        i = 0;
        while (i < ELEMENTS) {
            printf("squares[%d] has the value %d\n", i, squares[i]);
            i = i + 1;
        }
        return 0;
    }
    
    
  10. 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' for strings.
    3. Pass an array of a specific length to the function - e.g. always pass arrays of 20 elements
  11. 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.

  12. Write a C function that searches for a given int in an array and returns 0 if the array contains the int and 1 otherwise. Your function should use the following prototype
    int not_in_int_array(int nums[], int n, int x);
    
    int not_in_int_array(int nums[], int n, int x) {
        int i;
        i = 0;
        while (i < n) {
            if (nums[i] == x) {
                return 0;
            }
            i = i +  1;
        }
        return 1;
    }
    
  13. Write a C function that that calculates the dot-product of two vectors of integers. Your function should use the following prototype
    int dot_product(int vector1[VECTOR_LENGTH], int vector2[VECTOR_LENGTH])
    

    Reminder: you calculate the dot product by multiplying corresponding elements and summing the result.

    So the dot-product of 1 3 1 3 2 and 2 1 2 1 2 is 14.

    Sample solution for dot_product includsing a main function for testing.
    #include <stdio.h>
    
    #define VECTOR_LENGTH 10
    
    int dot_product(int vector1[VECTOR_LENGTH], int vector2[VECTOR_LENGTH]) {
        int i, dot_product;
    
        dot_product = 0;
        i = 0;
        while (i < VECTOR_LENGTH) {
            dot_product = dot_product + vector1[i] * vector2[i];
            i = i + 1;
        }
    
        return dot_product;
    }
    
    int main(void) {
        int vector1[VECTOR_LENGTH];
        int vector2[VECTOR_LENGTH];
        int i;
    
        printf("Enter vector 1 of %d positive numbers: ", VECTOR_LENGTH);
        i = 0;
        while (i < VECTOR_LENGTH) {
            scanf("%d", &vector1[i]);
            i = i + 1;
        }
    
        printf("Enter vector 2 of %d positive numbers: ", VECTOR_LENGTH);
        while (i < VECTOR_LENGTH) {
            scanf("%d", &vector2[i]);
            i = i + 1;
        }
    
    
        printf("Their dot-product is %d.\n", dot_product(vector1, vector2));
        return 0;
    }
    
    

    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.

  14. Write a C function that returns the maximum value found in an array of doubles.

    It should have this prototype:

    double max_value(double array[], int length)
    
    Sample solution for max_value includsing a main function for testing.
    #include <stdio.h>
    
    #define MAX_NUMBERS 100000
    
    double max_value(double array[], int length) {
      double max = array[0];
      int i;
    
      i = 1;
      while (i < length) {
         if( array[i] > max ) {
            max = array[i];
         }
         i = i + 1;
      }
    
      return max;
    }
    
    int main(void) {
        double numbers[MAX_NUMBERS];
        int n_numbers, number_read;
    
        n_numbers = 0;
        number_read = 1;
        while (number_read == 1 && n_numbers < MAX_NUMBERS) {
            number_read = scanf("%lf", &numbers[n_numbers]);
            n_numbers = n_numbers + 1;
        }
        printf("Maximum of the numbers is %lf\n",  max_value(numbers, n_numbers));
        return 0;
    }
    
    
  15. Write a C function that returns 1 if an array of ints is in increasing order, 0 otherwise,

    It should have this prototype:

    int increasing(double array[], int length)
    
    Sample solution for increasing that includes a main function for testing.
    #include <stdio.h>
    
    #define MAX_NUMBERS 100000
    
    int increasing(int array[], int length) {
      int i;
    
      i = 1;
      while (i < length) {
         if (array[i - 1] >= array[i]) {
            return 0;
         }
         i = i + 1;
      }
    
      return 1;
    }
    
    int main(void) {
        int numbers[MAX_NUMBERS];
        int n_numbers, number_read;
    
        n_numbers = 0;
        number_read = 1;
        while (number_read == 1 && n_numbers < MAX_NUMBERS) {
            number_read = scanf("%d", &numbers[n_numbers]);
            n_numbers = n_numbers + 1;
        }
    
        if (!increasing(numbers, n_numbers-1)) {
            printf("The numbers are not in increasing order\n");
        } else {
            printf("The numbers are in increasing order\n");
        }
        return 0;
    }
    
    
  16. 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 5. 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 x[N_NUMBERS], i, j, match, count;
        printf("Enter %d numbers: ", N_NUMBERS);
        i = 0;
        while (i< N_NUMBERS) {
            scanf("%d", &x[i]);
            i = i + 1;
        }
        count = 0;
        printf("Enter a number: ");
        scanf("%d", &match);
        j = N_NUMBERS - 1;
        while (j >= 0) {
            if (x[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;
    }
    
    
  17. 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 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.

  18. 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;
    }
    
    
  19. 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 funciton wants you to return a double.
  20. Write a C function that takes in an array of integers, and returns the difference between the smallest and largest numbers in it.
            double maxDifference(int numbers[], int num_elements);