Assignment_2_Solutions (1)

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

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

Try to define a procedure Sum(g, y), which has two parameters g, y. We 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

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern