Ch05.2-Instr

advertisement
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.
Copyright(c) 2001 by Brooks/Cole
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 (&&)
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) >= 6))
(! (6 >= 6))
(! (true))
false
b. (((1 + 6) == 7) || ((3 + 2) == 1))
((7 == 7)
|| (5 == 1))
((true)
|| (5 == 1))
(2d expression not evaluated)
true
c. (((1 > 5) || (6 < 50)) && (2 < 5))
(((false) || (true))
&& (true))
((true)
&& (true))
true
d. (((14 != 55) && (! (13 < 29))) || (31 > 52))
(((true)
&& (! (true)))
|| (false))
(( true
&& false)
|| false)
(( false)
|| (false))
false
e. ((6 < 7) > 5)
((true) > 5)
( 1
> 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 >= y
b. x < y
c. x != y
d. x == y
e. x > 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 && z
b. !y && z || x
c. !x && !y && z
d. x && y || z || y
e. !x || !y || !z || y || z
Problems
36.
if (score >= 90)
best = 1;
37.
best = (score >= 90 ? 1 : best);
38.
if (amount > 5.4)
num += 4;
39.
if (flag)
printf ("num has the value: %d\n", num);
40.
if (num)
printf ("not zero");
else
printf ("zero");
Page 5.2
41.
if (divisor)
quotient = (double)dividend / divisor;
else
quotient = divisor;
printf("%d / %d = %f\n", dividend, divisor, quotient);
42.
if (flag)
{
scanf ("%d %d", &a, &b);
printf ("The sum is: %d\n", a + b);
printf ("The average is: %f\n", (a + b) / 2.0);
} /* if */
43.
if (aChar == ‘E’)
{
c++;
printf ("Value is E\n");
} /* if */
44.
switch (ch)
{
case ‘E’ :
case ‘e’ :
case ‘A’ :
case ‘a’ :
countE++;
break;
countA++;
break;
case ‘I’ :
case ‘i’ :
countI++;
break;
default : printf("Error—Not A, E, or I\a\n");
break;
} /* switch */
45.
if (num1 == 10)
num1 *= num1;
else
if (num1 == 9)
scanf ("%d", &num1);
else
if (num1 == 2 || num1 == 3)
printf ("%d", num1 * 99);
46.
switch (num1)
{
case 10 :
case 9
:
case 2
case 3
:
:
num1 *= num1;
break;
scanf ("%d", &num1);
break;
printf ("%d", num1 * 99);
break;
} /* switch */
47.
printf ("Please enter two integers: ");
Page 5.3
scanf ("%d %d", &x, &y);
if (x > 0)
{
if (y > 0)
{
z = x;
y = x + 1;
} /* if */
else
z = y;
z = z + 1;
} /* if */
else
{
y = x – 1;
z = 2 * x;
} /* else */
printf ("x = %d, y = %d, z = %f\n", 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 < b && a < c)
smallest = a;
else if (b < 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 ("\nSunday\n");
break;
case 1 : printf ("\nMonday\n");
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 */
}
("\nTuesday\n");
("\nWednesday\n");
("\nThursday\n");
("\nFriday\n");
("\nSaturday\n");
("\nDay is not valid!\n");
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 ("\nJanuary\n");
break;
case 2 : printf ("\nFebruary\n");
break;
case 3 : printf ("\nMarch\n");
break;
case 4 : printf ("\nApril\n");
break;
case 5 : printf ("\nMay\n");
break;
case 6 : printf ("\nJune\n");
break;
case 7 : printf ("\nJuly\n");
break;
case 8 : printf ("\nAugust\n");
break;
case 9 : printf ("\nSeptember\n");
break;
case 10: printf ("\nOctober\n");
break;
case 11: printf ("\nNovember\n");
break;
case 12: printf ("\nDecember\n");
break;
default: printf ("\nMonth is not valid!\n");
}/* 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;
("\nVehicle not valid!\n");
= -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 < b && a < c && a < d)
smallest = a;
else if (b < c && b < d)
smallest = b;
else if (c < d)
smallest = c;
else
Page 5.6
}
smallest = d;
return smallest;
/* smallestOf4 */
Page 5.7
Download