oddeven
Write a C program oddeven.c that
- prompts the user to enter an integer value;
- outputs the word "EVEN" if the input number was even
- otherwise outputs the word "ODD"
Main Contents Page | CTEC1401 Programming in C |
Quick Reference |
Week 6: Consolidation | Week 7: Selection |
Week 8: Iteration |
Before the lab session read CFTTP Chapter 5 and study the solved exercises very carefully.
Attempt the unsolved exercises 5.1 to 5.5 at the end of Chapter 3.
Write a C program oddeven.c that
Write a C program signum.c that
Write a C program letters1.c that
Don't forget to consider both upper and lower case alphabetic characters.
Write a C program numbers.c that
Write a C program sort3.c that
if
statements).
Write a C program triangle.c that
"Illegal"
and then stops.
"Equilateral"
and then stops.
"Right-Isosceles"
if it is a right-angled triangle, OR
just "Isosceles"
otherwise. Then the program stops.
"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?
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
Consider carefully what values you will use to test this program.
What values will give you good coverage?
What values represent boundary cases?
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.
Write a program, digitsToWords.c, that prompts the user to
input a character in the range '0' to'9' (i.e. a digit character). If the user
enters '0' the program should print out "ZERO"; if the input is '1' the ouput should
be "ONE"; and so on. Any character outside the range '0' to '9' should cause the
program to output "NOT A DIGIT". Use a switch
statement.
Normally you want to put a break at the end of each case. However there are occasions when it is useful to have certain cases cascade into each other. This is commonly used when the same actions are executed for two or more different values. In the following example case 1 cascades into case 2.
switch (value) { case const1 : statement; statement; ... statement; /* drops into case 2 */ case const2 : statement; statement; ... statement; break; /* jump to end */ default : statement; /* otherwise */ statement; ... statement; }
A particularly useful variant shows how to execute a group of statements when the value matches any one of a set of constants.
case const1 : const2 : ... constN : statement; statement; ... ... etc.
Previously you wrote the program letters1.c.
Re-write that program as letters2.c, but this time
use a switch
statement to classify the letters.
Hint: check the validity of the input using an if
-statement
before you go on to classify it.
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
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):
|
|
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.
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.
Give wd
an initial value of day
If month
< 3, increase month
by 10 and reduce
year
by 1
otherwise reduce month
by 2.
Multiply month
by 26, subtract 1,
and then divide the result by 10.
Update wd
by adding this result to it.
Update wd
by adding the last two digits of
year
to it.
Update wd
by adding
(the last two digits of year
divided by 4) to it.
Update wd
by adding
(the first two digits of year
divided by 4) to it.
Multiply the first two digits of year
by 2 and update
wd
by subtracting this result from it.
Update wd
by adding 49 to it.
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.
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.
|
|
switch
statement.
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; }
____ A _____/1 /1 \2___ B ____/ / \ ____ C / \2____/1 /1 \2___ D Start ____/ \ ____ E \2 _____/1 \ /1 \2___ F \____/ \ ____ G \2____/1 \2___ H
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.