Você está na página 1de 4

CSE 101

Computer Engineering Concepts & Algorithms ( Fall 2006 )


Homework #4 Solutions
- Algorithmic Thinking
Sample Solutions

1. Write an algorithm for adding two n-digit numbers discussed in class so that it does
not print out nonsignificant leading zeroes; that is 149+029 would give answer 178
rather than 0178.
Solution:
Variables: m, list a0 ..an-1,list b0 . bn-1 ,list c0 cn-1 cn,carry,i, skipped
0. Get values for n, an-1 a0 and bn-1 b0
1. Set the value of carry to 0.
2. Set the value of i to 0.
3. Repeat steps 4-6 until i > n-1
4.Set the value of ci to ai + bi + carry
5.if ci 10 then
subtract 10 from ci and set the value of carry to 1
else set the value of carry to 0
6.Add 1 to i
Set
the value of cn to carry
7.
8. Set the value of i to n
9. Set the value of skipped to false
10. Repeat steps 10-13 until i<1
11. f ci is not 0 and skipped is false then
Set skipped to true
12. If skipped is true then
Print ci
13. Set i to i-1
14. Print c0

2. Below are two shampooing algorithms. Which do you think is a better general-purpose
solution? Why? (Hint: What if you wanted to wash your hair 1000 times?)
Algorithm 1:
1. wet hair
2. set value of washCount to 0
3. repeat steps 4 through 6 until the value of washcount equals 2
4. lather hair
5. rinse hair
6. add 1 to the value of washCount
7. stop
Algorithm 2:
1. wet hair
2. lather hair
3. rinse hair
4. lather hair
5. rinse hair
5. stop

Solution: First one is a better general-purpose solution, since we do not have to copy the
parts of the algorithms to wash our hair more than once. The first algorithm will not
change but only a constant value at step 3.
3. Here is Euclid's 2300-year-old algorithm for finding teh greatest common divisor of
two positive integers I and J.
1

Euclid's algorithm:
1. Get two positive integers as input. Call the larger value I and
the smaller value J.
2. Divide I by J, and call the remainder R.
3. If R is not 0, then reset I to the value of J, reset J to the
value of R, and go back to step 2.
4. Print out the answer, which is the value of J.
5. Stop.

a) Go through this algorithm using the input values 20 and 32. After each step of the
algorithm is completed, give the values of I, J and R. Determine the final output of the
algorithm.
b) Does the algorithm work correctly when the two inputs are 0 and 32? Describe
exactly what happens, and modify the algorithm so that it gives an appropriate error
message.
Solution:
a)
a. I=32 and J=20
b. The remainder of 32/20 is R=12
c. R is not 0, then I=J=20 and J=R=12, and go to step 2
Step 2 again. The remainder of 20/12 is R=8
Step 3 again. R is not 0, then I=J=12 and J=R=8, and go to step 2
Step 2 again. The remainder of 12/8 is R=4
Step 3 again. R is not 0, then I=J=8 and J=R=4, go to step 2
Step 2 again. The remainder of 8/4 is R=0
d. The value of J is the answer and it is 4
e. Stop
b) The algorithm does not work correctly when the two inputs are 0 and 32. The value
of J becomes 0, and DIVISION BY ZERO error occurs at step 2. To solve this
problem, we have to put additional step between the steps 1 and 2.
Step 1.5. If J=0 then print J value cannot be zero.
Step 1.6. Go to step 1

4. A salesperson wants to visit 25 cities while minimizing the total number of miles she
has to drive. Because she has studied computer science, she decides to design an
algorithm to determine the optimal order in which to visit the cities to (1) keep her
driving distance to a minimum and (2) visit each city exactly once. The algorithm she
devised is the following:
The computer would first list all possible ways to visit the 25
cities and then, for each one, determine the total mileage associated
with that particular ordering (assume the computer has access to a
roadmap that provides the distances between all cities). After
determining the total mileage for each possible trip, the computer
would search for the ordering with the minimum mileage and print out
the list of cities on the optimal route, that is, the order in which
the salesperson should visit her destinations.

If a computer could analyze 10,000,000 separate paths per second, how long would it
take the computer to determine the optimal route for visiting these 25 cities? On the
basis of your answer, do you think this is a feasible algorithm?
Solution: If we have a fixed initial city then we have 24! possible paths. If our computer
can analyze 10M separate paths per second, it would take 24!/10M=6.2E+16 sec.=2.7E+9
years. Therefore, the algorithm is not feasible. We need to approximate the algorithm
rather than to find the optimal solution.
2

5. One way to do multiplication is by repeated addition. For example, 47 x 25 can be


evaluated as 47 + 47+47+...+47 (25 times). Sketch out an algorithm for multiplying
two positive numbers a and b by using this technique.
Solution:
1.
2.
3.
4.

get a and b
set mul to 0
set counter to a
repeat until counter < 1
5.set mul to mul+b
6.set counter to counter 1
7. print mul
8. stop

6. Write pseudocode instructions to carry out each of the following operations:


a) Determine the area of a triangle given values for the base b and the height h.
Solution:
1. get b, h
2. set area to b*h/2
3. print area
4. stop

b) Compute the interest earned in one year given the starting account balance B
and the annual interest rate I and assuming simple interest, that is, no
compounding. Also determine the final balance at the end of the year.
Solution:
1. get B, I
2. set IE to B*I/100
3. set FB to B+IE
4. print IE, FB

5. stop
c) Determine the flying time between two cities given the mileage M between
them and the average speed of the airplane.
Solution:
1. get M, Averagespeed
2. set FlyingTime to M/Averagespeed
3. print FlyingTime
4. stop

7. Write an algorithm that inputs 4 numbers corresponding to the scores received on


three semester tests and a final examination. Your algorithm should compute and
display the average of all four tests, weighting the final exam twice as heavily as a
regular test.
Solution:
1. get M1, M2, M3, F
2. set Average (M1+M2+M3+2*F)/5
3. print Average
4. stop

8. Write an algorithm that inputs the length and width of a carpet in feet and the price in
dollar per square yard. The algorithm should print out the total cost of the carpet,
including a 6% sales tax.
Solution:
1. get
2. set
3. set
4. set

L, W, P
Total to (L/3)*(W/3)*P
Tax to Total*0.06
Total to Total+Tax

5. Print Total
9. Write an if-then-else instruction to do each of the following:
a) Compute and display the value x/y if the value of y is not 0. Otherwise, display
the massage "Unable to perform division!".
Solution:
1. get x, y
2. if y = 0
print
else
set z
print
3. Stop

then
Unable to perform division!
to x/y
z

b) Compute the area and circumference of a circle given the radius r if the radius
is greater than or equal to 1.0. Otherwise you should compute only the
circumference.
Solution:
1. get r
2. set c 2*3.14*r
3. if r >= 1.0 then
set a 3.14*r*r
print Area ,a, Circumference ,c
else
print Circumference ,c
4. Stop

Você também pode gostar