COMP 1917
Computing 1
Session 2, 2016

Tutorial - Week 3

Tutorial Presentation

Given a point P=(x,y) and a triangle ABC with A=(x1,y1), B=(x2,y2) and C=(x3,y3), how can you test whether the point P is inside or outside the triangle? Find out one or two methods for doing this and explain them to the class. (Note: you should explain them using mathematical formulas, not actual C code.)

Week 3 Tutorial Questions

  1. Write a program that given three numbers first, second and third, finds out if the numbers are in strictly descending order (first > second > third). Is there an alternative way to write this program?
     1 #include <stdio.h>
     2
     3 int main(int argc, char* argv[]){
     4    int first, second, third;
     5    printf("Enter numbers: ");
     6    scanf("%d %d %d", &first, &second, &third);
     7
     8    return 0;
     9 }
    
  2. Modify previous program to check if three numbers are in ascending or descending order.
  3. Write a program that allows the user to enter at integer and then prints out that many stars, each on a new line.
    % ./stars
    Enter : 5
    *
    *
    *
    *
    *
    
  4. Consider the following code:
     1 #include <stdio.h>
     2
     3 int main( void )
     4 {
     5    int num;
     6    int row, col;
     7
     8    // Obtain input
     9    printf("Enter number: ");
    10    scanf("%d", &num);
    
    11    for(col = 1; col <= num; col++){
    12       printf("*");
    13    }
    14    printf("\n");
    15
    16    for(row = 2; row < num; row++) {
    17        for(col = 1; col <= num; col++) {
    18            if(col == 1 || col == num){
    19                printf("*");
    20            }else{
    21                printf(" ");
    22            }
    23        }
    24        printf("\n"); // Start a new line
    25    }
    26
    27    for(col = 1; col <= num; col++){
    28       printf("*");
    29    }
    30      printf("\n");
    31
    32    return 0;
    33 }
    
    What is the output if the user types in the number 4?

    Modify the program so that it prints a triangle instead of a square:

    $ ./triangle
    Enter n ? 6
    *
    **
    ***
    ****
    *****
    ******
    
    How would you change your program to print the triangle in different orientations?
    (a) ******   (b)  ******    (c)     *
        *****          *****           **
        ****            ****          ***
        ***              ***         ****
        **                **        *****
        *                  *       ******
    
  5. Write a program that generates two random numbers between 1 and 107 and check if a smaller number is a factor of a larger.

Presentation Topic for Week 4

Describe the ASCII coding system. (What does ASCII stand for?) Draw a schematic "map" of the ASCII Table on the board, showing (in both decimal and hexadecimal notation) where the following sets of characters lie: