COMP1721 - Higher Computing 1B

Computing 1B - Week 04 Tutorial Solutions

  1. #include <stdio.h>
    #include <ctype.h>
    
    int
    is_vowel(int c) {
        c = tolower(c);
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }
    
    int
    main(int argc, char *args[]) {
        int  max_line = 80;
        char line[80];
        int  i;
        
        printf("Enter a string: ");
        fgets(line, max_line, stdin);
        for (i = 0; line[i] != '\0'; i++)
             if (!is_vowel(line[i]))
                printf("%c", line[i]); /* or putchar(line[i]) */
        return 0;
    }
    

  2. #include <stdio.h>
    #include <ctype.h>
    
    int
    main(int argc, char *args[]) {
    	int  max_line = 80;
        char line[80];
        int  i;
        
        while (1) {
    		if (fgets(line, max_line, stdin) == NULL)
    			break;
    		for (i = 0; line[i] != '\0' && line[i] != '\n'; i++)
    			;
    		for (i--; i >= 0; i--)
             	printf("%c", line[i]); /* or putchar(line[i]) */
             printf("\n");
        }
    	return 0;
    }
    

  3. #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