98
Structured thinking Structured thinking

Structured thinking Part 1: 1. Introduction 2. Structure Diagram 3. Comparison Part 2: 4. Examples 5. Rules 7. More examples Part 3: 8. Structure - pseudocode

  • View
    234

  • Download
    1

Embed Size (px)

Citation preview

Structured thinkingStructured thinking

Part 1:

1. Introduction

2. Structure Diagram

3. Comparison

Part 2:

4. Examples

5. Rules

7. More examples

Part 3:

8. Structure - pseudocode

9. Pseudocode - C code

10. End

ContentsContents

Morning routine

Structure diagramStructure diagram

”Morningwhile has gold in mouthd”

Get dressed Eat breakfast Go to school

TimeTimeDivisionDivision

Sequence (order in time)Sequence (order in time)

Get dressed Eat breakfast Go to school

Morning routine

8:00 am 8:02 am 8:05 am Time

Sequence

Sequence parts

Structure diagramStructure diagram

”Morningwhile has gold in mouthd”

Find clothes Put clothes on

Morning routine

Get dressed Eat breakfast Go to school

DivisionDivisionTimeTime

Take fromwash basket

Ask girlfriendwhere clothes are

SelectionSelection

Blue Pink White

What colour on your socks?

If blue existsIf blue doesn’t exist,but pink does Else

Selection

Selection parts

Structure diagramStructure diagram

”Morningwhile has gold in mouthd”

Many glasses of beer

One glassof beer

DivisionDivisionTimeTime

Morning routine

Take fromwash basket

Ask girlfriendwhere clothes are

Find clothes Put clothes on

Get dressed Eat breakfast Go to school

Iteration (repetition)Iteration (repetition)

Many glassesof beer

One beer

Drink beer (lightbeer)

As long as you’re thirsty

Iteration

Iteration part

Structure diagramStructure diagram

”Morningwhile has gold in mouthd”

DivisionDivisionTimeTime

Take a cab Run

One step

Morning routine

Take fromwash basket

Ask girlfriendwhere clothes are

Find clothes Put clothes on

Get dressed Eat breakfast Go to school

Many glasses of beer

One glassof beer

Flow chartFlow chart

Move foot one step

Run

Go to school

Eat one pancake

Eat breakfast

Put on the clothes

Ask girl/boyfriendGet from laundrybasket

Search clothes

Get dressed

Morning route

IF you find basket

WHILE pancakes <> 0

Take a cab

IF money = 0

ELSE

ELSE

WHILE distance to school <> 0

SummarySummary

A problem can be divided into a number of smaller problems.

When programming, it can sometimes be difficult to know where to start.This is where you use a structure diagram.

The endpart 1

• Examples

• Rules

• More examples

Program structureProgram structure

CrossingCrossing

A

B

Imagine driving in car B, just behind another car A.You come to a crossing.How will you know which direction it will chose?

Car A has to make a selection

CrossingCrossing a selection of directionsa selection of directions

How can you see in which direction the car in front of you will choose?

Crossing

Green signal

STOP DRIVE

Red signal

Left blinker Right blinkerNo blinker

Drive left Straight ahead Drive right

Selection

Marriage - a selection?Marriage - a selection?

Marry girl

Young man´sfuture choice

not JSP

SelectionpartsSelectionparts

Young man´s future choice

• A selection must contain at least two selection components. If It only contains one there is no selection.

Marry girl Be free!!

Like this!

Al Bundy?Al Bundy?

Young man´s future choice

We know the end…? Peg.. - make dinner!

Marry girl

It is however possible to leave a selection component empty.

Example of an iterationExample of an iteration

Initiate Input nameetc

Persons Terminate

Label

Person *Until name = ”end”

Status quo Labels Input nameetc

Print Label*Until N > 10

Iterations• Persons• Labels

Rules of ChristmasRules of Christmas

Checks hisslade

Santa ClausX-mas day

Gives onegift

*

While gifts not 0

not JSP

Santa changed his mindSanta changed his mind• Parts connected to the same component must be of similar type

