COMP1721 - Higher Computing 1B

Computing 1B - Week 05 Tutorial Questions

  1. Write a function that given an int between 0 and 65535 (inclusive), prints it out as a 16 bit number.

    For example, given 42 as argument it should print "0000000000101010"?

  2. Write a function that given a char containing a hexadecimal digit returns its numerical value. In other words. it returns as an intbetween 0 and 15 (inclusive).

  3. Write a function that given a char array containing a hexadecimal digits returns their numerical value. The prototype of this function should be:
    
    hex_digit_array_to_int(char c[], int n_digits)
    
    and, for example, this code segment should print 42:
    
    	char array[] = {'2','A'};
    	printf("%d\n", hex_digit_array_to_int(array, 2));
    

    Modify your function so it converts only a specified segment of the char array. The prototype of you function should now be:

    
    hex_digit_array_to_int(char c[], int offset, int n_digits)
    
    and, for example, this code segment should print 42:
    
    	char array[] = {'F','F','F', '2','A','E','E'};
    	printf("%d\n", hex_digit_array_to_int(array, 3, 2));
    

  4. Write a function which converts an array of byte values to 16-bit words assuming little-endian order. It should have this prototype:
    
    convert_bytes_to_little_endian_words(int bytes[], int n_bytes, int words[])
    

  5. Write a function which read a line from an ihex file. and stores the byte values in an array supplied as argument. It should have this prototype:
    
    read_ihex_line(FILE *f, int bytes[], int bytes_array_size);
    

  6. Write a program which given an ihex file as argument prints it out as 16-bit little-endian words. For example:

    
    % a.out /home/cs1721/public_html/05s2/emu/examples/iota.hex
        0 1110000000000000
        1 1110000010000001
        2 0010111110100000
        3 1110011111110000
        4 1110000011100000
        5 1001010100001001
        6 1110000000010001
        7 0000111100000001
        8 1110000000011010
        9 0001101100010000
       10 1111011110110001
       11 1110000010000000
       12 1110011111110000
       13 1110000011100000
       14 1001010100001001
    

  7. Write a single C statement which declares an integer array of four elements and assign the elements the initial values of 7, 22, 31 and -44.

  8. You are given a variable nums which refers to an array of ints.

    Write C statements to find the smallest element in the array of int nums of size N and print out its value. You can assume the array contains at least one element.

    Here is are two analogous Haskell functions:

    smallest::[Int] -> Int
    smallest nums
        | length nums == 1  = h
        | h < minRest       = h
        | otherwise         = minRest
          where
             h = head nums
             minRest = smallest (tail nums)
             
    smallest1 :: [Int] -> Int
    smallest1 [x] = x
    smallest1 (x:xs) = x `min` (smallest1 xs)
    

  9. Magic squares are squares that contain numbers arranged in such a way that all the rows columns and the two long diagonals add up to the same number.

    Here is an example of a magic square of size 3:

    618
    753
    294

    and an example of a magic square of size 5:

    15812417
    16147523
    22201364
    321191210
    92251811

    Write a C function which takes a 2-dimensional array of ints and returns true iff it contains a magic square.

    You can assume the constant SIZE contains the size of the array.

  10. You are given two arrays of length N, code and guess, which contain only positive integers.

    Write a C function that determines the number of bulls (equal values in corresponding positions) and cows (equal values in non-corresponding positions). A value can only be counted once, bulls being checked first.

    Hint: replace matched values by a special distinguished value.

    Example 1: [1, 3, 5, 4] and [6, 3, 1, 5] have 1 bull (the 3) and 2 cows (the 1 and 5) in common.

    Example 2: [1, 1, 1, 2] and [2, 1, 2, 2] have 2 bulls only.

  11. Write a C Program which reads in a number and then prints out a magic square of that size.

    Be warned this is difficult.

    For example:

    % a.out
    Enter the size of the magic square you wish to create: 7
    28 19 10  1 48 39 30 
    29 27 18  9  7 47 38 
    37 35 26 17  8  6 46 
    45 36 34 25 16 14  5 
     4 44 42 33 24 15 13 
    12  3 43 41 32 23 21 
    20 11  2 49 40 31 22 
    


Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW