examples rules more examples
DESCRIPTION
Program structure. Examples Rules More examples. Program structure. Transfering 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. ?. Program structure. - PowerPoint PPT PresentationTRANSCRIPT
• Examples
• Rules
• More examples
Program structureProgram structure
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
?
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