Santa ClausX-mas day

Checks hisslade

First

Hands out gifts

Last

This is CORRECT!! Gives one

gift

While gifts not 0

*

Paint house - an iterationPaint house - an iteration

PaintHouse

Get paint*While painter not satisfied with color

Paint withBrush

*

While entire house not painted

not JSP

Now you can begin to paintNow you can begin to paint

Get paint Begin to

paint

Paint

House

• An iteration can only contain one repeated component

Get onecan of paint

Paint with

Brush

* *

While painter not satisfied with color

While entire house not painted

Iterations

Iterationcomponents

OK !

Example of JSPExample of JSP

Input Calculation Print

Electricity bill

Tariff A Tariff B

Tariff = A Tariff = B

16 A 20 A 25 A

Fuse = 16

= 25= 20

16 A 20 A 25 A

= 20 = 25= 16

Program structureProgram structureTransfering the problem into code

• Split the problem into smaller problems

• Make a structure of the problems (indata/outdata)

• Write down an operationlist and the conditions

?

What is an operation? What is an operation?

• An operation describes WHAT to do with all the data that exist in the structure.

• An operation can for instance: read information, calculate, sort, seek, print, etc..

Operations are placed in the structure with a symbol

Radius and Area

Program

Example: Construct a programstructure to a program that calculates and prints the area of a circle

Operationlist - what do we need?

We realize there is three important operations we need to complete this task.

• First, we need to put in (input) the radius of the circle

• Second, calculate the area

• Third, print (output) the answer

And..And..

1 32

We have three operationslisted and placed on the structure diagram

PRESTO!PRESTO!

Operationlist

1. Radius of the circle2. Calculate circle area

3. Print circle area

Radius and Area

Program

OperationlistOperationlistOperationlist

A structure to a program that calculates the area or the circumference to a rectangle

1. Read length of rectangle2. Read width of rectangle

Data

Length WidthArea or circumf.

Area Cirumf.

3. Read selection (area/circumference) 4. Calculate area of rectangle5. Print area of rectangle6. Calculate circumference7. Print circumference of rectangle

1 2

3

4 5 6 7

Program structureProgram structure

Headline oftable

Pair ofnumbers

Table

Program

Until x = 100

*

Operationlist1. Print headline of table2. Input value of x3. Print x value4. Calculate value if y5. Print y value

Conditionlist(C 1) Until x = 100

1

2

3 4 5 2• A conditionlist contains all the conditions used in the structure

Program structureProgram structureTransfering the problem into code

• Split the problem into smaller problems

• Make a structure of the problems (indata/outdata)

• Write an operationslist and the conditions

• Pseudocode can be used

?

What is pseudocode? What is pseudocode?

• Pseudocode is a combination between ordinary language and real programcode

• The pseudocode includes the sequences,selections, iterations, conditions and operationsplaced in the structurediagram

• Pseudocode can be used to simplify thestructured problem before it becomes real programcode

Milk the cow - Milk the cow - PseudocodePseudocodePeasant needs

money

Milk the cow - Milk the cow - PseudocodePseudocodePeasant needs

money

Milk thecow

Operationlist

Conditionlist

1. Input peasant_tired value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

1. If peasant_tired = yes (sel bm)2. While milk = exist3. While milk = exist

1

By machine By hand

IF peasant_tired= yes

ELSE

Squeeze Squeeze* *WHILE milk = exist

WHILE milk = exist

4 5 6 7

2 3

Milk the cow - Milk the cow - PseudocodePseudocodePeasant needs

money

Milk thecow

By hand

ELSE

Squeeze*WHILE milk = exist

6 7

IFneighbour_home = yes

Operationlist

Conditionlist

