COMP1721 - Higher Computing 1B

Computing 1B - Week 14 Tutorial Questions

  1. Write a C program triangle.c which:

    Make your program behave exactly as indicated by the examples below.

    It must produce exactly the same output as below.

    You can assume the input contains one postive integer and nothing else.

    % a.out
    Enter the triangle size: 1
    *
    % a.out
    Enter the triangle size: 2
    *
    **
    % a.out
    Enter the triangle size: 4
    *
    **
    ***
    ****
    % a.out
    Enter the triangle size: 7
    *
    **
    ***
    ****
    *****
    ******
    *******
    

  2. Write a C program binary_ones.c which reads non-negative integers and returns a count of how many times the digit one occurs in their binary representation.

    For example, the binary representation of 42 is 101010. Hence the digit one occurs 3 times in its representation.

    Each integer will be entered one per line.

    The only functions from the C library you are permitted to use are scanf and printf. Do not use other functions from the C library.

    Your program should terminate when end of input is reached.

    You can assume the input is correct and you can assume contains one integer and nothing else.

    Your program should behave exactly like this:

    % a.out
    Enter number: 42
    3
    Enter number: 255
    8
    Enter number: 9
    2
    Enter number: 15
    4
    

  3. The weight of a number in a list is its value multiplied by how many times it occurs in the list. Consider the list 1 6 4 7 3 4 6 3 3. The number 7 occurs once so it has weight 7. The number 3 occurs 3 times so it has weight 9. The number 4 occurs twice so it has weight 8.

    Write a function heaviest which given a non-empty linked list of positive integers returns the integer with the largest weight.

    The heaviest integer in the list 7 5 7 4 5 5 is 5.

    The heaviest integer in the list 1 2 2 3 3 3 42 is 42.

    The heaviest integer in the list 1 6 4 7 4 6 3 3 is 6.

    Each item in a linked list will be stored in a struct of this type:

    struct node {
        int         value;
        struct node *next;
    }
    

    Your function heaviest will have one parameter, a pointer to the first item in the list. You can assume this list has at least one item in it. You may assume that there are no negative integers in the list.

    You may write extra functions, if you wish, in answering this questions.

    Your function heaviest must have this prototype:

    int heaviest(struct node *first);
    

    You can assume there is at least one integer in the list. You can assume that all integers in the list are greater than 0.

    You are not permitted to use arrays anywhere in your answer to this question. You are not permitted to use any functions from the C library in answering this question.

    You may use printf or other functions from the C library when debugging your answer. You must comment these out in the final version.

  4. We have a list of integers. We would like to know if this list contains 1 or more numbers which together sum to 21. The numbers may occur anywhere in the list. They do not have to be consecutive.

    You will write a C program twenty_one.c to determine this. The numbers will be provided to your program as arguments

    You program should print a message either indicating no combination of any of the numbers given sums to 21 or giving a giving a combination of the numbers which does sum to 21.

    Your program should follow the output format indicated below exactly.

    % a.out 10 3 10 1
    10 + 10 + 1 = 21
    % a.out 12 1 25 7
    No combination of the numbers given sums to 21
    % a.out 3 3 3 3 3 3 3 3 3 3 
    3 + 3 + 3 + 3 + 3 + 3 + 3 = 21
    % a.out 9 13 -1 -10
    9 + 13 + -1 = 21
    


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