King Fahd University of Petroleum and Minerals

advertisement
King Fahd University of Petroleum and Minerals
Information & Computer Science Department
ICS 103 – Computer Programming in C
Summer Semester 2008 (073)
Lab # 4 (Selection)
Scope:
The student should practice the following topics:
1.
The single-alternative if.
2.
The dual-alternative if.
3.
The multiple-alternative if-else.
4.
Nested if.
5.
The multiple-alternative switch statement.
Discussion:
The general syntax for the single-alternative if statement is:
if (condition)
statement;
for a single executable statement, and
if (condition)
{
Sequence of statements
}
for a sequence of executable statements.
The general syntax for the dual-alternative if statement is:
if (condition)
statement1;
else
statement2;
for a single executable statement in each clause, and
if (condition)
{
Sequence #1 of statements
} else
{
Sequence #2 of statements
}
1
for a sequence of executable statements in both clauses.
The general syntax for the multiple-alternative if-else statement is:
if (tested_condition1)
{
Sequence #1 of statements
} else if (tested_condition2)
{
Sequence #2 of statements
}
. . .
else if (tested_conditionN}
{
Sequence #N of statements
}
The general syntax for the switch statement is:
switch (expression)
{
case constant1_1:
[ case constant1_2: ...]
One_or_more_statements
break;
case constant2_1:
[ case constant2_2: ...]
One_or_more_statements
break;
...
case constantN_1:
[ case constantN_2: …]
One_or_more_statements
break;
default:
One_or_more_statements
}
Example:
The following problem will be discussed in the class:
Write a program that prompts the user to enter a character and uses the multiple-alternative if-else
statement to determine whether your input is one of the following:
a. An uppercase letter
b. A lowercase letter
c. A digit
d. A non-alphanumeric character.
Exercises:
1. Write a C program that reads an integer number, then it checks whether the number is
divisible by 5 or not and prints a proper message.
2
Sample Output:
Enter an integer number > 43
43 is not divisible by 5
2. Write a C program that reads three integer numbers and prints them in increasing order.
Sample Output:
Enter three integers> 65 -9 3
-9 3
65
3. Write a C program that reads an integer number n where 1  n  10, then it prints the
number as ordinal number. If the number is outside the range, the program then prints
wrong input. Use switch structure to make your decisions.
Sample Output:
Enter an integer number > 3
3rd
4. An electric power distribution company charges its domestic consumers as follows:
Consumption Units
Rate of Charge
0-200
SR. 0.50 per unit
201-400
SR. 1.00 per unit
401-600
SR. 2.30 per unit
601 and above
SR. 3.90 per unit
Write C Program to read the power consumed and prints the amount to be paid by the customer.
HW # 2
Due: next lab session
Write a C program that allows a warehouse employee to type in a product code and have the
computer print out the product characteristics. The character positions in the product code and the
meanings of the characters in those positions are in the following table:
Character Positions
1
Meaning
Country of manufacture
Characters
Translation
U or A
United States
J
Japan
S
Singapore
K
Korea
3
Character Positions
2
Meaning
Types of CPU
3- 5
Capacity of hard disk
Remaining
Types of
devices
Characters
Translation
3
Pentium IV
4
Pentium Celeron
5
Pentium Extreme
Number
Capacity in GB
Input-Output C
CD-ROM drive
D
DVD drive
E
Rewritable CD drive
F
Rewritable DVD drive
Sample Output:
Enter product code > J4200C
Made in Japan
Processor: Pentium Celeron
Hard disk: 200 GB
CD-ROM drive
Submission Guidelines:
1. The homework must be submitted at the beginning of the next lab session in both soft and hard
copies.
2. Late homework is not accepted.
3. The homework must be solved individually. Any kind of cheating will result in F grade for all
parties involved.
Evaluation:
Your grade will depend on your active participation and seriousness during the lab.
4
Download