SOLUTION SKETCH:
T3-PP1: Problem 1 (page 45) of [SG]; (in more verbose pseudo-code) (in concise pseudo-code) read in the values of x, y, z; read (x, y, z); assign the value of (x + y + z)/3 to average; average <-- (x + y + z)/3; print out the value of average; print average; T3-PP2: Problem 2 (page 45) of [SG]; (in more verbose pseudo-code) (in concise pseudo-code) Assign the value 3.1415926 to PI; PI <-- 3.1415926; read in the value of radius r; read (r); (* read in radius *) print the value of 2*PI*r and PI*r*r; print 2*PI*r, PI*r*r; T3-PP3: Problem 3 (page 45) of [SG]; (in more verbose pseudo-code) (in concise pseudo-code) read in AmtUsed; (* in kWh *) read (AmtUsed); (* un kWh *) read in Rate; (* cost per kWh *) read (Rate); (* cost per kWh *) Let Cost be AmtUsed * Rate * 1.07; Cost <-- AmtUsed * Rate * 1.07; print Cost print Cost
T3-D1: [Array-Sum] --- SOLUTION SKETCH
(a) Yes. Of course!
(b) Yes. Program will execute, will bypass the loop altogether.
Output will be 0. BUT that is "dangerous", it is NOT the correct answer. An error message and a warning might have been a better response!! Can you change the algorithm to do that too?
(c) Add an "if statement" so that we "add A[k] to Sum_SF only if (A[k] < 0)".
(d) Use variable COUNT (initialize to 0), increment only COUNT if (A[k] < 0).
T3-D2: [Sum-of-Squares] --- SOLUTION SKETCH
In Algorithm Array-Sum, "Sum_SF <-- SumSF + A[k]" changed to "Sum_SF <-- SumSF + A[k]*A[k]"
(See, it is easy to modify the template "linear search" Array-Sum algorithm to do other useful things.)
T3-D4: (10 points) [Exponentiation-by-Repeated-Multiplication]
DIY
T3-Q1: [Multiplication-by-Repeated-Addition] --- SOLUTION SKETCH
Algorithm Product(a,b); (* Computes a*b, via repeated addition *) begin if (a > b) then swap(a,b); SumSF <-- 0; Count <-- 1; while (Count <= a) do SumSF <-- SumSF + b; Count <-- Count + 1; endwhile Answer <-- SumSF; end; |
T3-Q2: (10 points) [Getting the Grade] --- SOLUTION SKETCH
(a), (b) Instead of giving a Scratch program, I give the algorithm.
I will leave it to you to convert the algorithm into a Scratch program.
This is also an important skillfor you to acquire.
|
|