{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Homework_2_Solutions

Homework_2_Solutions - Programming Languages G22.2110-001...

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

View Full Document Right Arrow Icon
Programming Languages G22.2110-001 – Spring 2011 Dr. Jean-Claude Franchitti New York University Computer Science Department Courant Institute of Mathematical Sciences Homework # 2 Solutions PROBLEM 1 (3.5): Consider the following pseudocode. 1. procedure main 2. a:integer:=1 3. b:integer:=2 4. procedure middle 5. b : integer := a 6. procedure inner 7. print a, b 8. a : integer := 3 9. – – body of middle 10. inner() 11. print a, b 12. – – body of main 13. middle() 14. print a, b Suppose this was code for a language with the declaration-order rules of C (but with nested subroutines)—that is, names must be declared before use, and the scope of a name extends from its declaration through the end of the block. At each print statement, indicate which declarations of a and b are in the referencing environment. What does the program print (or will the compiler identify static semantic errors)? Repeat the exercise for the declaration-order rules of C# (names must be declared before use, but the scope of a name is the entire block in which it is declared) and of Modula-3 (names can be declared in any order, and their scope is the entire block in which they are declared). Answer: With C rules, line 7 refers to the a and b declared on lines 2 and 5, respectively; line 11 refers to the a and b declared on lines 8 and 5, respectively; and line 14 refers to the a and b declared on lines 2 and 3, respectively.
Image of page 1

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

View Full Document Right Arrow Icon
The program prints: Line 7: a=1 b=1 Line 11: a=3 b=1 Line 14: a=1 b=2. With C# rules, the compiler should produce use-before-define errors for a at lines 5 and 7. With Modula-3 rules, line 7 refers to the a and b declared on lines 8 and 5, respectively; line 11 refers to the a and b declared on lines 8 and 5, respectively; and line 14 refers to the a and b declared on lines 2 and 3, respectively.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern