Fern   davin.50webs.com
New Zealanders making it harder to hate computers

       Main Menu        Research Projects       Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address     Computer Games        Web Design           Java Programming       The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      


S.J.S. tutorial 4: three kinds of loops

SECTION(4, Tutorial 4) m5_question(Study the following code: m4_begin_indent NU()CLASS CLSS(LoopTest) EOL BEGIN EOL PD FUNCTION INT FUNC(powerOf2A)(INT VARI(n)) EOL PD BEGIN EOL PD PD VAR INT VARI(counter) = n; EOL PD PD VAR INT VARI(result) = NUMB(1); EOL PD PD WHILE (counter != NUMB(0)) EOL PD PD BEGIN EOL PD PD PD result = NUMB(2) * result; EOL PD PD PD counter = counter - NUMB(1); EOL PD PD END EOL PD PD RETURN result; EOL PD END EOL EOL PD FUNCTION INT FUNC(powerOf2B)(INT VARI(n)) EOL PD BEGIN EOL PD PD VAR INT VARI(counter) = n; EOL PD PD VAR INT VARI(result) = NUMB(1); EOL PD PD DU EOL PD PD BEGIN EOL PD PD PD result = NUMB(2) * result; EOL PD PD PD counter = counter - NUMB(1); EOL PD PD END WHILE (counter != NUMB(0)); EOL PD PD RETURN result; EOL PD END EOL EOL PD FUNCTION INT FUNC(powerOf2C)(INT VARI(n)) EOL PD BEGIN EOL PD PD VAR INT VARI(result); EOL PD PD VAR INT VARI(counter); EOL PD PD FUR (counter = n, result = NUMB(1); counter != NUMB(0); counter = counter - NUMB(1)) EOL PD PD BEGIN EOL PD PD PD result = NUMB(2) * result; EOL PD PD END EOL PD PD RETURN result; EOL PD END EOL EOL PD COMM( EOL PD /** EOL PD * Prints a row of stars of a given length. EOL PD */) EOL PD FUNCTION VOID FUNC(printLineC)(INT VARI(length)) EOL PD BEGIN EOL PD PD FUR (VAR INT VARI(i) = NUMB(0); LT(i,length); i=i+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD SYSTEM_OUT_PRINT(STRI("HASH")); EOL PD PD END EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END EOL EOL PD BEGIN_MAIN EOL PD PD COMM(// For question 4.1 add some code here...) EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 4.1:) To the MAIN function add some code to call the functions TT(powerOf2A), TT(powerOf2B) and TT(powerOf2C) to verify that they all return the same result. To inspect the result you will need to apply the SYSTEM_OUT_PRINTLN statement to the values returned by those functions.) m5_question(BO(Question 4.2:) There is a bug in the TT(powerOf2B) method because it does not behave correctly in the case when n is zero. Put an EM(if) statement at the top of this method to make it handle the case of zero properly.) m5_question(BO(Question 4.3:) By copying the pattern of TT(powerOf2A), TT(powerOf2B) and TT(powerOf2C), write methods TT(printLineA) and TT(printLineB) that work identically to the method TT(printLineC), except that they use EM(while) loops and EM(do) loops, respectively. Add some code to the MAIN function to test them out. ) m5_question(BO(Question 4.4:) Based on the previous three questions, is there a best looping construct? Or does it depend on what the looping construct is going to be used for?)
Back to Main Menu
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Programming | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |

Please report any broken links to the Web-person
Last modified: Tue Mar 25 12:27:09 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.