> >HW3: Due 5/21/98. >================== > >1) What significant justification is there for the -> operator in C >and C++ ? Although it adds no functionality, the -> operator increases the readability of the code. The code becomes cleaner by replacing (*ptr).field, and it is especially needed since pointers are the main way to pass structures from function to function in C. [TA's comment: It is important to note that "->" is essential because pointers are so oftenly used in C. If it was something that is rarely used, it's probably not worth to add burdle on the design of the language (and the compiler).] >2) Assume the following rules of associativity and precedence for >expressions: > > Precedence: Highest *,/,not > +,-,&,mod > -(unary) > =,<>,<,<=,>=,> > and > Lowest or,xor > Associativity: Left to right > >Show the order of evaluation of the following expressions by >parenthesizing all subexpressions and placing a superscript on the >right parenthesis to indicate order. > > a) a * b - 1 + c 1 2 3 (((a * b) - 1) + c) > b) a * (b - 1) / c mod d 1 2 3 (((a * (b - 1)) / c) mod d) > c) (a - b) / c & (d * e / a -3) 1 2 3 4 (((a - b) / c) & (((d * e) / a) -3)) > d) -a or c = d and e 1 2 3 4 ((-a) or ((c = d) and e) ) > e) a > b xor c or d <= 17 1 3 2 4 (((a > b) xor c) or (d <= 17) ) > f) - a + b 1 2 (- (a + b) ) >3) Consider the following C program: > > int fun (int *i) { > *i += 5; > return *i; > } > > void main () { > int x = 3; > x = x + fun (&x); > } > >What is the value of x after the assignment stmt in main, assuming, > >a) operands are evaluated left to right 3 + fun(&x), where x = 3 x = 11 >b) operands are evaluated right to left. temp + fun(&x), where x = 3 8 + 8 x = 16 >4) Compare the FORTRAN computed GOTO with the Pascal case stmt, >especially in terms of readability and reliability. readability: The Pascal statement is more readable, giving a label for each case. Pascal gives the user an explicit default statement (which can be approximated in FORTRAN, but not as easy to read). reliability: The Pascal statement is also more reliable, forcing the programmer to match a label with the statements to be executed. Pascal aids with encapsulation, forcing the programmer to group the case statements together; FORTRAN line number may be anywhere in the code. >5) Rewrite the following code segment using loop structure in the >following languages: > >a) Pascal for k := (j+13)/27 to 10 step 1 i := 3 * (k+1) - 1 loop; >b) FORTRAN 77 DO 10 K = (J+13)/27, 10, 1 I = 3 * (K+1) - 1 10 CONTINUE > c) Ada for k in (j+13)/27 .. 10 loop i := 3 * (k+1) - 1; end loop; > d) C for (k=(j+13)/27; k<=10; k++) i = 3 * (k+1) - 1; > k := (j + 13) / 27 > loop: > if k > 10 then goto out > k := k + 1 > i := 3 * k - 1 > goto loop > out: .... > > Assume all variables are integer type. Discuss which language, for > this code has best writability, and the best combination of two. Ada has the best writability, explicitly stating what will be the range that the for loop will count, and also having an explicit "end loop" statement, reiterating which code segment the end belongs to. A better combination may be with Pascal to allow steps of particular increments. The best combination of readability/writeability could be Pascal, allowing the step statement allowing various increments; and also the explicit for..loop statement explicitly giving the bounds of the loop in the code. >6) Consider the following ALGOL 60 style for statement: > >for i := j + 1 step i * j until 3 * j do j := j + 1 > >Assume that the initial value of j is 1. List the sequence of values >for the variable i used, assuming the following semantics: > >a) All expressions are evaluated once at loop entry. Stmt | Iter# | j | i | x -----+-------+-----+----------- for | 0 | 1 | 2 | ; Initial Entry step | 0 | 1 | 2 | 2 ; Step Calculated until| 0 | 1 | 2 | 3 ; Until Calculated do | 1 | 2 | 2 ; Do Performed until| 1 | 2 | 2 ; Until Checked step | 1 | 2 | 4 ; Step Performed do | 1 | 3 | 4 ; Do Performed until| 1 | 3 | 4 ; Until Checked >b) All expressions are evaluated before at each iteration. Stmt | Iter# | j | i | x -----+-------+-----+----------- for | 0 | 1 | 2 | ; Initial Entry until| 1 | 1 | 2 | 3 ; Until Calculated do | 1 | 2 | 2 ; Do Performed until| 1 | 2 | 2 ; Until Checked step | 1 | 2 | 2 | 4 ; Step Calculated until| 2 | 2 | 2 | 6 ; Until Calculated step | 2 | 2 | 6 ; Step Performed do | 2 | 3 | 6 ; Do Performed until| 2 | 3 | 6 ; Until Checked [Usually, loop until exceeding the until value, so once more.] step | 2 | 3 | 6 | 18 ; Step Calculated until| 3 | 3 | 6 | 9 ; Until Calculated step | 3 | 3 | 24 ; Step Performed do | 3 | 4 | 24 ; Do Performed until| 3 | 4 | 24 ; Until Checked >c) step expressions are evaluated once at loop entry, and until >expressions are evaluated before each iteration. Stmt | Iter# | j | i | x -----+-------+-----+----------- for | 0 | 1 | 2 | ; Initial Entry step | 0 | 1 | 2 | 2 ; Step Calculated until| 1 | 1 | 2 | 3 ; Until Calculated do | 1 | 2 | 2 ; Do Performed until| 1 | 2 | 2 ; Until Checked step | 2 | 2 | 4 ; Step Performed until| 2 | 2 | 4 | 6 ; Until Calculated do | 1 | 3 | 4 ; Do Performed until| 1 | 3 | 4 ; Until Checked [...] Will never end; until grows faster than step >d) until expressions are evaluated once at loop entry, and step >expressions are evaluated before each iteration, just before the loop >counter is incremented. Stmt | Iter# | j | i | x -----+-------+-----+----------- for | 0 | 1 | 2 | ; Initial Entry until| 0 | 1 | 2 | 3 ; Until Calculated do | 1 | 2 | 2 ; Do Performed until| 1 | 2 | 2 ; Until Checked step | 2 | 2 | 2 | 4 ; Step Calculated step | 2 | 2 | 6 ; Step Performed do | 2 | 3 | 6 ; Do Performed until| 2 | 3 | 6 ; Until Checked >In all cases, when more than one expression is evaluated at the same >time, they are evaluated in left-to-right order. Also, the assignment >is always done as soon as its RHS is evaluated.