Download Ex1Fall96

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Big O notation wikipedia , lookup

Law of large numbers wikipedia , lookup

Addition wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Halting problem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Computer Science 101
Survey of Computer Science
Exam 1
Fall 2008
1. (40 points) Complete the following:
a. An informal definition of the term algorithm is step by step procedure for solving a
general class of problems
b. The term
effectively computable
means that each intended computing
agent has the ability to carry out each indicated operation.
c. Using the set of pseudocode statements covered in class, give a statement that will
give the variable subtotal the value obtained by multiplying the value of the
variable itemCost times the value of the variable numOrdered.
Set subtotal to itemCost x numOrdered
d. Repeat part c, but give a Python statement.
subtotal = itemCost * numOrdered
e. Write a pseudocode statement that assigns to the variable largest, the larger of the
values of the variables first and second.
if first>second then
Set largest to first
else
Set largest to second
f. Repeat part g, but with Python.
if first>second :
largest = first
else:
largest = second
g. Give the value of the following expressions as computed in Python:
42 % 5
2
42 - 5
37
42/5
8
42*5
210
h. In the context of a computer program, tell what is meant by:
runtime error: an error that occurs during program execution causing program to
terminate
logic error:
an error that causes program to give incorrect output
syntax error:
an error in the grammatical rules – statement not legal in language
i. In Python, the
and the
while
if
j. In Python, the symbol(s)
statement is used for iterative control flow,
statement is used for conditional control flow.
==
is used to compare two values for equality
and
=
is used to assign a value to a variable.
k. Both selection sort and bubble sort make passes through the list. The main
difference between the two algorithms is that:
selection makes on swap per
pass (largest unsorted with last unsorted) while bubble makes many swaps per
pass (adjacent elements that are out of order)
2 .(20 points) In this problem you are to assume the role of the “computing agent”.
Consider the following algorithm given in pseudocode. In the 5th step, we mean that
we should divide the whole number 2 into the whole number N and R should become
the remainder that is left after this division. For example, if N=17 then the quotient of
N divided by 2 is 8 with a remainder of 1; so R would become 1.
Set twoMult to 0
Set threeMult to 0
Get N
While N>0 do
Set R to the remainder of N divided by 2
If R = 0 then
Set twoMult to twoMult + N
Else
Set R to the remainder of N divided by 3
If R = 0 then
Set threeMult to threeMult + N
Get N
End-of-loop
Print twoMult, threeMult
Stop
a. Step through the algorithm carefully, assuming that the user inputs the values 6, 5,
15, 4 and 0, in that order, as inputs are needed. Keep track of the values of all of the
variables as you do this. Tell what will be printed at step 13. (Show work for
possible partial credit).
twoMult
threeMult
N
R
0
0
6
0
6
5
1
2
15
1
0
15
4
0
10
0
Output: 10, 15
b. Convert the pseudocode algorithm of part a) into Python.
Set twoMult to 0
Set threeMult to 0
Get N
While N>0 do
Set R to the remainder of N divided by 2
If R = 0 then
Set twoMult to twoMult + N
Else
Set R to the remainder of N divided by 3
If R = 0 then
Set threeMult to threeMult + N
Get N
End-of-loop
Print twoMult, threeMult
Stop
twoMult = 0
threeMult = 0
N = input(“Enter number: “)
while N>0 :
R=N%2
if R == 0 :
twoMult = twoMult + N
else:
R=N%3
if R == 0:
threeMult = threeMult + N
N = input(“Enter number: “)
print twoMult, threeMult
3. (15 points) Using the pseudocode covered in class or Python-like statements
a. Write an algorithm that has the user enter a number and then displays the square of
the number.
Get num
Set num to num times num
Print num
b. Write an algorithm (using a loop) that has the user input 500 numbers (both
positive and negative) and then gives the sum of the positive numbers . Note: You
should not use lists or lots of variables.
Set Sum to 0
Set Ct to 0
While Ct < 500 do
Get Num
If Num > 0 then
Set Sum to Sum + Num
Set Ct to Ct + 1
End-of-loop
Print Sum
4. (10 points) For this problem, assume that scores is a Python list that already has 200
numbers in it.
a. Write a Python statement to set the value of the third number in the list to be the
sum of the first two numbers in the list.
scores[2] = scores[0] + scores[1]
b. Suppose a mistake has been determined in grading the exam represented in the list
scores. Write Python code to add 5 to each of numbers in the list.
i=0
while i<200:
scores[i] = scores[i] + 5
i = i+1
5. (15 points) a. Show what the following list of data would look like after the first pass
of the Bubble Sort algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
19
8
13
7
10
9
29
32
22
10
28
12
17
1
2
3
4
5
6
7
8
9
10
11
12
8
13
10
9
29
22
10
28
12
17
Pass 1:
19
32
b. Beginning with the original data, show what the list would like after the first pass of
the Selection Sort algorithm.
Pass 1:
1
2
3
4
19
8
13
10
5
9
6
7
8
9
10
11
29
17
22
10
28
12
12
32
c. Beginning with the original data, indicate which element would be chosen for the
Quick Sort pivot element using the scheme covered in the course, and show exactly
what the list would look like after the first partitioning pass in executing the Quick
Sort algorithm.
1
2
19
8
3
13
7
Pivot element: 13
4
10
5
9
6
7
8
9
10
11
29
32
22
10
28
12
6
7
8
9
10
11
13
32
22
29
28
19
12
17
After partitioning:
1
2
3
4
10
8
12
10
5
9
12
17