Due September 10, 2015 68 Points Possible



Download 11.74 Kb.
Date conversion27.01.2017
Size11.74 Kb.
Programming Languages

Homework #1

Due September 10, 2015

68 Points Possible




  1. The following Pascal function computes the factorial of an integer. A) Convert this function to C++, and then B) rewrite the function into imperative style (i.e. using variables and eliminating recursion.) (10 pts)


function fact (n : integer) : integer;

begin

if n <= 1 then

fact := 1

else

fact := n * fact(n-1);

end;
2. The following is a Pascal function that computes the number of decimal digits in an integer. A) Convert this function to C++ and then B) rewrite this function in functional style (looping is handled by using recursion). (10 pts)

function numdigits (x : integer) : integer;

var t, n : integer;

begin



n := 1;

t := x;

while t >= 10 do

begin

n := n + 1;

t := t div 10;

end;



numdigits := n;

end;

3. Most programming languages now use the free format pioneered by Algol60: statements can begin anywhere and end not with the end of line of text but with an explicit end symbol, such as a semicolon. By contrast, Fortran and a few other languages used the fixed format: statements must begin in a particular column and are ended by the physical end of line, unless a continuation marks are provided. Discuss the effect of fixed format and free format in terms of A) Readability B) Writability and C) Security. (12 pts)

4. A possible additional language design principle is learnability: the ability of programmers to learn to use a language quickly and effectively. Describe ways that a language designer could use to improve the learnability of a language. Do any language principles support learnability? Do any hurt the learnability of a language? Explain each answer. (6 pts)
5) In Modula-2 and Ada, there is a LOOP..EXIT construct and in PL/I, there is a similar LOOP..BREAK construct. No such construct exists in Pascal. Why do you think Wirth chose not to include one? Is this an example of any design principle (explain)!. How about C++ - does it allow a similar construct ? (7 pts)
6. Two contrasting viewpoints on the declaration of comments in a programming language are represented by Ada and Modula-2. In Ada, comments begin with adjacent hyphens and end with the end of the line.

-- this is an Ada comment

In Modula-2, comments begin with “(*” and proceed to a matching “*)”. Comments can be nested.



(* this is a (* Modula-2 *) comment *)

Compare these two comment features with respect to readability, writability, and reliability. How does C++ differ in the way it handles comments? (8 pts)


7. The principle of locality maintains that variable declarations should come as close as possible to their use in a program. What language design features might promote or discourage this principle? How well does C++ uphold this principle? Do you agree that this principle is important? Why or why not? (6 pts)
8. When producing object code, optimization involves rearranging and changing operations to make the program run faster. One of these techniques is called folding, the process of computing at compile time arithmetic operations that are known. Suppose our source code includes the following sequence of statements: (9 pts)

I = 1 + 1; can be optimized to :: I := 2;

and


I = 3;

b = 6.2 + I;

can be optimized to :: b = 9.2;



Optimize the following sequences of statements (note A, B and C are independent of each other):
A) x = 10; y = x*2; z = sqr(x) – (x + y);
B) x = 10; y = x + t; t = sqr(x) – (x + y);
C) switch I

{

case 1 : cout << I * 2;

case 2 : cout << I * 3;

case 3 : cout << I * 4;

default : cout << I;

}


The database is protected by copyright ©ininet.org 2016
send message

    Main page