UIT2201: Tutorial Set 3 (Spring 2016)
(Solution Sketch to Selected Problems)
** NOT TO BE GIVEN TO FUTURE UIT2201 STUDENTS **

Solution sketches are provided as guidelines only. Do not think of them as *model answers*. As you have noticed from our tutorial discussions, there can be many answers and approaches. So keep an open mind. (More importantly, DO NOT CIRCULATE these solution sketches.)

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; 
(Dominant operation is shown in Bold-Red.)
Complexity: O(a) -- linear in a.

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.

Algorithm Grade (aMark);
(* Compute the letter Grade,
   given aMark *)
begin
  if (aMark >= 80) 
    then Grade <-- "A"
    else if (aMark >= 60) 
      then Grade <-- "B"
      else if (aMark >= 40) 
        then Grade <-- "C"
        else Grade <-- "D"
       endif;
     endif;
  endif;
  return Grade
end; 
(Use 3 nested if-statements.)
Algorithm Grade (aMark);
(* Compute the letter Grade, given aMark *)
begin
  if (aMark >= 80) then Grade <-- "A" endif;
  if (aMark >= 60) and (aMark < 80) then Grade <-- "B" endif;
  if (aMark >= 40) and (aMark < 60) then Grade <-- "C" endif;
  if (aMark < 40) then Grade <-- "D" endif;
  return Grade
end; 
(Use 4 independent if-statements. Here each if-stmt is executed once.)


UIT2201: CS & IT Revolution; (Spring 2016); A/P Leong HW