COMP1911 23T2 Introduction to Programming
    Note: The tutorial questions are designed so that your tutor can choose a selection of questions to go through during class and leave the rest for you to use for your own home study.

    1. Which of the following are valid variable names in C? Which are not? Which variable names conform to the Style Guide used in this subject?

      • THX1138

      • 2for1

      • mrBean

      • My Space

      • still_counting

      • ^oo^

      • _MEMLIMIT

    2. C is a typed language. What does this mean? Why is this useful?

    3. What is the most likely cause of the following compile time error and how would you fix it?

      example.c: In function 'main':
      example.c:4:5: error: 'num' undeclared (first use in this function)
           num = 3;
           ^
      example.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
      

    4. What is an uninitialised variable? What is the value of an uninitialised variable? What sort of problems can be caused by using uninitialised variables in programs?

    5. What does precedence of an operator mean? Make the precedence explicit in the following expressions by adding parentheses and evalute the expressions:
      1. 3 + 5 * 10 - 12

      2. 3 + 15 % 10 - 12 / 2

        Note: The precedence rules of arithmetic operators in C follow the conventions used in mathematics, as detailed in Chapter 3.1 of Moffat and in C_Basics lecture notes.

    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 type and 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

      5. 10/(1/2)

    7. What is wrong with each of the following fragments of code
      1. int num1 = 3;
        int num2 = 8;
        printf("The numbers are %d\n",num1,num2);
        

      2. int num1 = 3;
        int num2 = 8;
        printf("The numbers are %d %d\n",num1);
        

      3. double num1 = 3;
        int num2 = 8;
        printf("The numbers are %d %d\n",num1,num2);
        

    8. Suppose I am running the following program and the user types in incorrect input such as "hello". What will the program output?
      int num;
      printf("Enter a number: ");
      scanf("%d",&num);
      printf("Your number was %d\n",num);
      

    9. Write a program nextBirthday.c which reads someones age and prints out the age they will turn on their next birthday. Assume they are entering their age as integers.

      For example:

      ./nextBirthday
      Please enter your age: 2
      On your next birthday you will turn 3
      

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

      Reminder: there are 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?

    11. Write a program that reads in a real number and prints out its square-root.

      Hint: Use the math.h library function sqrt. How would you compile this if you were using gcc?

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

    13. What is the role of if statements in programs?

    14. Write a program odd_even.c that reads in an integer and prints out whether it is odd or even. For example:
      ./odd_even
      Please enter an integer: 42
      EVEN
      ./odd_even
      Please enter an integer: 1
      ODD
      ./odd_even
      Please enter an integer: -3
      ODD
      ./odd_even
      Please enter an integer: 0
      EVEN
      

    15. Write a program classifyNumber.c that reads in an integer and prints out whether it is odd or even and also whether it is positive, negative or zero. For example:
      ./classifyNumber
      Please enter an integer: 42
      EVEN, POSITIVE
      ./classifyNumber
      Please enter an integer: 1
      ODD, POSITIVE
      ./classifyNumber
      Please enter an integer: -3
      ODD, NEGATIVE
      ./classifyNumber
      Please enter an integer: 0
      EVEN, ZERO
      ./classifyNumber
      Please enter an integer: -2
      EVEN, NEGATIVE
      
      

    16. Write a program pass_fail.c that reads in an integer and prints out "PASS" if the integer is between 50 and 100 (exclusive) and fail if it is between 49 and 0 (exclusive). 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
      

    17. Write a C program is_triangle.c to read 3 integers and indicate whether they can be the sides of a triangle.

      Reminder: 3 numbers can be the sides of a triangle if the sum of any two of the numbers is larger than the third.

    18. At a theme park there are rides that anyone who is 120cm or over can go on ("Green Rides"), rides that anyone who is 130cm or over can go on ("Yellow Rides") and rides that anyone who is 140cm or over can go on ("Red Rides"). Write an application that asks the user to enter their height to the nearest cm and displays all the types of rides they can go on.For example:

      ./rides
      Please enter your height: 130
      Green Rides
      Yellow Rides
      ./rides
      Please enter your height: 119
      Sorry, no rides are safe for you!
      ./rides
      Please enter your height: 256
      Green Rides
      Yellow Rides
      Red Rides
      

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