8. Input neighbour_home value9. Read selection (neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

4. If neighbour_home = yes

Sell milk

To neighbours To village

ELSE

10 12 11 12

2 3 98

Milk the cow - Milk the cow - PseudocodePseudocodePeasant needs

Operationlist

Conditionlist

13. Buy clothes14. Read money value

5. While money = exist

Spend money

Buy clothesfor wife

* WHILE money = existTo neighbours To village

ELSE

money

If neighbour _home

Sell milk

10 12 1211 1413

98

Milk the cow - Milk the cow - PseudocodePseudocodePeasant needs

money

Milk thecow

Sell milk

By machine By hand

IF peasant_tired = yes

ELSE

Squeeze Squeeze* *WHILE milk = exist

WHILE milk = exist

IF neighbour_home = yes

To neighbours To village

ELSE

Spend money

Buy clothesfor wife

*WHILE money = exist

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist4. If neighbour_home = yes5. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist4. If neighbour_home = yes5. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist4. If neighbour_home = yes5. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PEASANT NEEDS MONEY

Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIFInput milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIFInput money value WHILE money = exist Buy clothes for wife

Read money value ENDWHILE

PseudocodePseudocode

1. If peasant_tired = yes (sel bm)2. While milk = exist3. If neighbour_home = yes4. While money = exist

Conditionlist

13. Buy clothes14. Read money value

8. Input neighbour_home value9. Read selection(neighbour/village)10. Sell to neighbours11. Sell to village 12. Input money value

1. Input peasant value2. Read selection (hand/machine)3. Input milk value4. Squeeze5. Read milk value6. Squeeze7. Read milk value

Operationlist

OK!How about some real programcoding?

The endpart 2

• Structure diagram

• Structure diagram to pseudocode

• Pseudocode to C programming

Solving 2nd degree equationsSolving 2nd degree equations

C

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

y = ax +bx +c2

2nd degree equation

0

04 4

02 4

2 4

2 4

2 4

2

2

2

2

2

22

2

22

2

2

2

2

2

a xb

ax

c

a

xb

ax

b

a

b

a

c

a

xb

a

b

a

c

a

xb

a

b

a

c

a

xb

a

b

a

c

a

xb

a

b

a

c

a

( )

( )

( )

( )

0 = ax +bx +c2

D

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

0 = ax +bx +c2

Information

2nd degree equation

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

0 = ax +bx +c2

Solve equation?

One equation*While answer n or N

Information

2nd degree equation

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

0 = ax +bx +c2

Possible solution

Solve equation?

One equation*While answer n or N

Information

Constant a Constant cConstant b

2nd degree equation

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

0= ax +bx +c2

Possible solution

Solve equation?

One equation*While answer n or N

Information

Constant a Constant cConstant b

If D 0

Solution No real solution

D = b²/(4a²)-c/a

2nd degree equation

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

0 = ax +bx +c2

Possible solution

Solve equation?

One equation*While answer n or N

Information

Constant a Constant cConstant b

If D 0

Solution No real solution

One solution

If D 0

Two solutions

D = b²/(4a²)-c/a

2nd degree equation

Solving 2nd degree equationsSolving 2nd degree equationsProgram structure

0 = ax +bx +c2

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

D = b²/(4a²)-c/a

Solving equationsSolving equations

Pseudo coding

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n) end while

Rotate and Flip

Solving equationsSolving equations

Pseudo coding

Pos

sib

le s

olu

tion

2n

d d

eg

ree

eq

uatio

n

Sol

ve e

qua

tion

?

One

eq

uatio

n*

Whi

le a

nsw

er

n o

r N

Info

rma

tion

1

Con

sta

nt a

3

Con

sta

nt c

5

Con

sta

nt b

4If

D

0

Sol

utio

nN

o re

al s

olu

tion

11

2

2

One

so

lutio

n

If D

0 7

86

910T

wo

so

lutio

ns

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n) end while

Rotate and Flip

DivisionDivision

TimeTime

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n) end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n) end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Solving equationsSolving equations

Pseudo coding

Solving 2nd degree equationDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

Possible solution

2nd degree equation

Solve equation?

One equation*While answer n or N

Information

1

Constant a

3

Constant c

5

Constant b

4If D 0

Solution No real solution

11

2

