1 / 49

Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences

Control Structure. Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah.uitm.edu.my (b): https://sitinur151.wordpress.com. Control Structure. Previously… Iteration / Looping Loop Control Structure

modesta
Download Presentation

Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structure Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah(e): sitinurbaya@kedah.uitm.edu.my(b): https://sitinur151.wordpress.com

  2. Control Structure Previously… • Iteration / Looping • Loop Control Structure • Basic Algorithm Using Looping • Calculate the total or sum • Calculate the average • Find maximum value and minimum value • Count • Single Looping • Nested Looping • Pseudo code & Flowchart • Determine Output / Tracing Table Previously… • Sequential • Creating a set of instructions to complete a task. • Selection • A decision within a computer program when the program decides to move on based on the results of an event.

  3. Control Structure Today… • Manipulate a series • Sentinel Loop • Infinite Loop • Nested Loop  Quick Revision • Draw Diagram Using Nested Loop

  4. Iteration Control Structure Manipulate a series

  5. Problem Solving: Manipulate a series Calculate the total of the following series: 3 + 6 + 9 + 12 + 15 + … until 30 times {consistent sequence} = Problem Definition:

  6. Problem Solving: Manipulate a series Calculate the total of the following series: 3 + 6 + 9 + 12 + 15 + … until 30 times {consistent sequence} Start total =0 number =3 counter = 1 While ( counter <= 30 ) total =total + number number =number + 3 counter = counter + 1 endWhile Display total End

  7. Problem Solving: Manipulate a series Calculate the total of the following series: 1 + 6 + 9 + 12 + 15 + … until 30 times {a pattern of sequence} Problem Definition:

  8. Problem Solving: Manipulate a series Calculate the total of the following series: 1 + 6 + 9 + 12 + 15 + … until 30 times {a pattern of sequence} Start total = 1 number = 6 counter = 2 While ( counter <= 30 ) total =total + number number =number + 3 counter = counter + 1 endWhile Display total End

  9. Problem Solving: Manipulate a series You are given the following series. Calculate H where H = F + G F = 1 + 7 + 13 + 19 + 25 + 31 + 37 +… until 10 times G = 4 + 8 + 12 + 16 + 20 + … until 15 times H = F + G {a missing formula} Problem Definition:

  10. Problem Solving: Manipulate a series Start F = 0 number = 1 counter = 1 While ( counter <= 10 ) F = F + number number =number + 6 counter = counter + 1 endWhile G = 0 number =1 counter = 1 While ( counter <= 15 ) G = G + number number =number + 4 counter = counter + 1 endWhile H = F + G Display H End

  11. Problem Solving: Manipulate a series Calculate the factorial of a positive number. Factorial for n! as follows: Factorial = 1 x2 x 3 x … x n – 1 x n {given a formula} Problem Definition:

  12. Problem Solving: Manipulate a series Start factorial = 1 Read n number = 1 While ( number <= n ) factorial = factorial * number number = number + 1 endWhile Display factorial End

  13. Problem Solving: Manipulate a series Flow Chart ?

  14. Problem Solving: Manipulate a series

  15. Start total = 1 number = 4 counter = 2 add = 3 While ( counter <= 10 ) total =total + number add =add+2 number =number + add counter = counter + 1 endWhile Display total End 4->9 =5 + 2 9->16 = 7 + 2 16 -> 25 = 9 + 2 25

  16. tart • i=2 • num=2 •        numb=5 • total=0 • While(i<=15) • total=(num/numb)+total • num=num+2 • numb=numb+5 • i++ • EndWhile • Display total • End

  17. Start total=1 number=1 count=1 while(count<20) number=number+count total=total+number count++ Endwhile display total End

  18. start read num1,num2,num3 multable=num1 firstnum=num2 stop=num3 counter=2 Total=0 While (counter<stop) multable=multable+1 Total=firstnum*multable counter=counter+1 Display firstnum, “x”, multable, “=“, Total Display newline Endwhile end

  19. Iteration Control Structure Sentinel Loop

  20. Iteration: Sentinel Loop • scenariowheretheexactnumber of repetitionisunknownbuttheremust be a conditionto stop therepetition • thesyntaxis: • all statements in between while .. endWhilewill be executed when the condition is TRUE • the repetition is stop when the condition is FALSE. Start initial value of a loop while ( condition ) statement(s) to be repeated statement to update the condition endWhile End

  21. Iteration: Sentinel Loop • Ask the user to enter a group of numbers. The last number is 999. Display all entered numbers. • Input mark of students. Display mark which is less than 50. Stop the process when the user enters invalid mark. Start Read number while ( number is =! 999 ) Display number Read number endWhile End Start Read mark while ( mark => 0.0 AND mark <= 100 ) If ( mark < 50 ) Display mark EndIF Read mark endWhile End

  22. Tracing Algorithm: Sentinel Loop • Given the following algorithm; Start sentinel = -1 count = 0 totalSquare=0 Read x while (x =! sentinel) totalSquare=totalSquare+ x2 count = count + 1 Read x endWhile Display “ total = “, totalSquare Display nextline Display “ count = “ , count End QUESTION What is the value of the sentinel value used? sentinel =-1 What is the body of repeat statements? total-square =total-square + x2 count = count + 1 Read x What is the value of total-square when the user enters the following data ? -1 3 9 5 total = 115 count = 3

  23. Tracing Algorithm: Sentinel Loop Start i = 1 XX = 0 Display “enter an integer : “ Read number Display “ MYSTERY MESSAGE ” , newline while ( i<= number ) If ( number % i == 0) XX = XX + 1 ; Display i , “ ” EndIF i = i + 1 endWhile diplay newline Display “ There are ” , XX , “ MYSTERY MESSAGE FOR ”, number End • QUESTION • Trace the pseudo code for the following data: • i- 9  output? • ii- 8  output? • Give a meaningful name for XX. • Replace MYSTERY MESSAGE FOR with a suitable words. • Given the following algorithm;

  24. Problem Solving: Sentinel Loop • Find the best mark for the first test in a class. Assume that the number of student is unknown. The process stops when the user enters an invalid mark. • Problem Defination In this problem, the user has to enter the student’s test mark many times. The process stops when the user enters invalid mark. Normally the valid mark is in the range of 0.0 to 100.0.

  25. Problem Solving: Sentinel Loop Start Display “ Enter test mark for the first student“ Read testMark max =0 while ( testMark => 0.0 AND testMark <= 100.0 ) If (testMark> max ) max =testMark EndIF Display “ Enter test mark for next student “ Read testMark endWhile Display “The highest mark “, max End

  26. Problem Solving: Sentinel Loop • Ask theusertoenter a set of numbers. Calculatehowmanynumberis divisible by 5. Theprocessstopswhentheuserenters 999. (pg 292) • Calculatethe total of thefollowing series and counthowmanynumbersinvolved. (pg 296) 3 + 6 + 9 + 12 + …………………… + 312 + 315 • Counthowmanynumberswhichis divisible by 7 in between 1 and 100. (pg297) • Calculatethemark and the grade forstudents. Themarkistheaverage of twohighest test markbetweenthree test marks. Theprocessstopswhenthere are no more data toprocess. Use thefollowingtableto determine the grade. (pg300) • Calculatethemark and the grade forstudents. Themarkistheaverage of twohighest test markbetweenthree test marks. Theprocessstopswhenthere are no more data toprocess. Use thefollowingtableto determine the grade. (pg 300) • Display the following menu. choice Operation + add 2 numbers * multiply 3 numbers - difference of 2 numbers x exit Ask the user to enter a choice. Repeat until the user enters ‘x’ as a choice.(pg303)

  27. Iteration Control Structure Infinite Loop

  28. Iteration: Infinite Loop • non stop loop • the repetition keeps running, it is never stop • thesyntaxis: • The above algorithm is missing the statement to increase the value of the variable number. It makes the condition (number ≤ 100) always true because the value of the variable number remains with 7. It makes the algorithm runs forever. Start count=0 number=7 while(number<= 100) count = count+ 1 endWhile Displaycount End

  29. Iteration Control Structure Nested Loop

  30. Iteration: Nested Loop • inside a loop there is another loop ; inner loop and outer loop • The execution of this type of structure is, the computer will finish the inner loop for every iteration of outer loop Start count 0 number 7 while(number ≤ 100) count count + 1 endWhile Displaycount End

  31. Iteration Control Structure Draw a Diagram Using Nested Loop

  32. Iteration: Draw a Diagram Using Nested Loop • Display the following diagram: &&&&&& &&&&&& &&&&&& &&&&&& 1 2 3 4 Start row = 1 while (row < = 4) row = row + 1 endWhile End EndIF row = 4 col = 1 while (col <= 6) Display “&” col = col +1 EndWhile Display newline 1 2 3 4 5 6 col = 6

  33. Start line = 1 While (line <= 5 ) If (line <= 3) Else Display newline EndIF line++ EndWhile End EndIF Iteration: Draw a Diagram Using Nested Loop countSymbol = 1 While (countSymbol <= 5) Display “$” countSymbol++ EndWhile • Display newline • Display the following diagram: $$$$$ $$$$$ $$$$$ ##### ##### 1 2 3 4 5 line = 5 line <= 3 countSymbol = 1 While (countSymbol <= 5) Display “#” • countSymbol++ EndWhile else 1 2 3 4 5 cauntSymbol = 5

  34. Start • row = 1 • while (row < = 7) • If ( (row = =1) OR (row = =7) ) Else EndIF • Display newline row ++ EndWhile End EndIF Iteration: Draw a Diagram Using Nested Loop column = 1 while (column <= 6) Display “$” column++ endWhile • Display the following diagram: $$$$$$ $ $ $ $ $ $ $ $ $ $ $$$$$$ 1 2 3 4 5 6 • column = 1 • while (column <= 6) • If ( (column = =1) OR (column = = 6) ) • Display “$” • Else • Display “ ” • EndIF • column++ • endWhile row = 6 1 2 3 4 5 6 column = 6

  35. Iteration: Draw a Diagram Using Nested Loop Flow Chart ?

  36. Iteration: Draw a Diagram Using Nested Loop • Display the following diagram. @ @@ @@@ @@@@ @@@@@ • Analysis: We can assume that the diagram is a square, consisting of five rows and five columns. The diagram consists of @ symbols and white space. We can solve the problem using a matrix concept. 1 2 3 4 5 row = 5 1 2 3 4 5 column = 6

  37. Iteration: Draw a Diagram Using Nested Loop •  In mathematics and computer science, a matrix is a set of numbers laid out in tabular form (in rows and columns). The matrix 5 x 5 means that the matrix consists of five rows and five columns. Each element of the matrix has the value of row and column as follows: • @ • @@ • @@@ • @@@@ • @@@@@

  38. Iteration: Draw a Diagram Using Nested Loop

  39. Iteration: Draw a Diagram Using Nested Loop • When we compare the value of rows and columns to determine where the @ symbol is located and where is the location of the white space, we found that @ symbols are at the element where the value of row is greater or equal to column ( row ≥ column).

  40. Iteration: Draw a Diagram Using Nested Loop Start row = 1 while (row < = 5) row ++ endWhile End EndIF • Display the following diagram. @ @@ @@@ @@@@ @@@@@ 1 2 3 4 5 column = 1 while (column <= 5) If (row => column) Display “@” Else Display “ “ EndIF column++ EndWhile row = 5 1 2 3 4 5 column = 6

  41. Iteration: Draw a Diagram Using Nested Loop Flow Chart ?

  42. Iteration: Draw a Diagram Using Nested Loop • Symbols $ are at the following positions : (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (2,1) (2,6) (3,1) (3,6) (4,1) (4,6) (5,1) (5,6) (6,1) (6,6) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) • Analysis: - $ symbols are at the positions: - all columns in the first row : ( when row == 1 ) - all columns in the seventh row : ( when row == 7 ) - all rows at the first column : ( when column == 1 ) - all rows at the sixth column : ( when column == 6 ) • $ $ $ $ $ $ • $ $ • $ $ • $ $ • $ $ • $ $ • $ $ $ $ $ $ • OUTPUT

  43. Iteration: Draw a Diagram Using Nested Loop Start row = 1 while (row < = 7) row ++ endWhile End EndIF • Display the following diagram. • $ $ $ $ $ $ • $ $ • $ $ • $ $ • $ $ • $ $ • $ $ $ $ $ $ column = 1 while (column <= 6) If ((row==1)OR (row==7) OR (column==1) OR (column==6)) Display “$” Else Display “ “ EndIF column++ EndWhile

  44. Iteration: Draw a Diagram Using Nested Loop Flow Chart ?

  45. Iteration: Draw a Diagram Using Nested Loop Write algorithm to display all of the following diagrams.

More Related