CSc 11: Introduction to Computing

Name:_____________________ 6 Weeks Exam: Fall 1996

1. Which of the following were "great ideas" of Babbage's Analytical Engine that anticipated modern computers? Write a short explanation for each one, explaining why or why not (20).

_____ It was a general purpose device.

_____ It was compact.

_____ It had a separate "mill" and "store".

_____ It was electro-mechanical.

_____ It had a branch instruction.



2. On the flip side is a Knobby's World program that adds 2 + 3. (24)

a) What problem solving strategy did I probably use to design my solution? Why do you think so?











B) What problem solving strategy would you use to write a Knobby program that adds 1+2? Why?









C) Write a Knobby program that adds 1+2 (on the flip side). Comment your code!

D) In what way could this program be made more general? What construct of the Knobby's World programming language might you use to achieve this generality?



//Knobby adder program, by Robert Barnes and Glenn Blank

//Add 2+3 as two strings of marks: XX XXX => XXXXX

define right as

{ //Turn Knobby's to the right

left left left

}

define getAMark as

{ //read a mark ('X') from next corner to the right

right move read

}

define goToBlank as

{ //go to blank past first two marks

move move

}

define fillItIn as

{ //put a mark in blank between strings of marks

write

}

define fillBlank as

{ //get a mark and put it in the space between strings of marks

getAMark goToBlank fillItIn

}

define goToEnd as

{ //go the end of the second string of three marks

move move move move

}

define getABlank as

{ //read a blank (empty corner) and turn around

read left left

}

define erase as

{ //erase a mark (with a blank)

move write

}

define eraseEnd as

{ //erase mark at the end of concatenated string of marks

goToEnd getABlank erase

}

define goHome as

{ //go back to starting position

move move move move move move right

}

define main as

{ //2+3 (XX XXX) == 5 (XXXXX)

fillBlank //put a mark between the XX and the XXX

eraseEnd //erase the mark at the end of the XXX

goHome //tada!

}



3. Write a C++ definition for pi (about 3.1416). What does this example illustrate about invariants? (8)





4. Suppose you want to use the function sqrt() in a C++ program. (15)

a) Write the code needed to get a C++ compiler to recognize this function.







b) What does this code do--i.e., how does it affect the process of translating your program?







c) How will the linker help to make your program executable?





5. What would the following C++ expression do? Show partial results, for example: (15)

1 + 3 - 2

4

2

a) 2 + 7 % 3 b) sqrt(pow(3,2)) / -2 c) -3.1 * 5 % 2 + 7

















6. Given the following class declaration: (14)

class Circle { //geometric circle

public:

float perimeter(); //computes perimeter in inches

float area(); //computes area of circle in inches

Circle(float rad); //create a Circle with radius in inches

private:

float radius; //radius is half diameter, in inches

}; //Circle

a) Write a snippet of C++ code that creates a Circle object, C, whose radius is 2 inches,

then displays its area on the monitor.





b) What would happen if you tried to write a snippet of code that displayed C's radius? Why?