problem solving what should i wear today? which sport should i do? which cell phone is best for me?...

25
PROBLEM SOLVING 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? There are lots of problems in our daily life 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 Many real world problems can be solved with a definite solution method.

Post on 15-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PROBLEM SOLVING 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

PROBLEM SOLVING

• 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?• …

There are lots of problems in our daily life

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

Many real world problems can be solved with a definite solution method.

Page 2: PROBLEM SOLVING 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

Programming is, first and foremost, a problem solving activity

The mathematician George Polya, an authority on problem solving, hasdivided 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?

PROBLEM SOLVING

Page 3: PROBLEM SOLVING 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

Sample problem: Pizza Dough Recipe (1 dough ball)

1. Gather Ingredients

2. Combine sugar (1tbs), salt (1tbs), olive oil (1tbs), flour (1cup) 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

Page 4: PROBLEM SOLVING 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

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

Sample problem: Pizza Dough Recipe (N dough ball)

Page 5: PROBLEM SOLVING 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

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

Sequence of Statements

Page 6: PROBLEM SOLVING 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

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

N: variable

Conditional

Page 7: PROBLEM SOLVING 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

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

Sub routinesmini algorithms

Page 8: PROBLEM SOLVING 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

ALGORITHM

• 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

• A finite set of unambiguous instructions performed in a prescribed sequence to achieve a goal

Al-Khwarizmi was a Persian mathematician who wrote a book on calculating with Hindu numerals in the 9th century CE. When translated to Latin, a pluralized form of his name (algorismus) became synonymous with a system of calculation.

Page 9: PROBLEM SOLVING 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

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

Page 10: PROBLEM SOLVING 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

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

FINISH

START

N=?

Gather Ingredients

Turn on mixer

Combine sugar (N tbs)…

Add N/4 cup of flour

dough comes off the sides?

Knead 15 minutes

Let rest for at least 45 minutes in warm area

Yes

Page 11: PROBLEM SOLVING 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

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.

X=5 ; variable X created and X is equal to 5Y=X ; variable Y created and Y is equal to 5X=6 ; X is changed from 5 to 6 (Y is still 5)X=X+Y ;X becomes 11Y=Y+1 ; (or Y++), Y is incremented by one, Y=6X-- ; X is decremented by one, X=10Z=X+Y ; variable Z created and its value is 16T=T+Z ; There is a mistake here, the value of T can not be known.

Example:

Page 12: PROBLEM SOLVING 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

Example: Define two number variables, increment one of them, add them and exit.

STARTX=10,Y=20X++Z=X+Y

FINISH

START

X=4, Y=8

Z=X+Y

FINISH

X++

X Y Z

4 8

5 8

5 8 13

Variable analyze:

Pseudo Code:

Flow Diagram:

Page 13: PROBLEM SOLVING 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

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.

Page 14: PROBLEM SOLVING 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

Algorithms

User input and screen output

• Algorithm can ask for user input when needed

N=?

• Algorithm can output the value of a variable in order to infoem the user

PRINT Z

INPUT N

PRINT Z

User enters a number and N is assigned to that value

User see the value of Z on the screen

Page 15: PROBLEM SOLVING 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

Algorithms

User input and screen output

Example: Write an algorithm that inputs two numbers, multiplies them and outputs the result

START

FINISH

Z=X*Y

X=?, Y=?

PRINT Z

START:INPUT X,YZ=X*YPRINT Z

FINISH

Page 16: PROBLEM SOLVING 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

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.

X>Y truefalse

Condition to control

……

Do this if condition is false (if X≤Y) Do this if condition is true (if X>Y)

Page 17: PROBLEM SOLVING 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

Some condition statements:

X>Y ;true if X is greater than Y, X==Y ;true if X is equal to YX<=Y+Z ;true if X is smaller than or equal to Y+ZX%2==0 ;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,..)

Algorithms

Control operations and Conditionals

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

Page 18: PROBLEM SOLVING 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

Algorithms

Control operations and Conditionals

Example: Write an algorithm that inputs two numbers and prints the bigger one

START

FINISH

X=?, Y=?

PRINT X

X>Y true

PRINT Y

START:INPUT X,YIF X>Y

PRINT XELSE

PRINT YFINISH

Page 19: PROBLEM SOLVING 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

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

Instructions.

True

InstructionsLoop Structure

Page 20: PROBLEM SOLVING 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

Algorithms

Loops and Iterative structures

WHILE Loop

BEGINX=0WHILE(X<10)

X=X+1PRINT X

FINISH? …

true

• 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.

This operation is done while X<10

Page 21: PROBLEM SOLVING 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

Algorithms

Loops and Iterative structures

Do-while Loop

BEGINX=10DO

X=X+1WHILE(X<10)PRINT X

FINISH

• 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.

This operation is repeated until X<10?

Statement

true

Page 22: PROBLEM SOLVING 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

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

FINISH

N=?

S<=N

S=1 T=0

T=T+SPRINT T

S=S+1

true

STARTS=1T=0INPUT NWHILE (S<=N)

T=T+SS=S+1

PRINT TFINISH

While loop

Page 23: PROBLEM SOLVING 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

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

FINISH

N=?

S<=N

S=0 T=0

T=T+S

PRINT T

S=S+1

true

STARTS=0T=0INPUT NDO

T=T+SS=S+1

WHILE (S<=N)PRINT T

FINISH

Do-while loop

Page 24: PROBLEM SOLVING 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

Algorithms

Nested structures (while-if)

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.

START

X=?

S=0

X%2==1

S++

X>0true

true

FINISH

PRINT S

Do-while loop

If structure

STARTS=0DO

INPUT XIF(X%2==1)

S++WHILE (X>0)PRINT S

FINISH

Page 25: PROBLEM SOLVING 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

Algorithms

Nested structures (if-if)

Example: Write an algorithm that inputs three numbers, then prints the biggest one.

START

X=? Y=? Z=?

X>Y

X>Z

PRINT X PRINT Z

Y>Z

PRINT Y PRINT Z

FINISH

true

true true

STARTINPUT X,Y,ZIF (X>Y)

IF(X>Z)PRINT X

ELSEPRINT Z

ELSEIF(Y>Z)

PRINT YELSE

PRINT ZFINISH