Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structure Senior Lecturer

Similar presentations


Presentation on theme: "Control Structure Senior Lecturer"— Presentation transcript:

1 Control Structure Senior Lecturer
Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): (b):

2 Control Structure Previously… Previously… Sequential Selection
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. 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

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: … until 30 times {consistent sequence} = Problem Definition: Input Process Output Nothing as the value for each number is identified The first number is 3 The different between a number and second number is 3 The relation between next number and current number is next number = current number + 3 OR number = number + 3 Total of 30 numbers

6 Problem Solving: Manipulate a series
Calculate the total of the following series: … 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: … until 30 times {a pattern of sequence} Problem Definition: Input Process Output Nothing as the value for each number is identified The first number is 1 The second number is 6 The different between first number and second number is 5 Starting from the 2nd number onwards, the different between the number and second number is consistent, icrease by 3 next number = current number + 3 OR number = number + 3 Total of 30 numbers

8 Problem Solving: Manipulate a series
Calculate the total of the following series: … 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 = … until 10 times G = … until 15 times H = F + G {a missing formula} Problem Definition: Input Process Output Nothing as the value for each number is identified Calculate Total F : number = number + 6 : 10 times Calculate Total G : number = number + 4 : 15 times Calculate Total H : Total F + Total G Total of F + G

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 counter = 1 While ( counter <= 15 ) G = G + number number = number + 4 H = F + G Display H End

11

12 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: Input Process Output n n will be input by user, n is the end value factorial = factorial * number factorial

13 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

14 Problem Solving: Manipulate a series
Flow Chart ?

15 Problem Solving: Manipulate a series

16 Iteration Control Structure
Sentinel Loop

17 Iteration: Sentinel Loop
scenario where the exact number of repetition is unknown but there must be a condition to stop the repetition the syntax is: all statements in between while .. endWhile will 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

18 Iteration: Sentinel Loop
Ask the user to enter a group of numbers. The last number is 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 endWhile End Start Read mark while ( mark => 0.0 AND mark <= 100 ) If ( mark < 50 ) Display mark EndIF endWhile End

19 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 ? total = 115 count = 3

20 Tracing Algorithm: Sentinel Loop
Given the following algorithm; 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: 9 8 ? Give a meaningful name for XX. Replace MYSTERY MESSAGE FOR with a suitable words.

21 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

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

23 Problem Solving: Sentinel Loop
Ask the user to enter a set of numbers. Calculate how many number is divisible by 5. The process stops when the user enters 999. (pg 292) Calculate the total of the following series and count how many numbers involved. (pg 296) …………………… Count how many numbers which is divisible by 7 in between 1 and 100. (pg 297) Calculate the mark and the grade for students. The mark is the average of two highest test mark between three test marks. The process stops when there are no more data to process. Use the following table to determine the grade. (pg 300) Calculate the mark and the grade for students. The mark is the average of two highest test mark between three test marks. The process stops when there are no more data to process. Use the following table to 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. (pg 303) Mark Grade 100 – 80 A 79 – 65 B 64 – 60 C 59 – 40 D 39 – 0 E

24 Iteration Control Structure
Infinite Loop

25 Iteration: Infinite Loop
non stop loop the repetition keeps running, it is never stop the syntax is: 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 Display count End

26 Iteration Control Structure
Nested Loop

27 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 Display count End

28 Iteration Control Structure
Draw a Diagram Using Nested Loop

29 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 col = 6

30 Iteration: Draw a Diagram Using Nested Loop
Start line = 1 While (line <= 5 ) If (line <= 3) Else Display newline EndIF line++ EndWhile End 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 cauntSymbol = 5

31

32 Iteration: Draw a Diagram Using Nested Loop
Start row = 1 while (row < = 7) If ( (row = =1) OR (row = =7) ) Else EndIF Display newline row ++ EndWhile End 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 column = 6

33 Iteration: Draw a Diagram Using Nested Loop
Flow Chart ?

34 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 symbols and white space. We can solve the problem using a matrix concept. 1 2 3 4 5 row = 5 column = 6

35 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: @

36 Iteration: Draw a Diagram Using Nested Loop

37 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).

38 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 column = 6

39 Iteration: Draw a Diagram Using Nested Loop
Flow Chart ?

40 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

41 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

42 Iteration: Draw a Diagram Using Nested Loop
Flow Chart ?

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


Download ppt "Control Structure Senior Lecturer"

Similar presentations


Ads by Google