exam3topicssol(1)

# Return value if subprogram is a function 5 consider

• Notes
• 4

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

return value - if subprogram is a function 5. Consider the axioms of assignment, composition, and loop below: Assignment { P [E/V] } V := E { P } Composition { P } S 1 { Q } , { Q } S 2 { R } { P } S 1 ; S 2 { R } Loop { P&B } S { P } { P } while B loop S end loop { P& ¬ B } Prove the correctness of the following program segment. { n = j × (j + 1) / 2 & i 0 } while j ! = i loop j := j + 1; n := n + j; end loop { n = j × (j + 1) / 2 & i 0 & ¬ (j negationslash = i) } Show all steps used in the proof. Noting that the loop invariant I n = j × (j + 1) / 2 & i 0, we first apply the Loop rule { I & j negationslash = i } j := j + 1; n := n + j { I } { I } while j ! = i loop j := j + 1; n := n + j; end loop { I & ¬ (j negationslash = i) } (1)

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

followed by the Composition rule. { I & j negationslash = i } j := j + 1 { P 1 } , { P 1 } n := n + j { I } { I & j negationslash = i } j := j + 1; n := n + j { I } (2) To determine P 1 , we apply the Assignment axiom. { n + j = j × (j + 1) / 2 & i 0 } n := n + j { n = j × (j + 1) / 2 & i 0 } (3) Therefore, P 1 n + j = j × (j + 1) / 2 & i 0. We apply the Assignment axiom again to prove { n + (j + 1) = (j + 1) × ((j + 1) + 1) / 2 & i 0 } j := j + 1 { n + j = j × (j + 1) / 2 & i 0 } (4) Note that n + (j + 1) = (j + 1) × ((j + 1) + 1) / 2 & i 0 n = j × (j + 1) / 2 & i 0 I. Since I & j negationslash = i I, the proof is complete. 6. Consider the Pascal array declaration: A : array [ 10 .. 10, 5 .. 5, 1 .. 10] of T; Assuming that T requires 10 bytes of storage and A is stored at relative location 100, what is the relative location of A [1, 1, 2]?
This is the end of the preview. Sign up to access the rest of the document.
• Fall '09
• css, activation record, virtual method table

{[ 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