
SOLUTIONS TO PROBLEMS Chapter 2 2.1 255, 12774, 255, 19414 2.2 Decimal Octal Hexadecimal 12 014 OxC 214 0326 OxD6 672 01240 Ox2AO 1999 03717 Ox7CF 2.3 1.432565E-I 2.4 \t = \Oll, \b = \010, \r = \015, \f = \014, \\ = \134 2.5 'a' = 97, '#' = 35, '\t' = 9, 'C'= 67 2.6 String length = 15, number of bytes consumed = 16 2.7 Ox3000 2.8 3_WAY does not begin with a letter. NET_$ and abs val both contain illegal characters ($ and space). 2.9 a + (b / (-e)) 2.10 a = (((b. c) / (-d)) % e) 2.11 a « (b + c) 2.12 a = (b ! = c) 2.13 a -= ((-(b--))-(--e)) 2.14 (a I b) II c 2.15 (!a) A (-b) 2.16 (a && b) II c 197 198 Solutions to Problems Expression value after assignment to char int float Side effects Comments 2.17 C 67 67.0 None 2.18 Z 90 90.0 None 2.19 J 74 74.0 None 2.20 i 105 105.0 None 2.21 2 50 50.0 None 2.22 \036 30 30.0 None 2.23 \0 0 0.0 None 2.24 \001 1 1.0 None 2.25 \0 0 0.0 None 2.26 \0 0 0.0 None 2.27 \036 30 30.0 None 2.28 \036 30 30.0 None 2.29 \001 1 1.0 None 2.30 \005 5 5.0 None 2.31 \020 16 16.0 None 2.32 p 80 80.0 None 2.33 W 87 87.5 None 2.34 x 120 120.0 None 2.35 e 101 101.0 None 2.36 g 103 103.0 None 2.37 @ 64 64.0 None 2.38 \220 144 144.0 None 2.39 \201 4225 4225.0 None 2.40 \002 2 2.0 None 2.41 \n 10 10.0 None 2.42 \t 9 9.7468 None 2.43 105 105.0 None 2.44 K 75 75.0 None 2.45 # 35 35.0 None 2.46 E 69 69.0 None 2.47 # 35 35.0 None 2.48 V 86 86.0 None 2.49 b 98 98.0 as[2].m4[0] = 'b' 2.50 z 122 122.0 as[2].m4[2] = 'z' 2.51 C 67 67.0 as[2].m4[1] = 'F' 2.52 \n 10 10.0 ai[O] = 10 2.53 \017 15 15.0 ai[2] = 90 2.54 # 35 35.0 ai[l] = 23 2.55 \003 3 3.5 af[0] = 3.5 2.56 \010 8 8.5 af[2] = 8.5 2.57 \006 6 6.0 new af[O] same as old af[0] 2.58 z 90 90.0 None 2.S9 x 120 120.0 None 2.60 A 65 65.0 None 2.61 \001 1 1.0 None 2.62 c 99 99.0 as[2].m4[2] = 'B' 2.63 d 100 100.0 il = 11 2.64 \0 256 256.0 as[2].m2 = ai[O] = 16 Solutions for Chapter 2 199 Expression value after assignment to char int float Side effects Comments 2.65 \003 3 3.0 as[2].m3 = 8.5; af[l] = 3.0 2.66 \0 0 0.0 i2 = 21 2.67 \001 I 1.0 il=ll 2.68 \0 0 0.0 i2 = ai[l] = 21; pi3 = &ai[2]; ai[2] = 0 2.69 K 75 75.5 s2.m3 = 4.5; af[O] = 4.5 (assuming that 4.0 is nearer to 4.5 than it is to 5.0); pfJ = &af[I]; af[l] = 75.5 2.70 \t 9 9.5 aps[O] = &as[2] 2.71 # 35 35.0 None 2.72 Z 90 90.0 None 2.73 \n 10 10.0 il=ll 2.74 \016 14 14.0 i2 = 19 2.75 \001 1.0 None 2.76 \001 I 1.0 None 2.77 \0 0 0.0 None 2.78 \001 I 1.0 None 2.79 a 97 97.0 s3.m4[1] = 'a'; pel = &s3.m4[1] 2.80 Z 90 90.0 aps[O] = &as[2] 2.81 \0 0 0.0 None 2.82 \001 I 1.0 None 2.83 ( 40 40.0 None 2.84 \003 3 3.0 None 2.85 \024 20 20.0 cl = 'B'; i2 = 21 2.86 \017 15 15.0 api[O] = &ai[ I] 2.87 \002 2 2.5 None 2.88 \004 4 4.0 fl = 4.0 2.89 \005 5 5.0 ai[O] = 5 2.90 \001 I 1.0 il = -10 2.91 # 35 35.0 il = 15; i2 = 25; i3 = 35 pil = &ai[2] 2.92 \017 15 15.0 il = 15; ai[O] = 16 2.93 # 35 35.0 None 2.94 \005 5 5.4772 None 2.95 \001 I 1.0 None 2.96 Z 90 90.0 None 2.97 \f 12 12.0 None 2.98 \001 I 1.0 None 2.99 \341 225 225.0 None 2.100 \001 1.0 None I The space between the "/" and the ".", in Problem 2.46, is the only one known to be mandatory in all of the expressions in Problems 2.17-2.100. If this space is omitted, the "/." combination will signal the beginning of a comment, causing the compiler to ignore all subsequent code (until a matching "./" is later encountered). 200 Solutions to Problems Chapter 3 3.1 char cl = & A', c2 = 'B " c3 = 'C'; int il = 10, i2 = 20, i3 = 30; floatfl = 1.5, f2 = 2.5, f3 = 3.5; 3.2 s truc t mixed { char ml; int m2; float m3; char m4 [3] ; }; structmixedsl = {'G', 40, 4.5, {'J', 'K', 'L'}}; struct mixed s2 = {'H', 50, 5.5, {'M', 'N', 'O'}}; structmixeds3 = {'I', 60, 6.5, {'p', 'Q', 'R'}}; 3.3 char *pel = &cl, *pc2 = &c2, *pc3 = &c3; int *pil = &il, *pi2 = &i2, *pi3 = &i3; float *pfl = &fl, *pf2 = &f2, *pf3 = &f3; s truc t mixed *psl = &sl, *ps2 = &s2, *ps3 = &s3; 3.4 char ac[] = {'D', 'E', 'F'}; int ai [] = {15, 25, 35}; float af[] = {4. 0, 5.0, 6. O}; structmixedas[] = {{'S', 70, 7.5, 'V', 'W', 'X'}, {'T', 80, 8.5, 'X', 'Y', 'Z'}, {'U', 90, 9.5, 'A', 'B', aCt} }; 3.5 char *apc [] = {ac, ac + I, ac + 2 }; int*api[] = {ai, ai + I, ai + 2}; float *apf[] = {af, af + I, af + 2}; structmixed*aps[] = {as, as + I, as +2}; 3.6 Fc (z) / * gi ven char, if upper-conver t to lower, * else-leave alone 0/ int Z; { 3.7 Fi (z) /0 given int arg, return its square */ int Z; { } Solutions for Chapter 3 201 3.8 double Ff(z) fo given floating arg, o return its square root of double z; { 3.9 char *Fpc(z,y) fo given 2 chars, search all char o arrays for charI. If found, o replace wi th char2 and return o ptr to it. Else, return NULL of int z; int y; { 3.10 int *Fpi (z) fo find nearest int in ai, replace it, o and return pointer to it of int z; { 3.11 float oFpf(z) fo find nearest float in af, replace o it, and return pointer to it of double z; { >.12 struct mixed oFps (z) f. given char, search m4' s of * all structures, and return ptr to o struct of first match or NULL of int z; { 202 Solutions to Problems 3.13 int i; fenlO { fen20 { maino { fenlO ; fen20 ; } 3.14 int i; fenlO { int j; fen2 () { int k; main(){ fenl (); fen2 (); } 3.15 First, convert each function definition's header declaration from the form fenl () [or fen2 () j into the form ehar *fenl () [or char *fen2 0 j.lfthe main function is positioned in the source file after these function definitions appear (as is the case in the solution given above), no further modifications are necessary. However, if the ma i n function is defined before the function definitions are encountered, then it too must be modified as follows: mainO{ char *fenlO, *fen20; fenlO ; fen20 ; } to ensure that the compiler is aware of the types returned by these functions, before their definitions are known. Solutions for Chapter 3 203 3.16 3 2 3.17 Variables visible file 1 int a; static float b; a mainO{ a, b short c; a, b a, b, c a, b, c a, b, c funct (d) a, b int d; a, b { a, b, d short e; a, b, d a, b, d, e a, b, d, e a, b, d, e file 2 subr (f) float f; { f extern float b; f f, b (from file 3) f, b " " f, b file 3 float b; { b int g; b b, g b, g b, g 3.18 First, the values of the arguments must be pushed onto the stack. Each argument expression is evaluated in tum and its value is pushed onto the stack. (A single stack is assumed in this example.) The order in which the arguments appear is immaterial, provided that the called routine is able to properly distinguish them. Next, the CALL is executed. The execution of this instruction pushes the return address onto the same stack. Next, the called routine begins by saving (on the same stack) the values of any processor registers that it will employ in its computation. Thus, before the function's calculation even begins, the stack has grown by the argument values (of varying sizes), which are under the return address, which is, in tum, under the set of saved register values.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages48 Page
-
File Size-