2

One solution

If D 0

7 8

6

9 10

Two solutions

Lego MindstormsLego Mindstorms

Lego MindstormsLego Mindstorms

Here’s an example of translating the pseudocode to C code

Solving equationsSolving equations

C

c:\windows\system\cool.dllProgram

Solving equationsSolving equations C coding

Solving 2nd degree equations

int main(){ char answer;

I’m lost in cyberspace

Solving equationsSolving equations C coding

Solving 2nd degree equations

int main(){ char answer;

I’m lost in cyberspace

Solving equationsSolving equations C coding

Solving 2nd degree equations

int main(){ char answer;

I’m lost in cyberspace

Solving equationsSolving equations C coding

Solving 2nd degree equations

int main(){ char answer;

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program information

int main(){ char answer; info();

Don’t worry!You’ll C!!

void info(void){ printf(”\nThis program will solve equations of the 2nd degree”); printf(”\n in the form of ax^2+bx+c=0.”); printf(”\nReturns only real values.”); printf(”\n”);}

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)

int main(){ char answer; info(); answer=ask_again();

Aha, this doesn´t seemto be that difficult!

char ask_again(void){ char c; printf(”\n Do you want to solve an equation? (y/n)”); while((c=getchar())==’\n’); return c;}

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)

int main(){ char answer; info(); answer=ask_again();

Make room! I’ve got more to say!

char ask_again(void){ char c; printf(”\n Do you want to solve an equation? (y/n)”); while((c=getchar())==’\n’); return c;}

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)

int main(){ char answer; info(); answer=ask_again();

Make room! I’ve got more to say!

char ask_again(void){ char c; printf(”\n Do you want to solve an equation? (y/n)”); while((c=getchar())==’\n’); return c;}

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N

int main(){ char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) {

Make room! I’ve got more to say!

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N

int main(){ char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) {

Make room! I’ve got more to say!

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N

int main(){ char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) {

Make room! I’ve got more to say!

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, a, b, c);

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a;

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0

else

end if else

end if

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) {

} else {

} } else { }

If and else?I don’t get it!

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0

else

end if else

end if

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) {

} else {

} } else { }

If and else?I don’t get it!

Wait, check this out!

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0

else

end if else

end if

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) {

} else {

} } else { }

Voila!

calculate x = -b/(2a)display x

x = -b/(2*a);printf(”One solution X:%.2lf \n”, x);

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else

end if else

end if

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { x = -b/(2*a); printf(”One solution X:%.2lf\n”, x); } else {

} } else { }

calculate x1= -b/(2a)+Dcalculate x2= -b/(2a)-Ddisplay x1, x2

x1 = -b/(2*a)+ sqrt(D);x2 = -b/(2*a)- sqrt(D);printf(”Two solutions X1:%.2lf X2:%.2lf\n”, x1, x2);

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2 end if else

end if

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { x = -b/(2*a); printf(”One solution X:%.2lf\n”, x); } else { x1 = -b/(2*a)+ sqrt(D); x2 = -b/(2*a)- sqrt(D); printf(”Two solutions X1:%.2lf X2:%.2lf\n”, x1, x2); } } else { }

display No solution

printf(”No solutions!\n”);

Solving equationsSolving equations C coding

Solving 2nd degree equationsDisplay program informationAsk if user would like to solve an equation (y/n)While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D 0 If D = 0 calculate x = -b/(2a) display x else calculate x1= -b/(2a)+D calculate x2= -b/(2a)-D display x1, x2

end if else display No solution end if Ask if user would like to solve an equation (y/n)end while

int main(){ char answer;

info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { x = -b/(2*a); printf(”One solution X:%.2lf\n”, x); } else { x1 = -b/(2*a)+ sqrt(D); x2 = -b/(2*a)- sqrt(D); printf(”Two solutions X1:%.2lf X2:%.2lf\n”, x1, x2); } } else { printf(”No solutions!\n”); } answer=ask_again(); }

c:\windows\system\cool.dllProgram

THE END