Selection Statements
Exercises
Question 5.1
The following program fragments illustrate the relational and equality operators. Show the output produced by each, assuming that i, j, and k are int variables.
i = 2; j = 3;
k = i * j == 6;
printf("%d", k);i = 5; j = 10; k = 1;
printf("%d", k > i < j);i = 3; j = 2; k = 1;
printf("%d", i < j == j < k);i = 3; j = 4; k = 5;
printf("%d", i % j + i < k);
- Answer
- Program
-
k = ((i * j) == 6)
k = ((2 * 3) == 6)
k = (6 == 6)
k = 1 -
((k > i) < j)
= ((1 > 5) < j)
= (0 < 10)
= 1 -
((i < j) == (j < k))
= ((3 < 2) == (2 < 1))
= (0 == 0)
= 1 -
(((i % j) + 1) < k)
= (((3 % 4) + 1) < k)
= ((0 + 1) < k)
= (1 < 5)
= 1
#include <stdio.h>
int main (void) {
int i1, i2, i3, i4, i5, i6;
int i7, i8, i9, i10, i11, i12;
int first_sum, second_sum, total;
printf("Enter the first 12 digits of an EAN: ");
Question 5.2
The following program fragments illustrate the logical operators. Show the output produced by each, assuming that i, j, and k are int variables.
i = 10; j = 5;
printf("%d", !i < j);i = 2; j = 1;
printf("%d", !!i + !j);i = 5; j = 0; k = -5;
printf("%d", i && j || k);i =1; j =2; k=3;
printf("%d", i < j || k);
- Answer
- Program
-
((!i) < j)
= ((!10) < 5)
= 0 < 5
= 1 -
((!(!i)) + (!j))
= ((!(!2)) + (!1))
= ((!0) + 0)
= (1 + 0)
= 1 -
((i && j) || k)
= ((5 && 0) || k)
= (0 || -5)
= 1 -
((i < j) || k)
= ((1 < 2) || k)
= (1 || 3)
= 1
#include <stdio.h>
int main (void) {
int i, j, k;
i = 10; j = 5;
printf("%d\n", !i < j);
Question 5.3
The following program fragments illustrate the short-circuit behavior of logical expressions. Show the output produced by each, assuming that i, j, and k are int variables.
i = 3; j = 4; k = 5;
printf("%d ", i < j || ++j < k);
printf("%d %d %d", i, j, k);i = 7; j = 8; k = 9;
printf("%d ", i - 7 && j++ < k);
printf("%d %d %d", i, j, k);i = 7; j = 8; k = 9;
printf("%d ", (i = j) || (j = k));
printf("%d %d %d", i, j, k);i = 1; j = 1; k = 1;
printf("%d ", ++i || ++j && ++k);printf("%d %d %d", i, j, k);
- Answer
- Program
-
((i < j) || ((++j) < k))
= ((3 < 4) || ((++j) < k))
= (1 || ((++j) < k))
= 1 (since (1 (or any non-zero number) || anything) is 1)
= 1 3 4 5 -
((i - 7) && ((j++) < k))
= ((7 - 7) && ((j++) < k))
= (0 && ((j++) < k))
= 0 (since (0 && anything) is 0)
= 0 7 8 9 -
((i = j) || (j = k))
= ((i = 8) || (j = k))
= (8 || (j = k))
= 1
= 1 8 8 9 -
((++i) || ((++j) && (++k)))
= ((2) || ((++j) && (++k)))
= 1
= 1 2 1 1 // && has higher precedance than ||, so the correct way to paranthesize the expression is ((++i) || ((++j) && (++k)))
#include <stdio.h>
int main (void) {
int i, j, k;
i = 3; j = 4; k = 5;
printf("%d ", i < j || ++j < k);
printf("%d %d %d\n", i, j, k);
Question 5.4
Write a single expression whose value is either -1, 0, or +1, depending on whether i is less than, equal to, or greater than j, respectively.
- Program
#include <stdio.h>
int main (void) {
int num;
int result;
printf("Enter a number: ");
scanf("%d", &num);
Question 5.5
Is the following if statement legal?
if (n >= 1 < = 10)
printf("n is between 1 and 10\n");
If so, what does it do when n is equal to 0?
- Answer
Yes, the given if statement is legal. The expression for the if statement can be broken down into sub-expressions as follows: ((n >= 1) <= 10). When n is 0, i.e.
((0 >= 1) <= 10)
= (0 <= 10)
= 1
This is because the result from relational operators can be 1 (meaning true) or 0 (meaning false). Also, the string being printed by printf is misleading as it does not check if n lies between 1 and 10.
Question 5.6
Is the following if statement legal?
if (n == 1 - 10)
printf("n is between 1 and 10\n");
If so, what does it do when n is equal to 5?
- Answer
Yes, the given statement is legal. The expression for the if statement can be paranthesized as: (n == (1 - 10)). When n is 5, i.e.
(5 == (1 - 10))
= (5 == -9)
= 0
Similar to relational operators, equality operator also results in either 0 (false) or 1 (true). Also, the string in the printf is misleading.
Question 5.7
What does the following statement print if i has the value 17? What does it print if i has the value -17?
printf("%d\n", i >= 0 ? i : -i);
- Answer
When i has the value of 17, i >= 0 holds true, so %d gets the value of i (17). When the value of i is -17. i >= 0 does not hold true, so %d gets the value of -i (-(-17)).
Question 5.8
The following if statement is unnecessarily complicated. Simplify it as much as possible. (Hint: The entire statement can be replaced by a single assignment.)
if (age >= 13)
if (age <= 19)
teenager = true;
else
teenager = false;
else if (age < 13)
teenager = false;
- Answer
We can simply this if-else conditonal statement with the help of conditional expression. We can simply rewrite the logic as: teenager = ((age < 13 || age > 19) ? false : true);
Question 5.9
Are the following if statements equivalent? If not, why not?
| |
- Answer
The given two programs are identical. When the score is less than 60, both programs prints F. When the score is in the range of 60-69, both will print D and so on.
Question 5.10
What output does the following program fragment produce? (Assume that i is an integer variable.)
i = 1;
switch (i % 3) {
case 0: printf("zero");
case 1: printf("one");
case 2: printf("two");
}
- Answer
i % 3 → 1 % 3 will result in 1 as the remainder. so it does not satisfy case 0, but does satisfy case 1. Because there is no break statement present in case 1, the control goes to following case, i.e. case 2 in this example (fall through), so the resultant output will be "onetwo".
Question 5.11
The following table shows telephone area codes in the state of Georgia along with the largest city in each area:
Area code 229 404 470 478 678 706 762 770 912 | Major city Albany Atlanta Atlanta Macon Atlanta Columbus Columbus Atlanta Savannah |
Write a switch statement whose controlling expression is the variable area code. If the value of area_code is in the table, the switch statement will print the corresponding city name. Otherwise, the switch statement will display the message "Area code not recognized". Use the techniques discussed in Section 5.3 to make the switch statement as simple as possible.
- Program
#include <stdio.h>
int main (void) {
int area_code;
printf("Enter the area code: ");
scanf("%3d", &area_code);
Programming Projects
Project 5.1
Write a program that calculates how many digits a number contains:
Enter a number: 374
The number 374 has 3 digits
You may assume that the number has no more than four digits. Hint: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit. If the number is between 10 and 99. it has two digits.
- Program
#include <stdio.h>
int main (void) {
int num;
int num_of_digits = 0;
printf("Enter a number: ");
scanf("%4d", &num);
Project 5.2
Write a program that asks the user for a 24-hour time, then displays the time in 12-hour form:
Enter a 24-hour time: 21:11
Equivalent 12-hour time: 9:11 PM
Be careful not to display 12:00 as 0:00.
- Program
#include <stdio.h>
int main (void) {
int hour = 0, minute = 0;
printf("Enter a 24-hour time: ");
scanf("%2d:%2d", &hour, &minute);
Project 5.3
Modify the broker.c program of Section 5.2 by making both of the following changes:
- Ask the user to enter the number of shares and the price per share, instead of the value of the trade.
- Add statements that compute the commission charged by a rival broker ($33 plus 3¢ per share for fewer than 2000 shares; $33 plus 2¢ per share for 2000 shares or more). Display the rival's commission as well as the commission charged by the original broker.
- Program
#include <stdio.h>
int main (void) {
int share_count;
float price_per_share, transaction_value;
float commission_original = 0.00f, commission_rival = 33.00f;
printf("Enter the number of shares: ");
Project 5.4
Here's a simplified version of the Beaufort scale, which is used to estimate wind force:
Speed (knots) Less than 1 1-3 4-27 28-47 48-63 Above 63 | Description Calm Light air Breeze Gale Storm Hurricane |
Write a program that asks the user to enter a wind speed (in knots), then displays the corresponding description.
- Program
#include <stdio.h>
int main (void) {
int speed;
printf("Enter the wind speed (in knots): ");
scanf("%d", &speed);
Project 5.5
In one state, single residents are subject to the following income tax:
Income Not over $ $-$ $-$ $-$ $-$ Over $ | Amount of tax of income $ plus of amount over $ $ plus of amount over $ $ plus of amount over $ $ plus of amount over $ $ plus of amount over $ |
Write a program that asks the user to enter the amount of taxable income, then displays the tax due.
- Program
#include <stdio.h>
int main (void) {
float income, tax_amount = 0.00f;
printf("Enter the income: ");
scanf("%f", &income);
Project 5.6
Modify the upc.c program of Section 4.1 so that it checks whether a UPC is valid. After the user enters a UPC, the program will display either VALID or NOT VALID.
- Program
#include <stdio.h>
int main (void) {
int first_digit, second_digit, third_digit, fourth_digit, fifth_digit;
int sixth_digit, seventh_digit, eighth_digit, ninth_digit, tenth_digit;
int eleventh_digit, twelfth_digit;
int first_sum, second_sum, total;
printf("Enter the 12 digits of UPC: ");
Project 5.7
Write a program that finds the largest and smallest of four integers entered by the user:
Enter four integers: 21 43 10 35
Largest: 43
Smallest: 10
Use as few if statements as possible. Hint: Four if statements are sufficient.
- Program
#include <stdio.h>
int main (void) {
int num_1, num_2, num_3, num_4;
int max_1, min_1, max_2, min_2;
int largest_num, smallest_num;
printf("Enter four integers: ");
Project 5.8
The following table shows the daily flights from one city to another:
Departure time 8:00 a.m. 9:43 a.m. 11:19 a.m. 12:47 p.m. 2:00 p.m. 3:45 p.m. 7:00 p.m. 9:45 p.m. | Arrival time 10:16 a.m. 11:52 a.m. 1:31 p.m. 3:00 p.m. 4:08 p.m. 5:55 p.m. 9:20 p.m. 11:58 p.m. |
Write a program that asks user to enter a time (expressed in hours and minutes, using the 24-hour clock). The program then displays the departure and arrival times for the flight whose departure time is closest to that entered by the user:
Enter a 24-hour time: 13:15
Closest departure time is 12:47 p.m., arriving at 3:00 p.m.
Hint: Convert the input into a time expressed in minutes since midnight, and compare it to the departure times, also expressed in minutes since midnight. For example, 13:15 is 13 x 60+ 15 = 795 minutes since midnight, which is closer to 12:47 p.m. (767 minutes since midnight) than to any of the other departure times.
- Program
#include <stdio.h>
int main (void) {
// declare departure time variables
int departure_1, departure_2, departure_3, departure_4;
int departure_5, departure_6, departure_7, departure_8;
// initialize deprature time in minutes
Project 5.9
Write a program that prompts the user to enter two dates and then indicates which date comes earlier on the calendar:
Enter first date (mm/dd/yy): 3/6/08
Enter second date (mm/dd/yy): 5/17/07
5/17/07 is earlier than 3/6/08
- Program
#include <stdio.h>
int main (void) {
int day_1, month_1, year_1;
int day_2, month_2, year_2;
printf("Enter first date (mm/dd/yy): ");
scanf("%2d/%2d/%2d", &month_1, &day_1, &year_1);
Project 5.10
Using the switch statement, write a program that converts a numerical grade into a letter grade:
Enter numerical grade: 84
Letter grade: B
Use the following grading scale: A = 90-100, B = 80-89, C = 70-79, D = 60-69, F = 0-59. Print an error message if the grade is larger than 100 or less than 0. Hint: Break the grade into two digits, then use a switch statement to test the ten's digit.
- Program
#include <stdio.h>
int main (void) {
int numerical_grade;
printf("Enter numerical grade: ");
scanf("%d", &numerical_grade);
Project 5.11
Write a program that asks the user for a two-digit number, then prints the English word for the number:
Enter a two-digit number: 45
You entered the number forty-five.
Hint: Break the number into two digits. Use one switch statement to print the word for the First digit("twenty", "thirty", and so forth). Use a second switch statement to print the word for the second digit. Don't forget that the numbers between 11 and 19 require special treatment.
- Program
#include <stdio.h>
int main (void) {
int num;
printf("Enter a two-digit number: ");
scanf("%2d", &num);