Main Contents Page
CTEC1401 Programming in C
Quick Reference
Week 6: Consolidation
Week 7: Selection
Week 8: Iteration
  1. Before the lab session read CFTTP Chapter 5 and study the solved exercises very carefully.

  2. Attempt the unsolved exercises 5.1 to 5.5 at the end of Chapter 3.

  3. oddeven

    Write a C program oddeven.c that

    1. prompts the user to enter an integer value;
    2. outputs the word "EVEN" if the input number was even
    3. otherwise outputs the word "ODD"
  4. signum

    Write a C program signum.c that

    1. prompts the user to enter an integer value;
    2. outputs -1 if the input number was less than zero;
    3. outputs 0 if the input number was exactly zero;
    4. outputs 1 if the input number was greater than zero.
  5. letters1

    Write a C program letters1.c that

    1. prompts the user to enter a character;
    2. outputs the word "VOWEL" if the input character was a vowel;
    3. outputs the word "CONSONANT" if the input character was a consonant;
    4. otherwise outputs the message: "Character is non-alphabetic".

    Don't forget to consider both upper and lower case alphabetic characters.

  6. numbers

    Write a C program numbers.c that

    1. prompts the user to enter three integer numbers;
    2. outputs "YES" if any one of the numbers is equal to the sum of the other two;
    3. otherwise outputs "NO".
  7. sort3

    Write a C program sort3.c that

    1. prompts the user to enter three integer numbers;
    2. outputs the numbers in ascending numerical order. (NB You should solve the problem using if statements).
  8. triangle

    Write a C program triangle.c that

    1. Prompts the user to enter three (positive) integer values representing the lengths of the sides of a triangle.
    2. If the input values could not represent a valid triangle (i.e. any side is longer than the sum of the other two) then prints out the message "Illegal" and then stops.
    3. If the input values represent an equilateral triangle then prints out the message "Equilateral" and then stops.
    4. If the input values represent an isosceles triangle then prints out EITHER "Right-Isosceles" if it is a right-angled triangle, OR just "Isosceles" otherwise. Then the program stops.
    5. If none of the above categories apply then prints out EITHER "Right-Scalene" if the triangle is right-angled OR "Scalene" otherwise.

    Consider carefully what values you will use to test this program.

    What values will give you good coverage?

    What values represent boundary cases?

  9. modulemark

    Assume that for a particular module the assessment is by a piece of coursework and an examination (both componenents being equally weighted). To pass the module the overall mark must be greater than or equal to 40%. Resits must be taken in any component that was failed (< 40%).

    Write a C program modulemark.c that

    1. prompts the user to enter two integer values - one representing the coursework mark (as a percentage) and the other representing the examination mark (as a percentage);
    2. calculates the overall module mark and prints it out;
    3. EITHER tells the user that the module was passed overall, OR tells the user that the module was failed overall;
    4. If (and only if) the module was failed overall, the program should print out "Resit Coursework" if the coursework component was below 40% and "Resit Examination" if the examination component was below 40%.

    Consider carefully what values you will use to test this program.

    What values will give you good coverage?

    What values represent boundary cases?

  10. colours

    Write a program, colours.c, that prompts the user to enter a number between 1 and 7 and outputs the corresponding rainbow colour: red, orange, yellow, green, blue, indigo, violet. Use a switch statement.

  11. Supplementary Exercises

  12. leapyear

    The Gregorian Calendar was introduced in the year 1582 and is the calendar used today. You can look up the history for yourself but this exercise is concerned with establishing which years are leap years in the Gregorian Calendar.

    The following algorithm can be used to establish if a year is a leap year:

    if year modulo 400 == 0
        then year IS a leap year
    else if year modulo 100 == 0
        then year is NOT a leap year
    else if year modulo 4 == 0
        then year IS a leap year
    else
        year is NOT a leap year

    Create a program leapyear.c that asks the user to input a year (≥1582) and reports whether or not it is a leap year.

  13. logic3

    In a 2-value logic system a variable is either true or false. It it is not true then it is false and if it is not false then it is true. This is the underlying logic used by the logical operators in C (&&, !, ||).

    Sometimes it is useful to use a 3-value logic system. For example, it might be that we don't know whether a value is true or false. For example, consider an SQL expression (Quantity > 0). It is possible that the value contained in Quantity has not been supplied (not known, not applicable, withheld, etc.) and SQL returns NULL. The expression therefore becomes (NULL > 0) which has an unknown value (it is not true but neither is it false). SQL interprets this as unknown.

    There are different ways of dealing with 3-valued logic systems. For this example consider Kleene Logic defined by the following tables (we have used the symbol ⊥ to represent unknown):

    a not a
    T F
    F T
          
    a b a and b a or b
    T T T T
    T F F T
    F T F T
    F F F F
    T T
    F F
    T T
    F F

    It is possible to represent a 3-valued logic in C using int. In fact there are many possible encodings that could be used, so for this exercise we will consider the following:
    T is represented by 1
    F is represented by 0
    is represented by any other value

    Write a program, logic3.c, that prompts the user to enter two (3-valued) logic values (a, b) and outputs the result of the expressions not a, not b, a and b, a or b. Solve this problem using switch statements.

  14. weekday

    Create a program weekday.c which gets three integer values from the keyboard - the day (e.g. 27), the month (e.g. 9) and the year (e.g. 2004) and calculates wd - the day of the week on which that date (e.g. 27/9/2004) falls.

    wd will be a number between 0 (Sunday) and 6 (Saturday).

    wd can be calculated from the date by the following steps.

    1. Give wd an initial value of day

    2. If month < 3, increase month by 10 and reduce year by 1
      otherwise reduce month by 2.

    3. Multiply month by 26, subtract 1, and then divide the result by 10.
      Update wd by adding this result to it.

    4. Update wd by adding the last two digits of year to it.

    5. Update wd by adding (the last two digits of year divided by 4) to it.

    6. Update wd by adding (the first two digits of year divided by 4) to it.

    7. Multiply the first two digits of year by 2 and update wd by subtracting this result from it.

    8. Update wd by adding 49 to it.

    9. Replace wd with the remainder after dividing wd by 7.

    Compile weekday.c and test it with several different input dates.

    You can use the Unix cal program to check the results. E.g.

    cal 2004
    

    displays the calendar for 2004,

    cal 1865
    

    displays the calendar for 1865

    Note that this is a much longer program than you have tackled before but is not hard if you just take it step by step.
    You don't have to write the whole program in one go - try compiling it after you write each bit.

  15. numbers20

    Write a program, numbers20.c, that prompts the user to enter a number between 1 and 20 and outputs a classification of that number. The classifications we are interested in are primes, squares or tens. The following table will be helpful.

    Number Prime Square Ten
    1
    2 yes
    3 yes
    4 yes
    5 yes
    6
    7 yes
    8
    9 yes
    10 yes
          
    Number Prime Square Ten
    11 yes
    12
    13 yes
    14
    15
    16 yes
    17 yes
    18
    19 yes
    20 yes
    All other numbers (i.e. 1, 6, 8, 12, 14, 15, 18) should result in the message Unclassified being output. Use a switch statement.

  16. pinball1 , pinball2

    Consider the following "tree" diagram showing how a path forks as you travel from the start towards one of the terminal nodes.

                     ____ A
               _____/1
              /1    \2___ B
    Start ___/   
             \       ____ C
              \2____/1
                    \2___ D
                 
    

    At each fork a number is used to determine which path to take. Since there are two possibilities at each fork we number the paths 1 or 2. Now consider a path indicated by the decisions taken at each point: e.g. 1 2 would represent the path that results at the terminal node B

    The following program allows the user to enter the two decisions and then prints out the terminal node that is reached.

    #include <stdio.h>
    
    int main()
    {
        int v1, v2;
        scanf("%i %i",&v1,&v2);
        if (v1==1)
        {
            if (v2==1)
               printf("A ");
            else
               printf("B ");
        }
    
        else
        {
            if (v2==1)
               printf("C ");
            else
               printf("D ");
        }
        printf("\n");
        return 0;
    }
    

    1. Type in this C program, pinball1.c, and check that it works for all four possible paths.
    2. Develop your own C program, pinball2.c, that works in a similar way for the following tree:
                                ____ A
                          _____/1
                         /1    \2___ B
                    ____/   
                   /    \       ____ C
                  /      \2____/1
                 /1            \2___ D
      Start ____/           
                \               ____ E
                 \2       _____/1
                  \      /1    \2___ F
                   \____/   
                        \       ____ G
                         \2____/1
                               \2___ H
      
  17. pinball3, pinball4

    Develop two programs, pinball3.c and pinball4.c, use the following (more sophisticated) tree.

                                       Start
                                         |
          _______________________________|_____________________________
         |1                    |2                       |3             |4
         |                     |                        |              |
     ____|____      ____ ______|__ ____ ____      ______|_______      _|__
    |1   |2   |3   |1   |2   |3   |4   |5   |6   |1   |2   |3   |4   |1   |2
    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |
    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O
    

    pinball3.c should use switch statements whenever appropriate;
    pinball4.c should use only if/else statements for navigating the paths through the tree.