COMP1721 - Higher Computing 1B

Computing 1B - Week 02 Tutorial Solutions

  1. No. Indenting and other formatting is vital for humans reading C program but it is not required by the compiler.

  2. This C:


    /* The meaning of life is:
    printf("%d\n", 6*7);
    
    
    

    is incorrect and does not compile:
    example.c:1:35: unterminated comment
    example.c: In function `main':
    example.c:1: error: syntax error at end of input
    

  3. This C:


    
    answer = 42;
    printf("%d\n", answer);
    

    is incorrect and does not compile:
    example.c: In function `main':
    example.c:2: error: `answer' undeclared (first use in this function)
    example.c:2: error: (Each undeclared identifier is reported only once
    example.c:2: error: for each function it appears in.)
    

    All variables must be declared before they are used.

  4. This C:


    int x, y;
    x = 15;
    y = x;
    x = 27;
    printf("%d\n", y);
    

    produces this output:
    15
    

    The assignment y = x assigns the current value of x to y. It is not a declaration of equality. C is not Haskell!

  5. This C:


    int x;
    x = "hello world";
    printf("%d\n", x);
    

    is incorrect and does not compile:
    example.c: In function `main':
    example.c:2: warning: assignment makes integer from pointer without a cast
    example.c: In function `main':
    example.c:2: warning: assignment makes integer from pointer without a cast
    

    Only values of the appropriate type can be assigned to a variable.

  6. This C:


    int x;
    x = 42;
    printf("%d\n", X);
    

    is incorrect and does not compile:
    example.c: In function `main':
    example.c:3: error: `X' undeclared (first use in this function)
    example.c:3: error: (Each undeclared identifier is reported only once
    example.c:3: error: for each function it appears in.)
    

    Case is significant in C variable names.

  7. #include <stdio.h>
    
    int
    main(void) {
        int n1, n2, product;
     
        printf("Enter first number: ");
        scanf("%d", &n1);
        printf("Enter second number: ");
        scanf("%d", &n2);
        product = n1 * n2;
        printf("The product of %d and %d is %d\n", n1, n2, product);
        return 0;
    }
    

  8. The design for the program follows the idea ...
    	write a loop to iterate 100 times
    	in each iteration of the loop
    		print the message
    
        int i;
    	
        i = 0;
        while (i < 100) {
            printf("I must not talk during lectures\n");
            i = i + 1;        
        }
       

  9. A simple Haskell solution is
    sigma n m
     | n > m      = 0
     | otherwise  = n + sigma (n + 1) m
    
    another Haskell solution is
    sigma n m = sum [n..m]
    
    These C statements will print the sum of the numbers n to m:
    
            int number;
            int total;
    
            total = 0;
            number = n;
    
            while (number <= m) {
                total = total + number;
                number = number + 1;
            }
            
            printf("%d\n", total);
    

  10. A suitable Haskell function is:
    sigma n m
     | n > m      = 0
     | otherwise  = n + sigma (n + 1) m
    
    tetrahedral n
     | n == 1    = 1
     | n > 1     = (sigma 1 n) + (tetrahedral (n - 1))
     | otherwise  = error "tetrahedral: illegal argument"
    

    These C statements will print the first 100 tetrahedral numbers:

            int i,j, n, total;
    		
            n = 1;
    
            while (n <= 100) {
                total = 0;
                j = 1;
                
                
                while (j <= n) {
                	i = 1;
                	while (i <= j) {
                    	total = total + i;
                   	    i = i + 1;
                   	}
                   	j = j + 1;
                }
                printf("%d\n", total);
                n = n + 1;
            }
    


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