You are on page 1of 30

Answers

ODD-NUMBERED SELF-CHECK EXERCISES


CHAPTER 1
SECTION 1.1
1. Software
SECTION 1.2
1. Cell 0: 75.625
Cell 2: 0.005
Cell 999: 75.62
3. Bit, byte, memory cell, main memory, secondary storage, LAN, WAN
SECTION 1.3
1. Add values of a, b, and c. Store sum in x.
Divide y by z. Store result in x.
Subtract b from c and then add a. Store result in d.
Add 1 to z. Store result in z.
Add 273.15 to celsius. Store result in kelvin.
3. Source program, compiler, editor (word processor)
SECTION 1.4
1. Problem requirements, analysis, design, implementation, testing and verification, maintenance
SECTION 1.5
1. Algorithm with refinements:
1. Get the distance in kilometers.
2. Convert the distance to miles.
2.1 The distance in miles is 0.621 times the distance in kilometers.
3. Display the distance in miles.
CHAPTER 2
SECTION 2.1
1. a. void, double, return
b. printf
c. MAX_ENTRIES, G
d. time, xyz123, this_is_a_long_one
e. Sue's, part#2, "char", #include
3. The preprocessor; #define and #include
SECTION 2.2
1. a. 0.0103 1234500.0 123450.0
b. 1.3e+3 1.2345e+2 4.26e3
3. double, int, char
SECTION 2.3
1. Enter two integers> 5 7
m = 10
n =21
3. My name is Jane Doe.
I live in Ann Arbor, MI
and I have 11 years of programming experience.
SECTION 2.4
1. /* This is a comment? */
/* This one seems like a comment doesn't it */
SECTION 2.5
1. a. 22 / 7 is 3 7 / 22 is 0 22 % 7 is 1 7 % 22 is 7
b. 16 / 15 is 1 15 / 16 is 0 16 % 15 is 1 15 % 16 is 15
c. 23 / 3 is 7 3 / 23 is 0 23 % 3 is 2 3 % 23 is 3
d. 16 / -3 is ?? -3 / 16 is ?? 16 % -3 is ?? -3 % 16 is ??
(?? means the result varies)
3. a. 3 g. undefined m. -3.14159
b. ?? h. undefined n. 1.0
c. 1 i. ?? o. 1
d. -3.14159 j. 3 p. undefined
e. ?? k. -3.0 q. 3
f. 0.0 l. 9 r. 0.75
(?? means the result varies)
5. a. white is 1.6666... c. orange is 0 e. lime is 2
b. green is 0.6666... d. blue is -3.0 f. purple is 0.0
SECTION 2.6
1. printf("Salary is %10.2f\n", salary);
3. xis12.34iis100
iis100
xis12.3
SECTION 2.7
1. Calls to printf to display prompts precede calls to scanf to obtain data. Calls to printf follow
calls to scanf when data are echoed. Prompts are used in interactive programs but not in batch
programs. Batch programs should echo input; interactive programs may also echo input.
CHAPTER 3
SECTION 3.1
1. Problem Inputs
double hours /* number of hours worked */
double rate /* hourly rate of pay */
Problem Output
double gross /* gross salary */
Algorithm
1. Input hours worked and rate of pay.
2. Compute gross salary.
2.1 Assign hours * rate to gross.
3. Display gross salary.
3. Problem Inputs
double reg_hours /* number of regular hours worked */
double ot_hours /* number of overtime hours worked */
double rate /* hourly rate of pay */
Problem Output
double gross /* gross salary */
Algorithm
1. Input regular (reg_hours) and overtime (ot_hours) hours worked and rate of pay.
2. Compute gross salary.
2.1 Assign reg_hours * rate to gross.
2.2 Add ot_hours * 1.5 * rate to the previous gross value.
3. DISPLAY GROSS SALARY. SECTION 3.2
1. a. sqrt(u + v) * pow(w, 2)
b. log(pow(x, y))
c. sqrt(pow(x - y, 3))
d. fabs(x * y - w / z)
SECTION 3.3
1. The design phase.
SECTION 3.4
1. HI MOM is printed vertically in large block letters.
SECTION 3.5
1. a. 3141.590000
b. 31.415900
c. ***********
* *
* 31.42 *
* *
***********
d. 3.141590
e. 3.141590
3. Function arguments are used to pass information between the separate modules of a program and
between the main function and its modules. Arguments make it easier for a function to be reused by
other functions or programs. Functions with arguments are building blocks for constructing larger
programs.
CHAPTER 4
SECTION 4.2
1. 1 (TRUE)
0 (FALSE)
1 (TRUE)
1 (TRUE)
3. x = 3.0 y = 4.0 z = 2.0 flag = 0
! ( flag || ( y + z >= x - z ))
0 4.0 2.0 3.0 2.0
6.0 1.0
1
1
0
5. ans is 2.
SECTION 4.3
1. a. not less
b. greater than
SECTION 4.4
1. if (x > y) {
x = x + 10.0;
printf("x Bigger\n");
} else {
printf("x Smaller\n");
printf("y is %.2f\n", y);
}
3. if (engine_type == 'J') {
printf("Jet engine");
speed_category = 1;
} else {
printf("Propellers");
speed_category = 2;
}
SECTION 4.5
1. Additional Program Constant
CAP_GALLONS 100 /* maximum number of gallons (in thousands) for basic
fee */
Additional Program Variable
int excess /* number of gallons over CAP_GALLONS */
Revised Algorithm for comp_use_charge:
1. used is current - previous
2. if used > CAP_GALLONS
excess is used - CAP_GALLONS
use_charge is CAP_GALLONS * PER_1000_CHG +
excess * PER_1000_CHG * 2
else
use_charge is used * PER_1000_CHG
SECTION 4.7
1. Statement Part
salary tax
Effect
if (salary < 0.0) 23500.00 ?
23500.00 < 0.00 is false.
else if (salary < 15000.00)
23500.00 < 15000.00 is false.
else if (salary < 30000.00)
23500.00 < 30000.00 is true.
tax = (salary - 15000.00)
Evaluates to 8500.00.
* 0.18
Evaluates to 1530.00.
+ 2250.00 3780.00
Evaluates to 3780.00.
3. if (pH > 7)
if (pH < 12)
printf("Alkaline");
else
printf("Very alkaline");
else if (pH == 7)
printf ("Neutral");
else if (pH > 2)
printf ("Acidic");
else
printf ("Very acidic");
SECTION 4.8
1. red
blue
yellow
CHAPTER 5
SECTION 5.1
1. Counting loop
1. Initialize sum to 0.
2. Set lcv to 0.
3. while lcv <35
4. Get next test score.
5. Add test score to sum.
6. Increase lcv by 1.
3. Endfile-controlled loop
1. Initialize count to zero.
2. Get first temperature and save input status.
3. while input status does not indicate that end of file has been reached
4. If temperature >100, increase count by 1.
5. Get next temperature and save input status.
SECTION 5.2
1. 0 10
1 9
2 8
3 7
4 6
5 5
SECTION 5.3
1. a. Enter an integer> 5
5
25
125
625
b. Enter an integer> 6
6
36
216
1296
c. Enter an integer> 7
7
49
343
2401
In general, this loop displays n, n
2
, n
3
, and n
4
.
3. count = 0;
sum = 0;
while (count < 5) {
printf("Next number> ");
scanf("%d", &next_num);
sum += next_num;
count += 1;
}
printf("%d numbers were added; ", count);
printf("their sum is %d.\n", sum);
SECTION 5.4
1. For n = 8:
Statement
odd sum
Effect
sum = 0; 0
Initialize sum to 0
odd = 1; 1
Initialize odd to 1
odd < n;
1 < 8 is true
sum += odd; 1 sum = 0 + 1
odd +=2 3 odd = 1 + 2
odd < n;
3 < 8 is true
sum += odd; 4 sum = 1 + 3
odd +=2 5 odd = 3 + 2
odd < n;
5 < 8 is true
sum += odd; 9 sum = 4 + 5
odd +=2 7 odd = 5 + 2
odd < n;
7 < 8 is true
sum += odd; 16 sum = 9 + 7
odd +=2 9 odd = 7 + 2
odd < n;
9 < 8 is false
exit loop
printf("Sum of... Output: Sum of positive odd numbers less
than 8 is 16.
3. The answer to both questions is 0.
5. ++i;
--j;
n = i * j;
m = i + j;
j--;
p = i + j;
7. a. 1 10
2 8
3 6
4 4
5 2
b. j = 10;
for (i = 0; i < 5; ++i) {
printf("%d %d\n", i+1, j);
j -= 2;
}
SECTION 5.5
1. Any initial supply less than 8000 barrels.
3. Number of barrels currently in tank> 8350.8
8350.80 barrels are available.
Enter number of gallons removed> 7581.0
After removal of 7581.00 gallons (180.50 barrels),
8170.30 barrels are available.
Enter number of gallons removed> 7984.2
After removal of 7984.20 gallons (190.10 barrels),
only 7980.20 barrels are left.
*** WARNING ***
Available supply is less than 10 percent of tank's 80000.00-barrel
capacity.
SECTION 5.6
1. step aInitialization of the loop control variable.
step cThe loop repetition condition.
step eThe update of the loop control variable.
SECTION 5.7
1. a. *
**
***
****
*****
b. ***
***
***
***
***
SECTION 5.8
1. The while loop is better because the do-while tests the same condition twice on each iteration.
SECTION 5.9
1. for (status = fscanf(hdd_file, "%d", &next_hdd);
status == 1;
status = fscanf(hdd_file, "%d", &next_hdd)) {
if (next_hdd > heat_deg_days) {
heat_deg_days = next_hdd;
coldest_mon = ct;
}
++ct;
}
SECTION 5.10
1. for (count = 0; count <= n; ++count) {
printf("DEBUG*** count = %d\n", count);
sum += count;
printf("DEBUG*** sum = %d\n", sum);
}
CHAPTER 6
SECTION 6.1
1. void
sum_n_avg(double n1, /* input numbers */
double n2,
double n3,
double *sump, /* output -sum of the three numbers */
double *avgp) /* output -average of the numbers */
3. Reference Where Legal Data Type Value
&many main int *
pointer to gray cell
valp sub double *
pointer to color-shaded cell
code main char 'g'
&code main char *
pointer to white cell
countp sub int *
pointer to gray cell
*countp sub int 14
*valp sub double 17.1
letp sub char *
pointer to white cell
&x main double *
pointer to color-shaded cell
SECTION 6.2
1. Function call
num1 num2 num3
8 12 10
order(&num3, &num2);
order(&num2, &num1); 12 8
order(&num3, &num2); 10 8
This sequence of calls to function order puts num1, num2, num3 in descending order (from largest
to smallest).
SECTION 6.4
1. void
onef(int dat, int *out1p, int *out2p)
{
int tmp;
twof(dat, &tmp, out2p);
. . .
}
void
twof(int indat, int *result1p, int *result2p)
SECTION 6.5
1. Because multiple values need to be returned, and this can only be done by using output parameters.
CHAPTER 7
SECTION 7.1
1. Representational error occurs when the number of bits (binary digits) in the mantissa of a type
double variable is insufficient to exactly represent a certain fraction. Cancellation error occurs when
performing an operation on two numbers that have a very large difference in magnitude, and the
smaller number's effect is lost.
3. x y m n
10.5 7.2 5 2
a. x / (double)m 2.1
b. x / m 2.1
c. (double)(n * m) 10.0
d. (double)(n / m) + y 7.2
e. (double)(n / m) 0.0
SECTION 7.2
1. a. 3
b. 'E'
c. -1
SECTION 7.3
1. typedef enum
{monday, tuesday, wednesday, thursday, friday, saturday, sunday}
dat_t;
a. 0
b. 3
c. 0 (FALSE)
d. friday
e. wednesday
f. 1 (TRUE)
SECTION 7.4
1. 2.0 . . 3.0 IS ONE SUCH INTERVAL. CHAPTER 8
SECTION 8.1
1. x3 is a valid variable name. x[3] is a reference to the fourth element of array x.
3. double sq_root[11];
int cube[11];
SECTION 8.2
1. Before: <art>
After: <art>
SECTION 8.3
1. #include <math.h>
#define MAX_SIZE 11
...
double cube[MAX_SIZE];
int sq_root[MAX_SIZE];
int i;

for (i = 0; i < MAX_SIZE; ++i) {
sq_root[i] = sqrt((double)i);
cube[i] = i * i * i;
}
SECTION 8.4
1. seg_len = sqrt(pow(x[i+1] - x[i], 2) + pow(y[i+1] - y[i], 2));
3. sum = 0;
for (i = 0; i < LIST_SIZE; i += 2)
sum += list[i];
SECTION 8.5
1. It is better to pass the entire array of data rather than individual elements if several elements of the
array are being manipulated by a function.
3. /*
* Gets data to place in dbl_arr until value of sentinel
* is encountered in the input.
* Returns number of values stored through dbl_sizep.
* Stops input prematurely if there are more than dbl_max data
* values before the sentinel or if invalid data is encountered.
* Pre: sentinel and dbl_max are defined and dbl_max
* is the declared size of dbl_arr
* Post: returns 1 for no error, and 0 for any error conditions.
*/
int
fill_to_sentinel(int dbl_max, /* input - declared size of
dbl_arr */
double sentinel, /* input - end of data value in
input
list */
double dbl_arr[], /* output - array of data */
int *dbl_sizep) /* output - number of data values
stored in dbl_arr */
{
double data;
int i, status;
int result = 1;
/* Sentinel input loop */
i = 0;
status = scanf("%lf", &data);
while (status == 1 && data != sentinel && i < dbl_max)
{
dbl_arr[i] = data;
i++;
status = scanf("%lf", &data);
}
/* Issues error message on premature exit */
if (status != 1) {
printf("\n*** Error in data format ***\n");
printf("*** Using first %d data values ***\n", i);
result = 0;
} else if (data != sentinel) {
printf("\n*** Error: too much data before sentinel ***\n");
printf("*** Using first %d data values ***\n", i);
result = 0;
}
/* Sends back size of used portion of array, and error status*/
*dbl_sizep = i;
return (result);
}
5. |
| $
| $ -
| $ ch is -.
SECTION 8.6
1. a. n-1 is returned as the position of the match.
b. The position of the first match is returned.
3. To sort in descending order, rewrite the selection sort algorithm (p. 399) substituting index_of_max
for each occurrence of index_of_min and replacing all occurrences of "smallest" by "largest."
SECTION 8.7
1. a. int i, j, k;
for (i = 0; i < MAXCRS; ++i) {
printf("Processing course number %d: \n", i);
for (j = 0; j < 5; ++j) {
printf(" Campus %d\n", j);
for (k = 0; k < 4; ++k)
printf(" Enter number of ");
switch (k) {
case 0 :
printf("Freshmen > ");
break;
case 1 :
printf("Sophomores > ");
break;
case 2 :
printf("Juniors > ");
break;
case 3 :
printf("Seniors > ");
break;
}
scanf("%d", &enroll[i][j][k]);
}
}
}
b. int i, j,
jcnt; /* Number of juniors. */
jcnt = 0;
for (i = 0; i < MAXCRS; ++i)
for (j = 0; j < 5; ++j)
jcnt += enroll[i][j][2];
c. /*
* Compute the number of students in a course who have a
* specific rank.
* returns -1 if rank or course is out of range.
*/
int
find_students (int enroll[][5][4], int rank, int course)
{
int i, cnt = 0;
if ((rank >= 0 && rank <= 3)
&& (course >= 0 && course < MAXCRS))
for (i = 0; i < 5; ++i)
cnt += enroll[course][i][rank];
else
cnt = -1;
return (cnt);
}
To use,
printf("Number of sophomores in course 2 is %d\n",
find_students(enroll, 1, 2);
d. int i, j, total, upper;
total = 0;
upper = 0;
for (j = 0; j < 5; ++j) {
for (i = 0; i < MAXCRS; ++i)
upper += enroll[i][j][2] + enroll[i][j][3];
printf("Number of upperclass students on campus ");
printf("%d is %d.\n", j, upper);
total += upper;
}
printf("Total upperclass students on all campuses is
%d.\n",total);
SECTION 8.8
1. a. sales[1][fall], sales[1][winter], sales[1][spring],
sales[1][summer]
b. sales[0][spring], sales[1][spring], sales[2][spring],
sales[3][spring], sales[4][spring]
c. sales[0][fall], sales[1][fall], sales[2][fall],
sales[3][fall], sales[4][fall]
sales[0][winter], sales[1][winter], sales[2][winter],
sales[3][winter], sales[4][winter]
sales[0][spring], sales[1][spring], sales[2][spring],
sales[3][spring], sales[4][spring]
sales[0][summer], sales[1][summer], sales[2][summer],
sales[3][summer], sales[4][summer]
CHAPTER 9
SECTION 9.1
1. b
3. /*0123456789012345678901234567890 */
char blanks[] = " ";
or
char blanks[30] = " ";
SECTION 9.2
1. Ad John Quincy Join
SECTION 9.3
1. John Adams
SECTION 9.4
1. a. if (strcmp(name1, name2) == 0)
printf("Names match.\n");
else
printf("Names do not match.\n");
b. if (strcmp(w1, w2) < 0)
strcpy(word, w1);
else
strcpy(word, w2);
c. int i, len;
len = strlen(s1);
i = strlen(s2);
if (i < len)
len = i;
/* Since len will never be larger than STR_LEN, no need to
check for overflow of strings. */
for (i = 0; i < len && s1[i] == s2[i]; ++i)
mtch[i] = s1[i];
mtch[i] = '\0';
SECTION 9.5
1. /* Orders a list of strings according to the string length
shortest to longest */
void order_by_len(char strings[][STRSIZ], /* input/output list of
strings */
int num_str); /* input -number of strings */
void order_by_len(char *strings[], /* input/output list of strings */
int num_str); /* input -number of strings */
SECTION 9.6
1. The problem is that isupper takes a character argument, not a string.
Function strncpy returns a string (char *), not a char.
if (isupper(str[0]))
printf("%s begins with a capital letter\n", str);
SECTION 9.7
1. The & is not needed since dayp, mth_name, yearp are all addresses. Variables dayp and yearp
are integer output arguments (type int *), and mth_name is a character array.
CHAPTER 10
SECTION 10.1
1. a.
multiply(5, 4)

multiply(5, 3)

multiply(5,
2)

multiply(5,
1)
and add 5 and add 5 and add 5

b.
count('d',"dad")

count('d',
"ad")

count('d',
"d")

count('d',
"")
and add 1 if and add 1 if and add 1 if
'd' is a 'd' 'a' is a 'd' 'd' is a 'd'

SECTION 10.2
1. Stack trace of multiply(6, 3)
n m ans
3 6 ?

2 6 ?
3 6 ?

1 6 6
2 6 ?
3 6 ?

2 6 12
3 6 ?

3 6 18
SECTION 10.3
1. int
power_raiser(int base, int power)
{
int ans;
if (power == 0)
ans = 1;
else
ans = base * power_raiser(base, power - 1);
return (ans);
}
3. When fibonacci's argument was 2, the else clause assignment statement would generate a call to
fibonacci (fibonacci(2 - 2) or fibonacci(0)) whose argument value does not satisfy
the precondition.
SECTION 10.4
1. <art>
SECTION 10.5
1. Trace of is_element and is_subset on call to is_subset("bc","cebf")
Entering is_subset with sub = {b, c} and set = {c, e, b, f}
Entering is_element with ele = b and set = {c, e, b, f}
Entering is_element with ele = b and set = {e, b, f}
Entering is_element with ele = b and set = {b, f}
Exiting is_element with ele = b, set = {b, f} and ans = 1
Exiting is_element with ele = b, set = {e, b, f} and ans = 1
Exiting is_element with ele = b, set = {c, e, b, f} and ans = 1
Entering is_subset with sub = {c} and set = {c, e, b, f}
Entering is_element with ele = c and set = {c, e, b, f}
Exiting is_element with ele = c and set = {c, e, b, f} and ans = 1
Entering is_subset with sub = {} and set = {c, e, b, f}
Exiting is_subset with sub = {} and set = {c, e, b, f} and ans = 1
Exiting is_subset with sub = {c} and set = {c, e, b, f} and ans = 1
Exiting is_subset with sub = {b,c} and set = {c, e, b, f} and ans = 1
SECTION 10.6
1. By the formula, moves =2
n
1. Thus a six-disk problem would require 2
6
1 =63 moves.
CHAPTER 11
SECTION 11.1
1. typedef struct {
int degrees,
minutes;
char direction;
} long_lat_t;
3. typedef struct {
char authors[50],
title[50],
publisher[50];
int year;
} catalog_entry_t;
catalog_entry_t book;
strcpy(book.authors,"Hanly, Koffman");
strcpy(book.title,
"Problem Solving and Program Design in C");
strcpy(book.publisher, "Addison-Wesley");
book.year = 2004;
SECTION 11.2
1. /* Displays with labels all components of a long_lat_t
* structure
*/
void
print_long_lat(long_lat_t pos) /* input - one long_lat
structure */
{
printf(" Degrees: %d deg\n", pos.degrees);
printf(" Minutes: %d deg\n", pos.minutes);
printf(" Direction: %c\n", pos.direction);
}
/* Determines whether or not the components of pos_1 and
* pos_2 match
*/
int
long_lat_equal(long_lat_t pos_1, /*input - positions to compare */
long_lat_t pos_2)
{
return (pos_1.degrees == pos_2.degrees &&
pos_1.minutes == pos_2.minutes &&
pos_1.direction == pos_1.direction);
}
/* Fills a type long_lat_t structure with input data.
* Integer returned as function result is
* success/failure/EOF indicator.
* 1 => successful input of pos
* 0 => error encountered
* EOF => insufficient data before end of file
* In case of error or EOF, value of type long_lat_t
* output argument is undefined.
*/
int
scan_long_lat(long_lat_t *pos) /* output - address of
long_lat_t
structure to fill */
{
int result;
result = scanf("%d%d %c", &pos->degrees,
&pos->minutes,
&pos->direction);
if (result == 3)
result = 1;
return (result);
}
SECTION 11.3
1. When time_now is passed as an argument to function new_time, the values of its components are
copied into new_time's formal parameter time_of_day. Assignments to these components just
change the function's local copy of the structure.
SECTION 11.4
1. (6.50 + 5.00i) + (3.00 - 4.00i) = (9.50 + 1.00i)
Second result = (1.50 + 5.00i)
SECTION 11.5
1. The & is not applied to units because units is an array of type unit_t, and an array name with
no subscript always represents the address of the array's initial element.
SECTION 11.6
1. Twenty-two bytes are allocated for a variable of type hair_info_t, but only four are in use when
wear_wig is valid.
CHAPTER 12
SECTION 12.1
1. a. n = 123 x = 3.145 str = "xyz" ch = \n
b. n = 123 x = 3.145 str = "xyz" ch = \n
c. n = 3 x = 123.0 str = 145 ch = .
d. n = 35 x = ?? str = xyz ch = z
3. fscan_complex(FILE *inp
...
status = fscanf(inp,
SECTION 12.2
1. fread(&exec, sizeof (person_t), 1, psn_inp);
3. fwrite(&exec, sizeof (person_t), 1, psn_outp);
5. fread(&num_err[3], sizeof (int), 1, nums_inp);
SECTION 12.3
1. a. Low bound for category ="printer stands"
High bound for category ="printer stands"
High bound for price =99.99
b. Low bound for stock number =5241
High bound for stock number =5241
c. Low bound for category ="data cartridges"
High bound for category ="data cartridges"
3. match does not check the number because it is called from within a while loop that calls it only for
stock numbers that are in range.
CHAPTER 13
SECTION 13.1
1. a. A microwave oven quickly heats up the objects placed inside of it when the controls are
correctly set. It is not necessary to know that the oven is actually emitting energy that is
specifically designed to agitate the water molecules in a substance and thus heat up the food.
b. A television allows one to see various programs simply by turning it on and turning the channels.
The user is totally isolated from the electronics used to tune and display the program signal.
c. A calculator allows the user to compute myriad numerical calculations without having any
knowledge of the electronics and logic embedded in the calculator.
SECTION 13.2
1. A system header file name is surrounded by angular brackets (< >), whereas a personal header file
name would be surrounded by quotes ("").
3. what, how
SECTION 13.3
1. This macro is included in the header file so the user will know the string size for the planet structure.
SECTION 13.4
1.
unit_max
is auto
found
is auto

convert
is extern
quantity
is auto
SECTION 13.5
1. Having constant macro and type definitions in just one file makes maintenance and modification of the
library more straightforward.
SECTION 13.6
1. #if defined (UNIX)
printf("Enter <ctrl-d> to quit.")
#elif defined (VMS)
printf("Enter <ctrl-z> to quit.")
#endif
SECTION 13.7
1. The following code would be added right after the line int ch;
/* See if arguments were included */
if (argc < 3) {
printf("\nPlease include input and output file name.\n");
exit(1);
}
SECTION 13.8
1. y = DOUBLE(a - b) y = (a - b) + (a - b) OK,
but the macro should have been written as #define DOUBLE(x) ((x) + (x))
3. if (DISCRIMINANT(a1, b1, c1) == 0)
if (((b1) * (b1) - 4 * (a1) * (c1)) == 0)
CHAPTER 14
SECTION 14.1
1. num_listpointer, array
3. inot a pointer
5. inppointer, file pointer
7. denomppointer, output parameter
9. statusnot a pointer
SECTION 14.2
1. printf("%c", *letp);
3. strcpy(planetp->name, "Uranus");
5. letp = (char *) calloc(30, sizeof (char));
SECTION 14.3
1. AC DC AC
9 115 220
SECTION 14.4
1. Find 5
cur_nodep is headp (start)
cur_nodep is headp->restp (4 checked)
cur_nodep is headp->restp->restp (1 checked)
cur_nodep is headp->restp->restp (5 checked and cur_nodep is returned)
Find 2
cur_nodep is headp (start)
cur_nodep is headp->restp (4 checked)
cur_nodep is headp->restp->restp (1 checked)
cur_nodep is headp->restp->restp->restp (5 checked)
cur_nodep is NULL (NULL is returned)
Find 4
cur_nodep is headp (start)
cur_nodep is headp (4 checked and cur_nodep is returned)
SECTION 14.5
1. <art>
SECTION 14.6
1. <art>
SECTION 14.7
1. The call to delete_ordered_node does not apply the address-of operator because
is_deletedp is already a pointer to the integer is_deleted flag.
SECTION 14.8
1. a. The left tree is a binary search tree. The right tree is not a binary search tree because the value 45
is found in its left subtree. This condition violates the requirement that a root have a larger key
than each item in its left subtree.
b. Inorder traversal of each tree:
left tree: 10, 15, 20, 40, 50, 55, 60
right tree: 25, 30, 45, 40, 50, 55, 60
c. The key values 41, 42,..., 48, 49.
CHAPTER 15
SECTION 15.1
1. A running program can be pre-empted at any time by the hardware interrupt system allowing access to
the CPU in a predictable way that is independent of the programs that are running and adjustable based
on criteria such as priority.
3. Time-sharing refers to allocating each system user a portion of the available CPU time thus sharing the
CPU time among multiple users. A time slice is the unit of time allocated to a system user during their
portion of the available CPU time.
SECTION 15.2
1. With the fork function.
3. With the execl function.
SECTION 15.3
1. Pipes may only be used with processes that are running on the same CPU and that have a common
ancestor.
3. With the dup2 function.
SECTION 15.4
1. With the pthread_create function.
3. With the pthread_mutex_lock and pthread_mutex_unlock functions.
SECTION 15.5
1. /
* Check for active consumer threads, wait for all active consumer *
threads to finish and cancel the producer thread * Pre: ptid is
defined */
3. /*
* Consumer thread function that runs for each pump request until *
the pump request has been satisfied, each consumer thread runs * its
own copy of the consumer thread function * Pre: global parameters are
defined and have been initialized */
CHAPTER 16
SECTION 16.1
1. xis12.33iis100
iis100xis12.3
3. const double KMS_PER_MILE = 1.609;
const int DAYS_IN_WEEK = 7;
SECTION 16.2
1. a. do not use
b. Constructors
c. const
d. class name, scope resolution
e. the types of the operands

You might also like