COMP1511 19T2
COMP1511 19T2
  1. Tuts from now on will start with code reviews.

    This week your tutor will show you how it is to be done in future weeks.

    Your tutor will nominate a lab pair this week to do next week's code review.

    How a code review works:

    • The reviewees show their code on the projector, and briefly walk through how it works.
    • The class and tutor (the reviewers) give feedback, ask questions, make suggestions. You tutor will show you how to do this at first but then will expect the other reviewers to take over.
    • Reviewers are NOT negative, a review is to be supportive and constructive and helpful to the reviewees.
    • The reviewees should speak very little, just give a brief overview of the code they want reviewed.
    • Let everyone have a turn to speak, don’t dominate the conversation.
    • Contribute and participate, don’t be silent. If you don’t understand anything then that doesn’t mean be silent - it means ASK for an explanation. By asking you are helping the coders to see how to be clearer.
  2. Which of the following are valid variable names in C?

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

    • THX1138
    • 2for1
    • mr_bean
    • my space
    • event_counter
    • ^oo^
    • _MEMLIMIT
    • return
  3. C is a typed language. What does this mean? Why is this useful?

  4. Write a C program cm2feet.c which reads a height in centimetres and prints it in feet.

    Reminder: there 2.54 centimetres in an inch and 12 inches in a foot.

    Use only int variables.

    Your program should behave like this:

    ./cm2feet
    Enter your height in centimetres: 183
    Your height in feet is 6
    

    Would double variables have been a better choice?

  5. Write a C program count42.c which prints the integers 1..42, one per line. For example:
    dcc -o count42 count42.c
    ./count42
    1
    2
    3
    
  6. Write a C program count_up.c which reads an integer n and then prints the integers 1..n, one per line. For example:
    dcc -o count_up count_up.c
    ./count_up
    Enter finish: 3
    1
    2
    3
    ./count_up
    Enter finish: 7
    1
    2
    3
    4
    5
    6
    7
    
  7. Write a C program range.c which reads integers n and m and then prints the integers n..m, one per line. For example:
    dcc -o range range.c
    ./range
    Enter start: 3
    Enter finish: 7
    3
    4
    5
    6
    7
    
  8. Write a C program range7.c which reads 2 integers n and m, and then prints the integers between n and m (including n and m) which are divisible by 7.

    Hint: if x is divisible by 7, then x % 7 == 0

    dcc -o range7 range7.c
    ./range7
    Enter start: 3
    Enter finish: 49
    7
    14
    21
    28
    35
    42
    49
    
  9. Write a C program range_divisible.c which reads 3 integers n, m and x then prints the integers between n and m (including n and m) which are divisible by x.
    dcc -o range_divisible range_divisible.c
    ./range_divisible
    Enter start: 20
    Enter finish: 100
    Enter divisor: 13
    26
    39
    52
    65
    78
    91
    ./range_divisible
    Enter start: 80
    Enter finish: 120
    Enter divisor: 5
    80
    85
    90
    95
    100
    105
    110
    115
    120
    

    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.

  10. Modify rectangle_area.c from last week 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.
  11. What is output by the following C program? Why? Make sure you compile the program and run it to confirm your answer.

    #include <stdio.h>
    
    #define FIRST_NUMBER     10
    #define SECOND_NUMBER    20
    #define TOTAL   FIRST_NUMBER + SECOND_NUMBER
    #define AVERAGE TOTAL / 2
    
    int main(void) {
        printf("The average of %d and %d is %d\n",
               FIRST_NUMBER, SECOND_NUMBER, AVERAGE);
    
        return 0;
    }
    

  12. 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. 1 / 2 * 500
    2. 1 / 2.0 * 500
    3. (17 / 5) * 5 + (17 % 5)
    4. (12 - 17) % 6 - 4
  13. Write a C program decompose.c that prompts the user to enter an integer, reads it from the input and prints out the number in individual digits. Allow the program to work for input numbers up to 5 digits, i.e. up to 99999. You should be able to write this program using some divisions and remainder (modulo '%') operations, if statements and simple comparisons.

    Your program should behave as follows:

    ./decompose
    Please enter an integer: 25
    You entered 25 which is decomposed: 2 5
    ./decompose
    Please enter an integer: 2825
    You entered 2825 which is decomposed: 2 8 2 5
    ./decompose
    Please enter an integer: 2
    You entered 2 which is decomposed: 2
    

    Your program should handle all integers in range (0 to 99999).

    Hint: use if, divide (/) and mod (%).

  14. Discuss the concept of short-circuit evaluation for the C logical operators || and &&. Give examples. Why is this feature useful?