Assignment_2_Solutions (1)

Assignment_2_Solutions (1) - 1) Exercise 3-5: Try to define...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Try to define a procedure Sum( g , y ), which has two parameters g , y. W e know in ALGOL , by default, the parameters are passed by name, that means the real parameters just SUBSTITUTE the parameters in the procedure. 1) Exercise 3-5: (We could have sued " for .. step .01 .. " which is legal in ALGOL to have non integer step! See: http://everything2.com/index.pl?node_id=1401345 But, we kept the original Sum function code as is) *** Procedure Sum real procedure sum (g, y); real g; real y; begin real S; S=0; y:=0; for y:= y + 0.01 while y<=1 do S := S + g; sum := S/100 end; *** Procedure f(x) real procedure f(x); value x; real x; f := x^2 +1; *** Caller of Sum(f(x)) begin real sumf; real x; sumf := sum (f(x), x) end a) both x and y are passed by value: 2) Exercise 10: Statements i A[1] A[2] A[3] x y Cumulative output A[1]:=7; A[2]:=11; A[3]:=13; i:=1; 1 7 11 13 ? ? P(A[i],i); 1 7 11 13 7 1 y:=2; 1 7 11 13 7 2 Print(x); 1 7 11 13 7 2 7 i:=3; 3 7 11 13 7 2 Print(x); 3 7 11 13 7 2 7, 7 i:=3; 3 7 11 13 7 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Print(x); 3 7 11 13 7 2 7, 7, 7 Print(y); 3 7 11 13 7 2 7, 7, 7, 2 P(i,A[i]); 3 7 11 13 3 13 y:=2; 3 7 11 13 3 2 Print(x); 3 7 11 13 3 2 7, 7, 7, 2, 3 i:=3; 3 7 11 13 3 2 Print(x); 3 7 11 13 3 2 7, 7, 7, 2, 3, 3 i:=3; 3 7 11 13 3 2 Print(x); 3 7 11 13 3 2 7, 7, 7, 2, 3, 3, 3 Print(y); 3 7 11 13 3 2 7, 7, 7, 2, 3, 3, 3, 2 b) x is passed value and y is passed by name: Statements i A[1] A[2] A[3] x y Cumulative
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Assignment_2_Solutions (1) - 1) Exercise 3-5: Try to define...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online