forked from PatrickHentz/A-First-Book-of-C-Exercises-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise 4.1 (Completed)
78 lines (76 loc) · 2.32 KB
/
Exercise 4.1 (Completed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
1. (Practice) Determine the value of the following expressions, assuming a = 5, b = 2, c = 4,
d = 6, and e = 3:
a. a > b
True. Value is 1.
b. a != b
True. Value is 1.
c. d % b == c % b
True. Value is 1.
d. a * c != d * b
True. Value is 1.
e. d * b == c * e
True. Value is 1.
f. a * b
False. Value is 10.
g. a % b * c
False. Value is 4.
h. c % b * a
False. Value is 0.
i. b % c * a
False. Value is 10.
2. (Practice) Using parentheses, rewrite the following expressions to indicate their order of
aevaluation correctly. Then evaluate each expression, assuming a = 5, b = 2, and c = 4.
a. a % b * c && c % b * a
((a % b) * c) && ((c % b) * a)
= ((5 % 2) * 4) && ((4 % 2) * 5)
= (1 * 4) && (0 * 5)
= 4 && 0 // same as True AND False
= 0
b. a % b * c || c % b * a
((a % b) * c) || ((c % b) * a)
= ((5 % 2) * 4) || ((4 % 2) * 5)
= (1 * 4) || (0 * 5)
= 4 || 0 // same as True OR False
= 1
c. b % c * a && a % c * b
((b % c) * a) && ((a % c) * b)
= ((2 % 4) * 5) && ((5 % 4) * 2)
= (2 * 5) && (1 * 2)
= 10 && 2 // same as True AND True
= 1
d. b % c * a || a % c * b
((b % c) * a) || ((a % c) * b)
= ((2 % 4) * 5) || ((5 % 4) * 2)
= (2 * 5) || (1 * 2)
= 10 || 2 // same as True OR True
= 1
3. (Practice) Write relational expressions to express the following conditions (using variable
names of your choosing):
a. A person’s age is equal to 30.
age == 30
b. A person’s temperature is greater than 98.6 degrees.
temp > 98.6
c. A person’s height is less than 6 feet.
height < 6
d. The current month is 12 (December).
month == 12
e. The letter input is m.
letterIn ++ 'm'
f. A person’s age is equal to 30, and the person is taller than 6 feet.
(age == 30) && (height > 6)
g. The current day is the 15th day of the 1st month.
(day == 15) && (month == 1)
h. A person is older than 50 or has been employed at the company for at least 5 years.
(age > 50) && (employed >= 5)
i. A person’s identification number is less than 500 and the person is older than 55.
(id < 500) && (age > 55)
j. A length is greater than 2 feet and less than 3 feet.
(length > 2) && (length < 3)
4. (Practice) Determine the value of the following expressions, assuming a = 5, b = 2, c = 4,
and d = 5:
a. a == 5
True. Value is 1.
b. b * d == c * c
False. (10 != 16) Value is 0.
c. d % b * c > 5 || c % b * d < 7
False OR True. Value is 1.