COMP1511 17s1 Introduction to Programming
  1. What is a bit?
    A bit is a binary value (0 or 1). The word bit is derived from binary digit.
  2. What is a byte?
    In modern computing a byte is simply 8 bits.

    If you are interested, read the more complex historical details

  3. How many distinct values can be represented with a single byte?
    2^8 = 256 possible bit patterns
  4. Give a representation scheme that could be used to encode a subset of integers in 4 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
  5. What is a file?
    A file is basically an array (sequence) of bytes stored in a semi-permanent way.
  6. What is a directory?
    A directory is basically an set of files or directories.
  7. Give examples of information that is commonly stored inside the bytes of a file, and specify an encoding that might be used for the byte values.
    File nameContentsByte encoding
    READMEtext (English)ASCII
    a.outmachine codeELF
    banner.jpgImageJPEG
    chessboard.bmpImageBMP
    main.cC programASCII
    song.mp3Sound (music)MP3

    ASCII and BMP will be covered in COMP1511.

  8. Which of the following are valid variable names in C?

    If they are valid, would they be a good name?

    • THX1138 Valid, not good - doesn't start with a lower-case letter
    • 2for1 Invalid - doesn't start with a letter or underscore
    • mrBean Valid, good if the variable has something to do with Mr Bean
    • my space Invalid - you can't have spaces in variables names
    • event_counter Valid in C, good
    • ^oo^ Invalid - only letters, numbers and underscore allowed
    • _MEMLIMIT Valid not good - doesn't start with a lower-case letter
    • return Invalid - this is a special keyword in C
  9. C is a typed language. What does this mean? Why is this useful?

    In a typed language, program entities (variables, functions, etc.) are declared to be of particular types. This allows additional checks (type-checking) to be performed that ensure the validity of programs. If a type mismatch is detected, then the programmer is warned by the compiler. Typed code is easier to understand and debug since there are guarantees about the kinds of values that are being manipulated. Giving a variable a type also lets the compiler determine the amount of storage space (memory) to set aside for storing values in the variable.
  10. 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;
    }
    
    
  11. Modify rectangle_area.c so that it reads floating-point (decimal) numbers and prints the area as a floating-point number.
    ./rectangle_area
    Please enter rectangle length: 3.14159
    Please enter rectangle width: 2.71828
    Area = 8.539721
    
    Note carefully the changes.
    A sample solution for rectangle_area.c
    // Compute area of a rectangle using doubles
    // 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) {
        double length, width, area;
    
        printf("Please enter rectangle length: ");
        scanf("%lf", &length);
        printf("Please enter rectangle width: ");
        scanf("%lf", &width);
    
        area = length * width;
    
        printf("Area = %lf\n", area);
    
        return 0;
    }
    
    
  12. 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.

    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.

  13. Write a program pass_fail.c that reads in an integer and prints out "PASS" if the integer is between 50 and 100 inclusive and fail if it is between 49 and 0, inclusive. It should print out ERROR if the number is less than 0, more than 100, or if the user does not enter a number. For example:
    ./pass_fail
    Please enter your mark: 42
    FAIL
    ./pass_fail
    Please enter your mark: 50
    PASS
    ./pass_fail
    Please enter your mark: 256
    ERROR
    
    A possible solution for pass_fail.c
    #include <stdio.h>
    
    int main(void) {
        int mark;
    
        printf("Please enter your mark: ");
        scanf("%d", &mark);
    
        if (mark < 0) {
            printf("ERROR\n");
        } else if (mark > 100) {
            printf("ERROR\n");
        } else if (mark >= 50) {
            printf("PASS\n");
        } else {
            printf("FAIL\n");
        }
    
        return 0;
    }
    
    
    Another possible solution for pass_fail.c
    #include <stdio.h>
    
    int main(void) {
        int mark;
    
        printf("Please enter your mark: ");
        scanf("%d", &mark);
    
        if (mark < 0 || mark > 100) {
            printf("ERROR\n");
        } else if (mark < 50) {
            printf("FAIL\n");
        } else {
            printf("PASS\n");
        }
    
        return 0;
    }
    
    
  14. Write a program that reads in an integer and determines if it is even or not. If the number is negative, print "NEGATIVE" instead. For example:
    ./even_or_odd
    Please enter a number: 42
    EVEN
    ./even_or_odd
    Please enter a number: 111
    ODD
    ./even_or_odd
    Please enter a number: -2
    NEGATIVE
    
    A possible solution for even_or_odd.c
    #include <stdio.h>
    
    int main(void) {
        int x;
        printf("Please enter a number: ");
        scanf("%d", &x);
    
        if (x < 0) {
            printf("NEGATIVE\n");
        } else if (x % 2 == 0) {
            printf("EVEN\n");
        } else {
            printf("ODD\n");
        }
    
        return 0;
    }