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?
  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]; ?
  4. How would you declare a variable squares to be an array of integers with 15 elements?

    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.

  5. Write a function with this prototype
    int array_length(int nums[])
    
    which returns the number of elements in the array nums.
  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.
  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.
  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.

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

  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.

  12. Write a C function that takes in an array of integers, and returns the average.
    double average(int numbers[], int num_elements);