Docsity

Solutions for Quiz 6 - Programming and Problem Solving | COSC 235, Quizzes of Computer Science

Toggle sidebar
bg1
Name:&_____________________________& & Pledged:&_____________________________&
&
Quiz%6%
Oc tob er( 17,(2008(
1. Indicate&for&each&term&in&the&right&column&an&example&of&the&concept&in&the&program&in&the&left&column.&
def fib(n):
"Return the nth Fibonacci number"
a, b = 0, 1
for k in range(n):
a, b = b, a + b
return b
def main():
for k in range(10):
print fib(k)
main()
function&definition&
formal&parameter&
actual&parameter&(argument)&
formal&parameter&list&
function&call&
local&variable&
docstring&
&
2. Show&the&output&of&the&following&program.#
def doubleSquare(x):
x = (2 * x) ** 2
y = 6
doubleSquare(y)
print y
3. Define&a&function&to&meet&this&specification:&
midpoint(p1, p2) p1 and p2 are point objects. Return the point that is
midway between p1 and p2
&
The&midpoint&of&two&points&
(x1,y1)&and&(x2,y2)&is&
€
x1+x2
2,y1+y2
2
&

Partial preview of the text

Download Solutions for Quiz 6 - Programming and Problem Solving | COSC 235 and more Quizzes Computer Science in PDF only on Docsity!

Name: _____________________________ Pledged: _____________________________

Quiz 6

October 17 , 2008

  1. Indicate for each term in the right column an example of the concept in the program in the left column. def fib(n): "Return the nth Fibonacci number" a, b = 0, 1 for k in range(n): a, b = b, a + b return b def main(): for k in range(10): print fib(k) main() function definition formal parameter actual parameter ( argument ) formal parameter list function call local variable docstring 2. Show the output of the following program. def doubleSquare(x): x = (2 * x) ** 2 y = 6 doubleSquare(y) print y
  2. Define a function to meet this specification: midpoint(p1, p2) p1 and p2 are point objects. Return the point that is midway between p1 and p The midpoint of two points (x 1 ,y 1 ) and (x 2 ,y 2 ) is

x 1 + x 2

y 1 + y 2