PROBLEM SOLVING There are lots of problems in
PROBLEM SOLVING There are lots of problems in our daily life • • • What should I wear today? Which sport should I do? Which cell phone is best for me? Which team is probably the winner? Will Euro be raised in the next month? … Many real world problems can be solved with a definite solution method. What should I wear today? • Look outside the window • If it is cold • Wear sweatshirt and a coat • Else, wear a shirt or t-shirt • If it is raining • Take an umbrella
PROBLEM SOLVING Programming is, first and foremost, a problem solving activity The mathematician George Polya, an authority on problem solving, has divided problem solving into a four step activity 1. Understanding the problem: • What data goes in? What will the result be? who knows about this problem? The problem should be expressed clearly. 2. Devising a plan: • A paper exercise. Once the problem is understood you must devise the plan of action to solve the problem. The inputs, outputs must be determined and the solution method should be demonstrated by flow diagrams. 3. Implement the plan: • How do I code and implement my plan ? 4. Evaluating: • Has the problem been solved – can you demonstrate this?
Sample problem: Pizza Dough Recipe (1 dough ball) 1. Gather Ingredients 2. Combine sugar (1 tbs), salt (1 tbs), olive oil (1 tbs), flour (1 cup) in mixing bowl 3. Turn on mixer 4. Add 1/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area tbs: table spoon
Sample problem: Pizza Dough Recipe (N dough ball) 1. Gather Ingredients 2. Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N cups) in mixing bowl 3. Turn on mixer 4. Add N/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area
PIZZA DOUGH ALGORITHM 1. Gather Ingredients 2. Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N cups) in mixing bowl 3. Turn on mixer 4. Add N/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area Sequence of Statements
PIZZA DOUGH ALGORITHM 1. Gather Ingredients 2. Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N cups) in mixing bowl N: variable 3. Turn on mixer 4. Add N/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area Conditional
PIZZA DOUGH ALGORITHM 1. Gather Ingredients 2. Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N cups) in mixing bowl 3. Turn on mixer 4. Add N/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area Sub routines mini algorithms
ALGORITHM • A finite set of unambiguous instructions performed in a prescribed sequence to achieve a goal • An algorithm is set of steps for carrying out a task that can be written down and implemented • An algorithm includes the steps you take to solve a problem manually • An algorithm specifies how to manipulate information Al-Khwarizmi was a Persian mathematician who wrote a book on calculating with Hindu numerals in the 9 th century CE. When translated to Latin, a pluralized form of his name (algorismus) became synonymous with a system of calculation.
REPRESENTING of ALGORITHMS Flow diagrams • • • An algorithm can be schematically expressed by flow diagrams. Flow diagram shows components of a program and relation between these components. Flow diagrams displays an algorithm by boxes and arrows. Basic flow diagram symbols: Ellipse Shows start and finish of an algorithm. Parallelogram Shows data input or output Rectangle Shows any process, function or action to do. (i. e. Arithmetic operations and value assignments ) Rhomb Shows a decision according to a condition Arrows Shows connection and direction between symbols
PIZZA DOUGH ALGORITHM START N=? 1. Gather Ingredients 2. Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N cups) in mixing bowl Gather Ingredients Combine sugar (N tbs)… 3. Turn on mixer 4. Add N/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area Turn on mixer Add N/4 cup of flour Knead 15 minutes dough comes off the sides? Let rest for at least 45 minutes in warm area Yes FINISH
Algorithms Variables, value assignment and variable operations • • • A variable can be a number, a letter or a word. The value of a variable can be changed anytime. Variable values can be determined • Before being used • By user input, • While the program running, by calculation. Example: X=5 Y=X X=6 X=X+Y Y=Y+1 X-Z=X+Y T=T+Z ; variable X created and X is equal to 5 ; variable Y created and Y is equal to 5 ; X is changed from 5 to 6 (Y is still 5) ; X becomes 11 ; (or Y++), Y is incremented by one, Y=6 ; X is decremented by one, X=10 ; variable Z created and its value is 16 ; There is a mistake here, the value of T can not be known.
Example: Define two number variables, increment one of them, add them and exit. Flow Diagram: Pseudo Code: START X=10, Y=20 X++ Z=X+Y FINISH Variable analyze: START X Y X=4, Y=8 4 8 X++ 5 8 Z=X+Y 5 8 FINISH Z 13
Algorithms User input and screen output • A program can be thought as a closed box: • The user of a program can not see the variables freely. • The user can not change the flow of a program whenever he wants. • But, a program can interactively communicate with the user. • User can input data when asked • The program outputs any data to the user.
Algorithms User input and screen output • Algorithm can ask for user input when needed INPUT N N=? User enters a number and N is assigned to that value • Algorithm can output the value of a variable in order to infoem the user PRINT Z User see the value of Z on the screen
Algorithms User input and screen output Example: Write an algorithm that inputs two numbers, multiplies them and outputs the result START: INPUT X, Y Z=X*Y PRINT Z FINISH X=? , Y=? Z=X*Y PRINT Z FINISH
Algorithms Control operations and Conditionals • Control operations allow us to alter the normal sequential flow of control in an algorithm. • A control operation has two output, true or false • A control operation always have a condition to make a decision. Condition to control … false X>Y Do this if condition is false (if X≤Y) true … Do this if condition is true (if X>Y)
Algorithms Control operations and Conditionals Some condition statements: X>Y X==Y X<=Y+Z X%2==0 ; true if X is greater than Y, ; true if X is equal to Y ; true if X is smaller than or equal to Y+Z ; true if X mod 2 is equal to 0 (if X is an even number) X>Y && X<Z ; true if X is smaller than Z and greater than Y X>6 || X%4==1 ; true if X is greater than 6 or X mod 4 is equal to 1 (X can be 1, 5, 6, 7, . . ) X is even number and smaller than Y : X%2==0 && X<Y X is a digit : X>=0 && x<10 X is not between 10 and 15 : X<=10 || X>=15
Algorithms Control operations and Conditionals Example: Write an algorithm that inputs two numbers and prints the bigger one START: INPUT X, Y IF X>Y PRINT X ELSE PRINT Y FINISH X=? , Y=? X>Y PRINT Y true PRINT X FINISH
Algorithms Loops and Iterative structures • • Sometimes, a part of an algorithm needs to be repeated more than once Loops enables us to repeat a process until specified condition is satisfied Condition Holds True Instructions. Loop Structure Instructions
Algorithms Loops and Iterative structures WHILE Loop • • • The instructions under While statement are repeated while the condition holds. While loop first controls the condition, then executes the instructions according to the control statemet. For example the algorithm below will print the value of X as 10. BEGIN X=0 WHILE(X<10) X=X+1 PRINT X FINISH This operation is done while X<10 ? true …
Algorithms Loops and Iterative structures Do-while Loop BEGIN X=10 DO X=X+1 WHILE(X<10) PRINT X FINISH Statement ? • • • true This operation is repeated until X<10 The instructions between Do and While statement are repeated while the condition holds. Do-While loop first executes the instructions and then controls the condition. If the condition holds, repeats the statement For example the algorithm above will print the value of X as 11.
Algorithms Loops and Iterative structures Example: Write an algorithm that calculates the sum of first N counting numbers (N is an input from user) and outputs the result (use while loop) START S=1 T=0 N=? While loop S<=N true T=T+S S=S+1 PRINT T FINISH START S=1 T=0 INPUT N WHILE (S<=N) T=T+S S=S+1 PRINT T FINISH
Algorithms Loops and Iterative structures Example: Write an algorithm that calculates the sum of first N counting numbers (N is an input from user) and outputs the result (use do-while loop). START S=0 T=0 N=? T=T+S Do-while loop S=S+1 true PRINT T S<=N FINISH START S=0 T=0 INPUT N DO T=T+S S=S+1 WHILE (S<=N) PRINT T FINISH
START Algorithms Nested structures (while-if) S=0 Example: Write an algorithm that continuously inputs a number from the user and counts the number of odd numbers entered. Algorithm prints the count value and exits if the user enters 0. X=? X%2==1 true START S=0 DO INPUT X IF(X%2==1) S++ WHILE (X>0) PRINT S FINISH S++ true X>0 PRINT S FINISH Do-while loop If structure
Algorithms Nested structures (if-if) Example: Write an algorithm that inputs three numbers, then prints the biggest one. START X=? Y=? Z=? true PRINT X X>Y true X>Z PRINT Y FINISH Y>Z PRINT Z START INPUT X, Y, Z IF (X>Y) IF(X>Z) PRINT X ELSE PRINT Z ELSE IF(Y>Z) PRINT Y ELSE PRINT Z FINISH
- Slides: 25