Solution Manual For A First Book of C 4th Edition

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Solution Manual for A Firstof C++, 4th Edition

To download the complete and accurate content document, go to:


https://testbankbell.com/download/solution-manual-for-a-first-book-of-c-4th-edition/
Solution Manual for A Firstof C++, 4th Edition

A First Book of C++, 4th Edition


Chapter 8 Solutions
Exercises 8.1

1. The value stored in the variable total, the number of bytes reserved for the
variable, and where these bytes are located in memory.
2. &average means the address of the variable named average.
3. The address of a variable is always its starting location, so the addresses are as
follows:
&temp is 16892
&dist is 16896
&date is 16900
&miles is 16908
4.
a. See solution file pgm8-1ex4a.cpp.
b. Answers will vary.
c. See solution file pgm8-1ex4c.cpp. The output indicates that storage for each
variable was allocated in the order the variables were declared. In almost all
cases, variables are stored starting at a word boundary. That is, if the computer
has a 32-bit word (4 bytes), each variable is stored starting at the beginning of
the 4 bytes defining a word.
5. An address is stored in a pointer.
6.
a. *xAddr
b. *yAddr
c. *ptYld
d. *ptMiles
e. *mptr
f. *pdate
g. *distPtr
h. *tabPt
i. *hoursPt
7.
a. int *yAddr;
b. char *chAddr;

Visit TestBankBell.com to get complete for all chapters


c. long *ptYr;
d. double *amt;
e. int *z;
f. float *qp;
g. int *datePt;
h. double *yldAddr;
i. float *amtPt;
j. char *ptChr;
8.
a. Each of these variables is a pointer, which means addresses are stored in each
variable.
b. They aren’t descriptive names and don’t indicate that they’re pointers.
9. For this exercise, several descriptions can be given. Each of the following is
correct:
a. keyAddr is a pointer to a character.
keyAddr points to a character.
The variable whose address is in keyAddr is a character.
b. m is a pointer to an integer.
m points to an integer.
The variable whose address is in m is an integer.
c. yldAddr is a pointer to a double.
yldAddr points to a double.
The variable whose address is in yldAddr is a double.
d. yPtr is a pointer to a long integer.
yPtr points to a long integer.
The variable whose address is in yPtr is a long integer.
e. pCou is a pointer to a double.
pCou points to a double.
The variable whose address is in pCou is a double
f. ptDate is a pointer to an integer.
ptDate points to an integer.
The variable whose address is in ptDate is an integer.
10. All pointer declarations must have an asterisk. Therefore, c, e, g, and i are pointer
declarations.
11. Only a, h, n, and s are valid assignment statements.
12.
Variable: ptNum Variable: amtAddr
Address: 500 Address: 564
8096 16256

Variable: zAddr Variable: numAddr


Address: 8024 Address: 10132
20492 18938

Variable: ptDay Variable: ptYr


Address: 14862 Address: 15010
20492 694

Variable: years Variable: m


Address: 694 Address: 8096
2011

Variable: amt Variable: firstnum


Address: 16256 Address: 18938
154 154

Variable: slope Variable: k


Address: 20492 Address: 24608
25 154

Exercises 8.2

1.
a. *(prices + 5)
b. *(grades + 2)
c. *(yield + 10)
d. *(dist + 9)
e. *mile
f. *(temp + 20)
g. *(celsius + 16)
h. *(num + 50)
i. *(time + 12)
2.
a. message[6]
b. amount[0]
c. yrs[10]
d. stocks[2]
e. rates[15]
f. codes[19]
3.
a. The declaration double prices[5]; causes storage space for five double-
precision numbers, creates a pointer constant named prices, and equates the
pointer constant to the address of the first element (&prices[0]).
b. Each element in prices contains 8 bytes and there are five elements, for a total
of 40 bytes.
c. prices
&prices[0]

prices[0] or prices[1] or
*prices *(prices + 1)

prices[3] or prices[4] or
*(prices + 3) *(prices + 4)

prices[5] or
*(prices + 5)

d. The byte offset for this element from the beginning of the array is 3 * 8 = 24
bytes.
4.
a. See solution file pgm8-2ex4a.cpp.
b. See solution file pgm8-2ex4b.cpp.
5. See solution file pgm8-2ex5.cpp.
6.
a. See solution file pgm8-2ex6a.cpp.
b. See solution file pgm8-2ex6b.cpp.
7. See solution file pgm8-2ex7.cpp.
8. See solution file pgm8-2ex8.cpp.

Exercises 8.3

1. See solution file pgm8-3ex1.cpp.


2.
a. See solution file pgm8-3ex2a.cpp.
b. See solution file pgm8-3ex2b.cpp.
3.
a. See solution file pgm8-3ex3a.cpp.
b. See solution file pgm8-3ex3b.cpp.
c. See solution file pgm8-3ex3c.cpp.
4. See solution file pgm8-3ex4.cpp.
5. See solution file pgm8-3ex5.cpp.
6. See solution file pgm8-3ex6.cpp.

Exercises 8.4

1.
void sortArray(double inArray[500])
void sortArray(double inArray[])
void sortArray(double *inArray)
2.
void findKey(char select[256])
void findKey(char select[])
void findKey(char *select)
3.
double maximum(double speed[100])
double maximum (double speed[])
double maximum (double * speed)
4.
int findMin(int *vals, int numels)
{
int i, min = *vals++;

for (i = 1; i < numels; i++, vals++)


{
if (min > *vals)
min = *vals;
}
return min;
}
5. The problem with this function is in the following statement:
if(max < *vals++)
max = *vals;
It compares the correct value with max but then increments the address in the
pointer before any assignment is made. Therefore, the element assigned to
max by the expression max = *vals is one element beyond the element
pointed to in the parentheses.
6.
a. See solution file pgm8-4ex6a.cpp.
b. See solution file pgm8-4ex6b.cpp.
7.
a. See solution file pgm8-4ex7a.cpp.
b. See solution file pgm8-4ex7b.cpp.
8. See solution file pgm8-4ex8.cpp.
9. The function is written as follows and tested in solution file pgm8-4ex9.cpp:
void trimfrnt(char *strng)
{
char * strng2;
strng2 = strng; // store the starting address

while(*strng2 == ' ') ++strng2; // move over any leading blanks


while(*strng++ = *strng2++) ; // copy remaining chars up to and
// including the '\0'

return;
}
10. The function is written as follows and tested in solution file pgm8-4ex10.cpp:
void trimrear(char *strng)
{
char * strng2;
strng2 = strng; // store the starting address

while(*strng != '\0') ++strng; // move to the end of the string


--strng; // move to char before '\0'
while(*strng == ' ') --strng; // move over any tailing blanks
*(++strng) = '\0'; // terminate the string
strng = strng2; // reset the starting address

return;
}
11. See solution file pgm8-4ex11.cpp.
12. The output is as follows:
33
16
99
34

This is why:
*(*val) = *(val[0]) = val[0][0] = 33;
*(*val + 1) = *(val[1]) = val[1][0] = 16;
*(*(val + 1) + 2) = *(*(val[1]) + 2) = *(val[1][2]) = 99;
*(*val) + 1 = *(val[0]) + 1 = val[0][0] + 1 = 33 + 1 = 34;
Solution Manual for A Firstof C++, 4th Edition

b. The notation val[1][2] is valid in the function.

Visit TestBankBell.com to get complete for all chapters

You might also like