100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

CMPSC 461: Programming Language Concepts Assignment 5 Solutions

Rating
-
Sold
-
Pages
3
Grade
A+
Uploaded on
25-09-2023
Written in
2023/2024

CMPSC 461: Programming Language Concepts Assignment 5 Solution Problem 1 [9pt] Consider the following C program: int SumOfSquares(int n) { if (n <= 0) return 0; else return n*n+SumOfSquares(n-1); } a) (5pt) Write down a tail recursive implementation of function SumOfSquares in C language. You can use helper function in your solution. Solution: int SumOfSquares(int n) { return SumOfSquaresHelp(0, n); } int SumOfSquaresHelp(int a, int n) { if (n <= 0) return a; else return SumOfSquaresHelp(a+n*n, n-1); } b) (4pt) An “optimizing” compiler will often be able to generate efficient code for recursive functions when they are tail-recursive. Refer to activation record, briefly explain how a compiler may “reuse” the same activation record for your solution in a). Solution: Before the recursive function call, the compiler can reuse the memory space that stores the caller’s parameters for the parameters passed to the callee. Then, the control flow can be switched to the callee without the calling sequences. Problem 2 [6pt] In early implementations of Fortran language, a compiler may choose to use static allocation (i.e., allocation in the static area) for local variables and parameters, effectively arranging for the variables of different invocations to share the same locations, and thereby avoiding any run-time overhead for creation and destruction of stack frames. However, such an implementation changes the meaning of recursive function calls. Write down a simple example and explain how its meaning changes under the “Fortran” semantics as stated above, compared with standard semantics found in language like C, Python, Java. Solution:Consider the int fact (int n) function in Lecture 22. When the input is 2, the recursive call fact(1) will change the value of n in both the caller (fact with input 2) and callee (fact with input 1) to 1 since they share the same memory. Hence, while fact(1) still returns 1, the caller (fact with input 2) will return 1 × 1 due to the updated value of n. Problem 3 [6pt] Suppose as a language designer

Show more Read less








Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
September 25, 2023
Number of pages
3
Written in
2023/2024
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
ExamsConnoisseur Self
View profile
Follow You need to be logged in order to follow users or courses
Sold
566
Member since
2 year
Number of followers
343
Documents
1497
Last sold
1 week ago

4.3

67 reviews

5
40
4
11
3
12
2
1
1
3

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions