Uploaded by Sir Mark Leslie Melendez

-3-Decision-and-Loops-Flowchart-Algorithm

advertisement
Click to edit Master title style
DATA STRUCTURES
AND ALGORITHM
Decision and Loop Structure
1
Example #1
Click to edit Master title style
Create a flowchart that ask the user to enter 3
number. Identify if the input number is odd or even.
2 2
Output
Click to edit Master title style
Enter a Number: 5
Odd
Enter a Number: 20
Even
Enter a Number: 8
Even
3 3
Algorithm
Click to edit Master title style
1. Set counter
5. Update counter
2. Print “Enter a Number”
6. Go to step 2
3. Input number
4. Identify if input number is odd/even
Modulus
- number mod(%)2
(5%2) = 2 r.1
if remainder 1
(20%2) =
r.0
(10%4) =
r.2
odd
if remainder 0
even
4 4
Click
to edit Master title style
Flowchart
5 5
Example #2
Click to edit Master title style
Create a flowchart that checks whether an
element is found on a list with ten (10) elements.
Array
list={5, 20, 8, -2, 7, 30, 0, 1. -99, 15}
6 6
Output
Click to edit Master title style
Array
list={5, 20, 8, -2, 7, 30, 0, 1. -99, 15}
Search a number: 1
1 is found on the list
Search a number: 55
55 is not on the list
7
7
Algorithm
Click to edit Master title style
1. Print “Search number”
2. Input number
3. Identify if input number is found on the list
1. compare all the element of the list and the input number
2. Move from one index to another
5
20
8
-2
7
30
0
1
-99
15
8 8
Algorithm
Click to edit Master title style
Search a number: 1
1 is found on the list
Index
0
1
2
3
4
5
6
7
8
9
Value
5
20
8
-2
7
30
0
1
-99
15
1 is found on the list
9 9
Algorithm
Click to edit Master title style
Search a number: 55
55 is not on the list
Index
0
1
2
3
4
5
6
7
8
9
Value
5
20
8
-2
7
30
0
1
-99
15
55 is not on the list
10 10
Click
to edit Master title style
Flowchart
11 11
Example #3
Click to edit Master title style
Create a flowchart that finds the lowest number
on a list of ten (10) elements.
Array
list={5, 20, 8, -2, 7, 30, 0, 1. -99, 15}
12 12
Output
Click to edit Master title style
Array
list={5, 20, 8, -2, 7, 30, 0, 1. -99, 15}
Output:
Lowest number is -99
13 13
Algorithm
Click to edit Master title style
1. Set the first element/item as the lowest number
2. Identify if there is lesser number
a. Move form one index to another to compare all the other element and
the set lowest number
b. if current lowest number is greater than the next index, change the
lowest number
Index
0
1
2
3
4
5
6
7
8
9
Value
5
20
8
-2
7
30
0
1
-99
15
Lnum > 20
14 14
Click
to edit Master title style
Flowchart
15 15
Example #4
Click to edit Master title style
Create a flowchart that arranges the elements
on a list in ascending order.
Array
list={5, 20, 8, -2, 7, 30, 0, 1. -99, 15}
16 16
Output
Click to edit Master title style
Array
list={5, 20, 8, -2, 7, 30, 0, 1. -99, 15}
Output:
list=5, 20, 8, -2, 7, 30, 0, 1. -99, 15
Sorted list: -99, -2, 0, 1, 5, 7, 8, 15, 20, 30
17 17
Algorithm
Click to edit Master title style
1. Compare all the elements (remaining elements) to find the lowest number
Lnum
LIndex
2. Swap the lowest number and the marked number
MIndex
3. Increment the marked index
4. Go to step 1
18 18
Algorithm
Click to edit Master title style
Index
0
1
2
3
4
5
6
7
8
9
Value
5
20
8
-2
7
30
0
1
-99
15
Marker
Lowest
Index
0
1
2
3
4
5
6
7
8
9
Value
-99
20
8
-2
7
30
0
1
5
15
Marker
Lowest
Index
0
1
2
3
4
5
6
7
8
9
Value
-99
-2
8
20
7
30
0
1
5
15
Marker
Lowest
19 19
Algorithm
Click to edit Master title style
Index
0
1
2
3
4
5
6
7
8
9
Value
-99
-2
8
20
7
30
0
1
5
15
Marker
Lowest
Index
0
1
2
3
4
5
6
7
8
9
Value
-99
-2
0
20
7
30
8
1
5
15
20 20
Click
to edit Master title style
Flowchart
21 21
Click to edit Master title style
Thank You. ❤
Sir Mark Leslie D. Melendez
22
Download