# Ch05.2-Instr

```This material is provided to the professors and students using Computer Science: A Structured Approach Using C,
Second Edition. All reprints in any form must cite this copyright.
A division of Thomson Learning
Chapter 5 Selection--Making Decisions
Review Questions
1.
a. True
2.
b. False
3.
a. True
4.
b. False
5.
b. False
6.
a. And (&amp;&amp;)
7.
c. =
8.
c. if...else
9.
e. The selection expression cannot have a side effect.
10.
a. A nested if statement without a false statement.
11.
d. else if and switch
12.
c. The case-labeled expression can be a constant or a variable.
13.
c. The else if requires integral values in its expression.
14.
a. ASCII
Exercises
15.
a. (! ((3 + 3) &gt;= 6))
(! (6 &gt;= 6))
(! (true))
false
b. (((1 + 6) == 7) || ((3 + 2) == 1))
((7 == 7)
|| (5 == 1))
((true)
|| (5 == 1))
(2d expression not evaluated)
true
c. (((1 &gt; 5) || (6 &lt; 50)) &amp;&amp; (2 &lt; 5))
(((false) || (true))
&amp;&amp; (true))
((true)
&amp;&amp; (true))
true
d. (((14 != 55) &amp;&amp; (! (13 &lt; 29))) || (31 &gt; 52))
(((true)
&amp;&amp; (! (true)))
|| (false))
(( true
&amp;&amp; false)
|| false)
(( false)
|| (false))
false
e. ((6 &lt; 7) &gt; 5)
((true) &gt; 5)
( 1
&gt; 5)
false
16.
a. x = 0, y = 295, z = 5
b. x = 9, y = 5, z = 9
c. x = –2, y = 5, z = 8
17.
a. 1
b. 1
c. 1
d. 1
e. 1
Page 5.1
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
a. x &gt;= y
b. x &lt; y
c. x != y
d. x == y
e. x &gt; y
a. 0
b. 1
c. 1
d. 1
e. 0
x = 4, y = 3, z = 2
x = 4, y = 1, z = 2
x = 4, y = 2, z = 2
x = 4, y = 1, z = 2
x = 0, y = 0, z = 1
x = 2, y = 0, z = 2
x = 0, y = 0, z = 1
x = 0, y = 0, z = 1
x = –1, y = 0, z = 0
x = 0, y = 1, z = 0
x = 1, y = 3, z = 1
x = 1, y = 3, z = 1
x = 4, y = 3, z = 0
a. c (lowercase 'c')
b. ?
c. c (lowercase 'c')
d. 5
a. C (upper case 'C')
b. C (upper case 'C')
c. ?
d. 7
a. !x || !y &amp;&amp; z
b. !y &amp;&amp; z || x
c. !x &amp;&amp; !y &amp;&amp; z
d. x &amp;&amp; y || z || y
e. !x || !y || !z || y || z
Problems
36.
if (score &gt;= 90)
best = 1;
37.
best = (score &gt;= 90 ? 1 : best);
38.
if (amount &gt; 5.4)
num += 4;
39.
if (flag)
printf (&quot;num has the value: %d\n&quot;, num);
40.
if (num)
printf (&quot;not zero&quot;);
else
printf (&quot;zero&quot;);
Page 5.2
41.
if (divisor)
quotient = (double)dividend / divisor;
else
quotient = divisor;
printf(&quot;%d / %d = %f\n&quot;, dividend, divisor, quotient);
42.
if (flag)
{
scanf (&quot;%d %d&quot;, &amp;a, &amp;b);
printf (&quot;The sum is: %d\n&quot;, a + b);
printf (&quot;The average is: %f\n&quot;, (a + b) / 2.0);
} /* if */
43.
if (aChar == ‘E’)
{
c++;
printf (&quot;Value is E\n&quot;);
} /* if */
44.
switch (ch)
{
case ‘E’ :
case ‘e’ :
case ‘A’ :
case ‘a’ :
countE++;
break;
countA++;
break;
case ‘I’ :
case ‘i’ :
countI++;
break;
default : printf(&quot;Error—Not A, E, or I\a\n&quot;);
break;
} /* switch */
45.
if (num1 == 10)
num1 *= num1;
else
if (num1 == 9)
scanf (&quot;%d&quot;, &amp;num1);
else
if (num1 == 2 || num1 == 3)
printf (&quot;%d&quot;, num1 * 99);
46.
switch (num1)
{
case 10 :
case 9
:
case 2
case 3
:
:
num1 *= num1;
break;
scanf (&quot;%d&quot;, &amp;num1);
break;
printf (&quot;%d&quot;, num1 * 99);
break;
} /* switch */
47.
printf (&quot;Please enter two integers: &quot;);
Page 5.3
scanf (&quot;%d %d&quot;, &amp;x, &amp;y);
if (x &gt; 0)
{
if (y &gt; 0)
{
z = x;
y = x + 1;
} /* if */
else
z = y;
z = z + 1;
} /* if */
else
{
y = x – 1;
z = 2 * x;
} /* else */
printf (&quot;x = %d, y = %d, z = %f\n&quot;, x, y, z);
48.
/* ==================== smallest ======================
This function returns the smallest of 3 integers.
Pre
given 3 integers
Post the smallest integer returned
*/
int smallest (int a,
int b,
int c)
{
/* Local Definitions */
int smallest;
/* Statements */
if (a &lt; b &amp;&amp; a &lt; c)
smallest = a;
else if (b &lt; c)
smallest = b;
else
smallest = c;
return smallest;
} /* end of smallest */
49.
/* ================= day_of_week ===============
This function displays the day of week corresponding
to a number between 0 and 6.
Pre given an integer representing a day
Post day of week printed
*/
void day_of_week (int day)
{
/* Statements */
switch (day)
{
case 0 : printf (&quot;\nSunday\n&quot;);
break;
case 1 : printf (&quot;\nMonday\n&quot;);
break;
Page 5.4
case 2 : printf
break;
case 3 : printf
break;
case 4 : printf
break;
case 5 : printf
break;
case 6 : printf
break;
default: printf
} /* switch */
}
(&quot;\nTuesday\n&quot;);
(&quot;\nWednesday\n&quot;);
(&quot;\nThursday\n&quot;);
(&quot;\nFriday\n&quot;);
(&quot;\nSaturday\n&quot;);
(&quot;\nDay is not valid!\n&quot;);
return;
/* end of day_of_week */
50.
/* =============== month_of_year ==================
This function displays the month of year corresponding
to a number between 1 and 12.
Pre
given an integer month
Post month printed
*/
void month_of_year (int month)
{
/* Statements */
switch (month)
{
case 1 : printf (&quot;\nJanuary\n&quot;);
break;
case 2 : printf (&quot;\nFebruary\n&quot;);
break;
case 3 : printf (&quot;\nMarch\n&quot;);
break;
case 4 : printf (&quot;\nApril\n&quot;);
break;
case 5 : printf (&quot;\nMay\n&quot;);
break;
case 6 : printf (&quot;\nJune\n&quot;);
break;
case 7 : printf (&quot;\nJuly\n&quot;);
break;
case 8 : printf (&quot;\nAugust\n&quot;);
break;
case 9 : printf (&quot;\nSeptember\n&quot;);
break;
case 10: printf (&quot;\nOctober\n&quot;);
break;
case 11: printf (&quot;\nNovember\n&quot;);
break;
case 12: printf (&quot;\nDecember\n&quot;);
break;
default: printf (&quot;\nMonth is not valid!\n&quot;);
}/* switch */
return;
} /* end of monthOfYear */
Page 5.5
51.
/* ===================== parkingCharge =====================
Display the parking charge by type of vehicle
Pre
type of vehicles and the hours
Post parking charge returned (-1 if error)
*/
#define CAR_CHARGE
2.00
#define BUS_CHARGE
3.00
#define TRUCK-_CHARGE 4.00
#define CAR
'c'
#define BUS
'b'
#define TRUCK 't'
float parkingCharge (char
float
{
/* Local Definitions */
float charge;
/* Statements */
switch (vehicle)
{
case CAR
}
vehicle,
hours)
: charge
break;
case BUS
: charge
break;
case TRUCK : charge
break;
default
: printf
charge
} /* switch */
return charge;
/* end of parkingCharge */
= hours * CAR_CHARGE;
= hours * BUS_CHARGE;
= hours * TRUCK_CHARGE;
(&quot;\nVehicle not valid!\n&quot;);
= -1.0;
52.
/* =================== smallestOf4 ===================
This function returns the smallest of 4 integers.
Pre
given 3 integers
Post the smallest integer returned
*/
int smallestOf4 (int a,
int b,
int c,
int d)
{
/* Local Definitions */
int smallest;
/* Statements */
if (a &lt; b &amp;&amp; a &lt; c &amp;&amp; a &lt; d)
smallest = a;
else if (b &lt; c &amp;&amp; b &lt; d)
smallest = b;
else if (c &lt; d)
smallest = c;
else
Page 5.6
}
smallest = d;
return smallest;
/* smallestOf4 */
Page 5.7
```