UIT2201: Tutorial Set 2 (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.)

T2-Q2: (Algorithm for Odd-Sum) --- SOLUTION SKETCH
(* Algorithm to compute Odd-Sum = 101 + 103 + 105 + ... + 2011 + 2013. *)

  Algorithm Odd-Sum-101-to-2013): 
  1. begin
  2.   SumSoFar <--0;
  3.   k <-- 101;
  4.   while (k <= 2013) do
  5.     SumSoFar <-- SumSoFar + k;
  6.     k <-- k + 2;     // increment by 2
  7.   EndWhile
  8.   Even-Sum <-- SumSoFar;
  9. end; 
   
  Algorithm Odd-Sum-101-to-2013): (with repeat)
  1. begin
  2.   SumSoFar <--0;
  3.   k <-- 101;
  4.   Repeat if (k <= 2013)
  5.     SumSoFar <-- SumSoFar + k;
  6.     k <-- k + 2;     // increment by 2
  7.   End-Repeat
  8.   Even-Sum <-- SumSoFar;
  9. end; 


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