COMP1721 - Higher Computing 1B

Computing 1B - Week 03 Tutorial Solutions

  1. This C:


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

    produces this output:
    10
    18
    24
    28
    30
    

    Each execution of the loop produces a line of output containing one number. The first time through the loop, x is 1, y is 10, and so z is 10. Then x is incremented to 2, y is decremented to 9, and the loop repeats. This time, the output is 2*9, or 18. This continues until x becomes 6 and y becomes 5.

  2. #include <stdio.h>
    
    int
    main(void) {
        int i,n;
        double total;
            
        printf("How many times should I roll the dice? ");
        scanf("%d", &n);
    
        total = 0.0;
        for (i = 1; i <= n; i++) {
            total += 1.0/i;
        }
        printf("%f\n", total);
        return 0;
    }
    

  3. /*
     * estimate pi using the series 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + .....
     */
    #include <stdio.h>
    
    int
    main(void) {
        double MINIMUM_TERM = 0.000001;
        int i, sign;
        double sum, term;
    
        sum = 0.0;
        sign = 1;
        
        for (i = 1; ; i += 2) {
            term = 4.0 / i;
            if (term < MINIMUM_TERM)
                break;
            sum += sign * term;
            sign *= -1;
        }
        printf("Estimate of pi is %f\n", sum);
        return 0;
    }
    

  4. First:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int
    main(void) {
        int die1, die2, total; 
            
        srand(time(NULL));
        die1 = 1 + (int)(rand() % 6);
        die2 = 1 + (int)(rand() % 6);
        total = die1 + die2;
        printf("You rolled %d and %d = %d\n", die1, die2, total);
        return 0;
    }
    
    Second:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int
    main(void) {
        int die1, die2, total, i, count; 
            
        srand(time(NULL));
        printf("How many times should I roll the dice? ");
        scanf("%d", &count);
        for (i = 0; i < count; i++) {
            die1 = 1 + (int)(rand() % 6);
            die2 = 1 + (int)(rand() % 6);
            total = die1 + die2;
            printf("You rolled %d and %d = %d\n", die1, die2, total);
        }
        return 0;
    }
    
    Third:
    #include <stdlib.h>
    #include <time.h>
    
    int
    main(void) {
        int die1, die2, total, i, count, sevenCount;
            
        srand(time(NULL));
        printf("How many times should I roll the dice? ");
        scanf("%d", &count);
    
        sevenCount = 0;
    
        for (i = 0; i < count; i++) {
            die1 = 1 + (int)(rand() % 6);
            die2 = 1 + (int)(rand() % 6);
            total = die1 + die2;
            printf("You rolled %d and %d = %d\n", die1, die2, total);
            if (total == 7)
                sevenCount++;
        }
        printf("%d 7s were rolled\n", sevenCount);
        return 0;
    }
    

  5. #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int
    main(void) {
        int die1, die2, total, i, count;
        int tallies[13];
          
        for (i = 2; i <= 12; i++)
            tallies[i] = 0;
    
        srand(time(NULL));
        printf("How many times should I roll the dice? ");
        scanf("%d", &count);
    
    
        for (i = 0; i < count; i++) {
            die1 = 1 + (int)(rand() % 6);
            die2 = 1 + (int)(rand() % 6);
            total = die1 + die2;
            printf("You rolled %d and %d = %d\n", die1, die2, total);
            tallies[total]++;
        }
        for (i = 2; i <= 12; i++) {
            if (tallies[i] > 0)
                printf("%d %ds were rolled\n", tallies[i], i);
        }
        return 0;
    }
    


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