COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)
    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. Quickly discuss mistakes students are making working on the assignment.

  3. What is an array?

  4. Give an expression that sums the first and third element of an array called numbers

  5. 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]; ?

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

  7. How is the game of Farnarkle played?

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

  9. You have to complete these 3 functions from farnarkle,c in the lab:
    int count_farnarkles(int tiles1[N_TILES], int tiles2[N_TILES]);
    int count_arkles(int tiles1[N_TILES], int tiles2[N_TILES]);
    void play_farnarkle(int hidden_tiles[]);
    
    what do they do?
  10. Write a function with this prototype:
    void print_tiles(int tiles[N_TILES]);
    
    which prints an array of N_TILES ints (representing Farnarkle tiles) separated by spaces

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

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

  13. Write a function with this prototype:
    int read_tiles(int tiles[N_TILES]);
    
    which reads N_TILES ints and stores them in the array tiles

    read_tiles should check each int is a valid Farnarkle tile.

    If read_tiles reads an invalid int or is unable to read an int it should return.

    The return value of read_tiles should be the number of ints successfully read.

  14. Write a function with this prototype
    int array_length(int nums[])
    
    which returns the number of elements in the array nums.

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

    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.

  16. 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)
    

  17. 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)
    

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

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

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

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

  22. Write a C function that takes in an array of integers, and returns the difference between the smallest and largest numbers in it.
            double max_difference(int numbers[], int num_elements);