Week 02 Tutorial Sample Answers

  1. What are the types of variables that we've seen so far and what do they represent?
    Ints - whole numbers

    Doubles - floating point numbers

  2. What is the difference between a variable and a constant?
    A variable is a value with a specific type that we've saved in memory and we can change if we want.

    A constant is a value that cannot be changed.

  3. What is the difference between declaring variable and using #define?
    Declared variables get given a certain amount of memory to use to store a value. This allows them to be accessed again and changed.

    #define actually tells the compiler to replace all instances of the first symbol with the second. This means that it basically acts as a text replacement for the value and it doesn't actually get a specific piece of memory like a variable.

  4. What is the syntax of C if statements? What is the role of if statements in programs?
    If statements allow branching in programs, i.e., selecting and executing different blocks of code according to certain conditions.
  5. A leap year is a year that has 366 days instead of 365. There is a leap year every year divisible by four except for years which are both divisible by 100 and not divisible by 400.

    For more detail information of leap years see: Leap Year

    Draw a simple flow chart showing the branches of possibilities to decide if a year is a Leap Year.

    For some humourous information about flow charts see: Flow Charts (xkcd).

    Can you then write some simple code using if/else statements in C that decides if a year is a Leap Year? (hint: modulus is a useful way to decide whether a number is divisible by another) This code question will be continued in this week's lab, so try to remember or keep a copy/photo of the diagram and code you've written.

    A diagram drawn by groups in class.

    It should have some simple forks based on dividing by 4, dividing by 100 and 400.

    Code will be in this week's Lab Solutions.

  6. The value of C  arithmetic operations depends on the types of the operand. If the types of the operands differ, C automatically converts the types as appropriate.

    Determine the value of each expression and sub-expression:

    • 1 / 2 * 500
      0
    • 1 / 2.0 * 500
      250.0
    • (17 / 5) * 5 + (17 % 5)
      17
    • (12 - 17) % 6 - 4
      -9
  7. The and (&&) and or (||) operators can be used to combine multiple statments into one (such as the statements created in the leap year activity). Write either a true or false statement about this dog and share with the class. Are your classmates' statments true or false?
    A yellow, orange, and pink dog (Source: Jeffree Star Cosmetics, 2012)

    For example:

    • The dog is pink || blue
      True
    • The dog is pink && blue
      False
  8. Write a program rectangle_area.c that reads in 2 integers which are the side-length of a rectangle, and then prints the area of the rectangle.

    For example:

    ./rectangle_area
    Please enter rectangle length: 3
    Please enter rectangle width: 5
    Area = 15
    ./rectangle_area
    Please enter rectangle length: 42
    Please enter rectangle width: 42
    Area = 1764
    
    A sample solution for rectangle_area.c
    // Compute area of a rectangle using ints
    // Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
    // as a lab example for COMP1511
    
    // Note this program doesn't check whether
    // the two scanf calls successfully read a number.
    // This is covered later in the course.
    
    #include <stdio.h>
    
    int main(void) {
        int length, width, area;
    
        printf("Please enter rectangle length: ");
        scanf("%d", &length);
        printf("Please enter rectangle width: ");
        scanf("%d", &width);
    
        area = length * width;
    
        printf("Area = %d\n", area);
    
        return 0;
    }
    
  9. What is a bit?
    A bit is a binary value (0 or 1). The word bit is derived from binary digit.
  10. What is a byte?
    In modern computing a byte is simply 8 bits.

    If you are interested, read the more complex historical details

  11. How many distinct values can be represented with a single byte?
    2^8 = 256 possible bit patterns
  12. How would we represent integers using 4 bits? Is there some kind of scheme we could use to reliably store integer information using bits?
    This the more commonly used scheme(Two's complement):

    Bit PatternInteger
    00000
    00011
    00102
    00113
    01004
    01015
    01106
    01117
    1000-8
    1001-7
    1010-6
    1011-5
    1100-4
    1101-3
    1110-2
    1111-1
  13. What is a file?
    A file is basically an array (sequence) of bytes stored in a semi-permanent way.
  14. What is a directory?
    A directory is basically a set of files or directories.