COMP1511 17s1 Introduction to Programming
    The tutorial will start with a code review.

    Your tutor has asked a lab pair to present their week 3 work.

    Discuss the good, the bad and the ugly aspects of their code.

    Please be gentle in any criticism - we are all learning!

  1. New lab pairs - you will have a new lab partner for weeks 5-8.

    Your tutor will facilitate the formation of new pairs.

    Please cooperate with them.

  2. Blogging starts this week - what do you have to do?
  3. What is the value of an uninitialized variable?

    What sort of problems can be caused by using uninitialised variables in programs?

    Why do programs with uninitialised variables apparently work?

    An uninitialized local variable holds an undefined value.

    It is not legal to examine the value of an uninitialized variable and hence doing so may result in arbitrary behaviour from your program including immediate termination.

    In some circumstances malicious users can exploit uninitialized variables as asecuriy hole.

    In many circumstances uninitialized variables result in unpredictable and unreliable behaviour.

    In practice the value will be what happens to be stored at the memory address the variable is allocated - often a seemingly random value.

    Often this value will differ between different machines.

    Often this value will differ between different compilers.

    Often this value will differ with different compiler options.

    Sometimes this value will differ even between executions.

    Often the memory address the variable is allocated to will happen to contain zero and it is common that this is the value that the variable should have been initialized to.

    The combination results in the program working.

  4. Consider the following program square.c
    #include <stdio.h>
    
    int main(void) {
        int number;
        int row, column;
    
        // Obtain input
        printf("Enter size: ");
        scanf("%d", &number);
    
        row = 1;
        while (row <= number) {
            column = 1;
            while (column <= number) {
                printf("*");
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
    The output if the user types in the number 5 is:
     ./square
    Enter size: 5
    *****
    *****
    *****
    *****
    *****
    
    Modify the program so that it prints out a triangle like this:
     ./triangle
    Enter number: 5
    ----*
    ---**
    --***
    -****
    *****
    

    Sample solution for triangle.c
    // Written 14/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
    // as a lab example for COMP1511
    
    // Print an nxn "plus" pattern of asterisks and dashes
    //
    // For example here is the output for n == 9
    //
    // --------*
    // -------**
    // ------***
    // -----****
    // ----*****
    // ---******
    // --*******
    // -********
    // *********
    
    #include <stdio.h>
    
    int main(void) {
        int size, n_numbers_read;
        int row, column;
    
        printf("Enter size: ");
        n_numbers_read = scanf("%d", &size);
    
        if (n_numbers_read != 1) {
            // scanf failed to read a number
            return 1;
        }
    
        row = 0;
        while (row < size) {
            column = 0;
            while (column < size) {
                if (column > size - row - 2) {
                    printf("*");
                } else {
                    printf("-");
                }
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
    Now modify so it prints the following pattern:
    ./diagonal
    Enter an integer: 10
    *---------
    -*--------
    --*-------
    ---*------
    ----*-----
    -----*----
    ------*---
    -------*--
    --------*-
    ---------*
    
    Sample solution for diagonal.c
    // Written 14/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
    // as a lab example for COMP1511
    
    // Print an nxn "plus" pattern of asterisks and dashes
    //
    // For example here is the output for n == 9
    //
    // *--------
    // -*-------
    // --*------
    // ---*-----
    // ----*----
    // -----*---
    // ------*--
    // -------*-
    // --------*
    
    #include <stdio.h>
    
    int main(void) {
        int size, n_numbers_read;
        int row, column;
    
        printf("Enter size: ");
        n_numbers_read = scanf("%d", &size);
    
        if (n_numbers_read != 1) {
            // scanf failed to read a number
            return 1;
        }
    
        row = 0;
        while (row < size) {
            column = 0;
            while (column < size) {
                if (row == column) {
                    printf("*");
                } else {
                    printf("-");
                }
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
    Now modify so it prints the following pattern:
    ./bars
    Enter an integer: 9
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    -*-*-*-*-
    
    Sample solution for bars.c
    // Written 14/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
    // as a lab example for COMP1511
    
    // Print an nxn "bars" pattern of asterisks and spaces
    //
    // For example here is the output for n == 9
    //
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    // -*-*-*-*-
    
    #include <stdio.h>
    
    int main(void) {
        int size, n_numbers_read;
        int row, column;
    
        printf("Enter size: ");
        n_numbers_read = scanf("%d", &size);
    
        if (n_numbers_read != 1) {
            // scanf failed to read a number
            return 1;
        }
    
        if (size < 5 || size % 2 != 1) {
            printf("Error: size has to be odd and >= 5.\n");
            return 1;
        }
    
        row = 0;
        while (row < size) {
            column = 0;
            while (column < size) {
                if (column % 2 == 1) {
                    printf("*");
                } else {
                    printf("-");
                }
                column = column + 1;
            }
            printf("\n");
            row = row + 1;
        }
    
        return 0;
    }
    
    
  5. Write a C program log10.c which reads a positive integer, and calculates the integer part of its base 10 logarithm without using functions from the maths library.

    Hint: repeatedly divide by 10.

    Sample solution for log10.c
    // Written 14/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
    // as a tut example for COMP1511
    
    // calculate log10 of an int
    
    #include <stdio.h>
    
    int main(void) {
        int x, y, log_ten;
    
        printf("Enter a positive integer: ");
        scanf("%d", &x);
    
        log_ten = 0;
        y = x;
        while (y >= 10) {
            y = y / 10;
            log_ten = log_ten + 1;
        }
        printf("log10 of %d is %d\n", x, log_ten);
    
        return 0;
    }
    
    

    Revision questions

    The remaining tutorial questions are primarily intended for revision - either this week or later in session.

    Your tutor may still choose to cover some of the questions time permitting.

  6. Write a program that reads two integers (height x length) and prints a rectangular, asterisk outline with the specified dimensions, for example:
    ./rectangle
    Enter rectangle height and length: 3 5
    *****
    *   *
    *****
    
    Sample solution for rectangle.c
    #include <stdio.h>
    
    int main(void){
        int height, length;
        int row, column;
    
        printf("Enter rectangle height and length: ");
        scanf("%d", &height);
        scanf("%d", &length);
    
        row = 0;
        while (row < height) {
            column = 0;
            while (column < length) {
    
                // If the index is on one of the edges, print *.
                // Otherwise, print a space.
                if (row == 0 || row == height - 1 || column == 0 || column == length - 1) {
                    printf("*");
                } else {
                    printf(" ");
                }
    
                column = column + 1;
             }
             printf("\n");
             row = row + 1;
        }
        return 0;
    }
    
    

  7. For the program above consider and discuss the ways in which you would test their correct behaviour. Then extend the program to include error checking for, and handling of, invalid input.
    Sample solution for rectangle1.c
    #include <stdio.h>
    
    int main(void){
        int height, length;
        int row, column;
        int valuesRead;
    
        printf("Enter rectangle height and length: ");
        valuesRead = scanf("%d%d", &height, &length);
    
        if (valuesRead != 2) {
            printf("Two integers must be supplied as input.\n");
        } else if (height < 1 || length < 1) {
            printf("It is impossible to draw a rectangle with the given height and length.\n");
        } else {
            row = 0;
            while (row < height) {
                column = 0;
                while (column < length) {
    
                    // If the index is on one of the edges, print *.
                    // Otherwise, print a space.
                    if (row == 0 || row == height - 1 || column == 0 || column == length - 1) {
                        printf("*");
                    } else {
                        printf(" ");
                    }
    
                    column = column + 1;
                 }
                 printf("\n");
                 row = row + 1;
            }
        }
    
        return 0;
    }
    
    
  8. Design and write a program that reads an integer n and prints a diamond asterisk outline with side length n, for example:
    ./diamond
    Enter side length: 3
      *
     * *
    *   *
     * *
      *
    ./diamond
    Enter side length: 6
         *
        * *
       *   *
      *     *
     *       *
    *         *
     *       *
      *     *
       *   *
        * *
         *
    
    Sample solution for diamond.c
    #include <stdio.h>
    
    int main(void){
        int side;
        int row, column;
    
        printf("Enter side length: ");
        scanf("%d", &side);
    
        row = 0;
        while (row < side * 2 - 1) {
            column = 0;
            while (column < side * 2 - 1) {
                if (row <= (side - 1)) {
                    if (column == (side - 1) - row || column  == (side - 1) + row) {
                        printf("*");
                    } else {
                        printf(" ");
                    }
                } else {
                    if (column == row - (side - 1) || column  ==  3 * (side - 1) - row) {
                        printf("*");
                    } else {
                        printf(" ");
                    }
                }
                column = column + 1;
             }
             printf("\n");
             row = row + 1;
        }
        return 0;
    }
    
    
  9. Write a program that repeatedly reads in integers until a non-integer is read in and then prints the number of integers read in. For example:
    ./readInts
    Please enter some integers:
    10 -90 100 999 78hello
    You entered 5 integers
    
    ./readInts
    Please enter some integers:
    1 2 3 4 5 6 7 8 9
    10 11 12 hello
    You entered 12 integers
    
    Sample solution for read_until_non_integer.c
    #include <stdio.h>
    
    #define N 10
    
    int main(void) {
       int n, returnCode;
       int count;
    
       printf("Please enter some integers:\n");
       count = 0;
       returnCode = scanf("%d", &n);
       while (returnCode == 1) {
           count = count + 1;
           returnCode = scanf("%d", &n);
       }
       printf("You entered %d integers\n", count);
    
       return 0;
    }
    
    
  10. Write a program that reads in an integer n, and then prints a nxn multiplication table. For example:
    ./multiplication_table
    Enter multiplication table size: 5
     1|   1    2    3    4    5
     2|   2    4    6    8   10
     3|   3    6    9   12   15
     4|   4    8   12   16   20
     5|   5   10   15   20   25
    
    Sample solution for multiplication_table.c
    #include <stdio.h> 
    
    int main(void){
    
    	int row = 1, col = 1; 
    	int table_size; 
    
    
    	printf("Enter multiplication table size: "); 
    	scanf("%d", &table_size); 
    
    	while (row <= table_size){
    		col = 1; 
    		while(col <= table_size){
    			if (col == 1){
    				printf("%2d|", row); 
    			}
    			printf("%4d ", row * col); 
    			col++; 
    		}
    		printf("\n"); 
    
    		row++; 
    	}
    	return 0;
    }
    
  11. Write a program (a small game), which generates a random number for the program-user to guess. For example:
    ./guess_number
    Random number is between 1 and 100.
    Enter your guess: 50
    Random number is between 1 and 50.
    Enter your guess: 40
    Random number is between 1 and 40.
    Enter your guess: 37
    
    Yay, you guessed the number 37 correctly!
    
    
    ./guess_number
    Random number is between 1 and 100.
    Enter your guess: 80
    Random number is between 80 and 100.
    Enter your guess: 90
    Random number is between 80 and 90.
    Enter your guess: 85
    Random number is between 80 and 85.
    Enter your guess: 83
    Random number is between 80 and 83.
    Enter your guess: 82
    
    Yay, you guessed the number 82 correctly!
    
    Sample solution for guess_number.c
    // let a user guesses a random number 1..100 inclusive .
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX 100
    
    int main(void) {
        int guess;
    
        // seed pseudo-random number generator with current time
        srand(time(NULL));
    
        // rand() returns a  pseudo-random number
    
        int random_number = 1 + rand() % MAX;
        int min = 1;
        int max = MAX;
    
        // set guess to an impossible value so loop will be entered
    
        guess = -1;
        while (guess != random_number) {
            printf("Random number is between %d and %d.\n", min, max);
            printf("Enter your guess: ");
    
            // we should check the scan successds here
            scanf("%d", &guess);
    
            if (guess > random_number && max > guess){
                max = guess;
            }
            if (guess < random_number && min < guess){
                min = guess;
            }
        }
    
        printf("\nYay, you guessed the number %d correctly!\n\n", guess);
    
        return 0;
    }