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

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

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

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

  1. Did you blog last week? What is this week's blogging theme?

  2. Discuss the assignment including:
    • what is a CAPTCHA and what they are used for
    • what exactly are the inputs and output of crack_digit and crack_captcha
    • what is a PBM file
    • what do you have to do get a PS (50%) CR (65%), DN (75%) & HD (85%) on the assignment
  3. A paranoid student wants to check that read_pbm is only giving them black & white pixels, in other words all elements of the array pixels contain either 1 or 0. Write a function to check this with this prototype:

    int is_monochrome(int height, int width, int pixels[height][width]);
    

    Your function should return 1 to indicate all elements of the array are 1 or 0. It should return 0 otherwise.

  4. A student working on assignment 1 wrote code like this:
        density = pixel_count / (height * width);
    
    to calculate an attribute suggested in the assignment spec. When they test their code they find that density is always 0.

    They are mystified becaused they are calculating pixel_count, height and width correctly. What is happening?

  5. What is the end-of-file (EOF) character in C?

  6. When do you have a use a for loop?

    When are for loops preferable?

  7. Name 3 errors in this program:
    #include <stdio.h>
    
    #define MAX_LINE 4096
    
    int main(void) {
        char line[MAX_LINE];
        int  i;
    
        while (fgets(line, MAX_LINE, stdin) != NULL) {
            i = MAX_LINE;
            while (line[i] != '\n') {
                i = i - 1;
            }
            printf("line %d characters long\n", i);
        }
        return 0;
    }
    

  8. Write a program line_length.c which reads lines from its input and prints how many characters each line contains.

    The only functions you can use are fgets and printf.

    You can assume lines contain at most 4096 characters.

    For example:

    ./line_length
    Andrew Rocks
    line 12 characters long
    A very long line.
    line 17 characters long
    short
    line 5 characters long
    
    line 0 characters long
    

  9. Write a program strip_comments.c which reads lines from its input and prints them after removing any C // style comments. In another words if the line contains // it does not print the // or anything after it.

    The only functions you can use are fgets and printf.

    You can assume lines contain at most 4096 characters.

    For example:

    ./strip_comments
         x = x + 1;  // This means add one to the variable x
         x = x + 1;
    
    Also - is that a good comment to add to a C program?

    What if the input contains printf("//");?

  10. Write a program filter_empty_lines.c which reads lines from its input and prints them only if they contain a non-white-space-character.

    In another words remove lines are empty or contain only white-space.

    The only functions you can use are fgets and printf.

    You can assume lines contain at most 4096 characters.

    You can assume there are only 3 white space characters, space, tab & new-line.

    For example:

    ./filter_empty_lines
    full line
    full line
             
    another no-empty line
    another no-empty line
    

  11. Write a C program reverse.c which reads lines and writes them out with the characters of each line in reverse order. It should stop when it reaches the end of input.

    For example:

    ./reverse
    The quick brown fox jumped over the lazy dog.
    .god yzal eht revo depmuj xof nworb kciuq ehT
    It was the best of times. It was the worst of times.
    .semit fo tsrow eht saw tI .semit fo tseb eht saw tI
    This is the last line.
    .enil tsal eht si sihT
    <control-d>
    

    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.

  12. Calculate the density of a block. The density can be defined as the number of black pixels divided by the total number of pixels in the block.
    double get_density(int height, int width, int pixels[height][width]);
        
  13. Given a 2d array, rotate all the pixels inside of that clockwise by the rotation number, which can be positive or negative. A clockwise rotation of pixels is simply a circlular rotation of the pixels in the array. A general approach would be to start with the outer layer, and rotate all the values in it around by the rotation amount. To rotate, think about doing this one pixel at a time. (If you get this working with a loop, you can think about making it more efficient by shifting more pixels at a time). Once you have done the outer layer, continue to the inner layers until you get to the middle. Be careful of the corners, since the pixels in the bottom right need to end up in the bottom and shift left.
    void rotate_array(int height, int width, int pixels[height][width], int rotation);
        
  14. Given an array of characters and a string, determine if you can build that string from the array of characters. Return 1 if you can, 0 otherwise. You cannot reuse a character (althought there might be multiple in the array). If you have unused characters from your set, then that is ok.
    int scrabble(char letters[], int num_letters, char *string);