COMP1511 17s1 Introduction to Programming
  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 this week a lab pair to do next week's code review.

  2. Discuss the errors in these while loops
    int i;
    
    while (i < 100) {
        printf("%d\n", i);
        i = i + 1;
    }
    

    int i = 0;
    int j = 0;
    
    while (j = 1 || i < 100) {
        printf("%d\n", i);
        i = i + 1;
    }
    

    int i = 0;
    int n = 10;
    while (i < n) {
        printf("%d\n", i);
        n = n + i;
        i = i + 1;
    }
    

    int i = 0;
    while (i < 10)
        printf("%d\n", i);
        i = i + 1;
    

  3. Write a program that reads in an integer and prints out that many asterisks, each on a new line.
    ./asterisks
    Please enter an integer: 5
    *
    *
    *
    *
    *
    

  4. Write a C program multiple_of_ten.c which reads 2 integers and then prints all of the multiples of ten between those numbers.
    ./multiple_of_ten
    Enter start: 12
    Enter finish: 42
    20
    30
    40
    ./multiple_of_ten
    Enter start: -1
    Enter finish: 7
    0
    

  5. Your lab partner worked too late last night and wrote some unusual C statements:
    #include <stdio.h>
    
    int main(void) {
        printf("%d\n", 13 < 42);
        printf("%d\n", 13 < 42 || 42 > 50);
        printf("%d\n", 13 < 42 && 42 > 50);
        printf("%d\n", 13 && 42);
        printf("%d\n", 13 || 42);
        return 0;
    }
    
    
    Obviously your partner's code has to be completely rewritten, but first figure out what it will print and why?

  6. 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?

  7. Write a C program fahrenheit2celsius.c which reads a US temperature in Fahrenheit and prints it in Celsius . Reminder:
    Celsius = 5/9 (Fahrenheit - 32)
    

    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.

  8. 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 (%).

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

  10. Consider the following expressions:

    • (x == 4) && (y == 4)

    • (x == 4) || (y == 4)

    • !((x == 4) || (y == 4))

    • (!(x == 4)) && (!(y == 4))

    What do each of the expressions evaluate to given the following values of x and y

    • x = 5; y = 5;
    • x = 5; y = 4;
    • x = 4; y = 3;
    • x = 4; y = 4;

    Compare the evaluation of the last 2 expressions on the given inputs. What do you notice?

  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. What does precedence of an operator mean? Make the precedence explicit in the following expressions by adding parentheses:
    1. 3 + 5 * 10 - 12

    2. a > 5 || b < 3

    3. a = b > c && b || d

    4. !a || a && c

    Figuring this out can be a bit tedious and prone to errors. The moral? In complex expressions make your intention explicit by using parentheses.

    Note: The precedence rules of arithmetic operators in C follow the conventions used in mathematics.

  14. This is a revision question, to make the difference between if and while statements clear. Consider the two code snippets below, what will be the output after execution of each of the programs for input_num = 3.
    int input_num, i; 
    
    printf("Enter a number: "); 
    scanf("%d", &input_num); 
    
    i = input_num; 
    
    if(i <= 5){
      printf("%d\n", i * i); 
      i++; 
    }
    
    
    int input_num, i; 
    
    printf("Enter a number: "); 
    scanf("%d", &input_num); 
    
    i = input_num; 
    
    while(i <= 5){
      printf("%d\n", i * i); 
      i++; 
    }