UIT2201: Tutorial Set 3 (Spring 2013)
(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-PP1: 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-PP1: 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.
(b) Yes. Output will be 0. Program will execute, will bypass the loop altogether.
(c) Add if statement and add A[k] only if (A[k] < 0).
(d) Use variable COUNT (initialize to 0), increment COUNT if (A[k] < 0).
(e) "Sum_SF <-- SumSF + A[k]" changed to "Sum_SF <-- SumSF + A[k]*A[k]"

T3-D3: [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 <= b) do
    SumSF <-- SumSF + a;    
    Count <-- Count + 1;
  endwhile
  Answer <-- SumSF;
end; 
(Dominant operation is shown in Bold-Red.)
Complexity: O(b) -- linear in b.

T3-Q1: (10 points) [Exponentiation-by-Repeated-Multiplication]
(a) One way to do exponentiation is by repeated multiplication. For example, 85 = 8 * 8 * 8 * 8 * 8.
Write a simple Scratch program that reads in two positive numbers a and b and computes ab using this technique.
(b) Now, give the pseudo-code for your algorithm for computing ab, for two positive numbers a and b using this technique.

T3-Q2: (10 points) [Getting the Grade] --- SOLUTION SKETCH
(a) Write a Scratch program that will read in a mark (an integer) that a student gets for UIT2201, and outputs both the mark and the grade.
Assume, for simplicity only, that the letter grades are A, B, C, D, F and that the cutoffs for are 80, 70, 60, 50. Namely, A is 80-100, B is 70-79, C is 60-69, D is 50-59 and F is 0-49.
(b) Now, extend your Scratch program to process a list of marks entered by the user, instead of just one mark. The user will enter a "-1" to indicate that there are no more marks left.

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

T3-Q3: (10 points) [Cat going in a spiral]
See your Scratch programs in the Workbin.

T3-Q4: (10 points) [Other algorithmic frameworks] -- YOUR SOLUTIONS

What you all came up with: (some more appropriate than others)
 Playing song on a guitar, playing a piece of music on a piano;
 Driving directions (from A to B), Driving directions via Google Maps,
   Direction from A to B, Alice riding a bicycle from A to B;
 Search a particular book in NUS Library,
   Procedure to Find a Lost Item;
 Building a Lego Model kit,
  How to use a fire extinguisher during an emergency,
   Game tutorial/walkthrough;
 Singapore Education System (going from kindergarten to university);
 Spring cleaning of your home;
  Watercolour painting;
   Jogging,
    Sort colored cards into piles of same color (?); 


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