COMP1721 - Higher Computing 1B

Computing 1B - Week 03 Tutorial Questions

  1. What would be the result of compiling and executing this C?

            int x = 1, y = 10;
            
            while (x < y) {
                printf("%d\n", x*y);
                x = x + 1;
                y = y - 1;
            }
    

  2. Write a C program which reads an integer n and prints the sum of the series:
    1 + 1/2 + 1/3 + .... + 1/n
    
    For example:
    % a.out
    Enter n: 2
    1.5
    % a.out
    Enter n: 10
    2.9289682539682538
    

  3. Write a C program computes an estimate of pi using this series:
    4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + .....
    
    You must invent a suitable stopping condition.

  4. This question involves simulating the rolling a pair of six-sided dice in C.

    Recall from lectures that the function rand() yields a pseudom-random int.

    First, write a C program which simulates rolling the pair of dice once. The output from your program should look like this:

    % a.out
    You rolled 1 and 2 = 3
    % a.out
    You rolled 5 and 5 = 10
    % a.out
    You rolled 6 and 2 = 8
    
    Second, modify the program to ask the user how many times to roll the dice. The output from your program should look like this:
    % a.out
    How many times should I roll the dice? 5
    You rolled 5 and 1 = 6
    You rolled 1 and 5 = 6
    You rolled 6 and 1 = 7
    You rolled 4 and 3 = 7
    You rolled 4 and 2 = 6
    
    Third, modify the program to also output a tally of the number of sevens rolled. The output from your program should look like this:
    %  a.out
    How many times should I roll the dice? 5
    You rolled 2 and 2 = 4
    You rolled 3 and 1 = 4
    You rolled 2 and 5 = 7
    You rolled 6 and 4 = 10
    You rolled 2 and 5 = 7
    2 7s were rolled
    

  5. This question involves simulating the rolling a pair of six-sided dice in C.

    Recall from lectures that the function rand() yields a pseudom-random int.

    Write a C program which asks the user how many times to roll a pair of 6 sided dice and prints a tally of all the numbers rolled. Don't output zero tallies. The output from your program should look like this:

    % a.out
    How many times should I roll the dice? 8
    You rolled 4 and 6 = 10
    You rolled 1 and 2 = 3
    You rolled 4 and 6 = 10
    You rolled 6 and 5 = 11
    You rolled 3 and 6 = 9
    You rolled 1 and 2 = 3
    You rolled 5 and 4 = 9
    You rolled 3 and 6 = 9
    2 3s were rolled
    3 9s were rolled
    2 10s were rolled
    1 11s were rolled
    

    Use an array to store the tallies.


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