P2 - Unit 7 Problem Solving and Programming
P2 - Unit 7 Problem Solving and Programming
Store current
player details
Structure diagrams
• One possible answer is shown below
• More levels would be required
for larger projects
Basketball
software
Store current
Store details Results
player details
15 /2 ( Division) 7.5
10 * 2 ( multiplication) 20
Operators 2 ^ 4 ( power)
10 MOD 2 ( Modulus)
16
Use 10 DIV 2
7 MOD 10
( Dividend) 5
15 DIV 10 1
15 MOD 10 5
( 40 MOD 10= =0 ) AND ( 35 MOD 10= = 0 ) False / AND will return a true if both the conditions around AND
are true
40 MOD 10 == 0 is true, but 35 MOD 10 == 0 is false
( 40 MOD 10 = 0 ) OR ( 35 MOD 10 = 0) True / OR will return a true if either of the condition around OR is
true or both are true
40 MOD 10 == 0 is true, but 35 MOD 10 == 0 is false
NOT ( 40 MOD 10 == 0 ) False , 40 MOD 10 == 0 is true but Not is applied outside this
condition that is why True is turned to False. Not Turns True to
False and False to True
Shapes of
the flowchart
Pseudocode To
design
algorithm
Data types and operations
Unit 9 Key programming concepts
Data types
• Variables will typically be one of the following types:
• Integer, Boolean, real, char or string
• For the variables given, the data types would be:
• numberOfStudents Integer
• circleArea Real
• found Boolean
• answer Char
• studentName String
• DOB Date
• The data type used will determine the amount of memory that needs to be allocated for the
variable
Data types and operations
Unit 9 Key programming concepts
Declaring variables
• Before using variables they should be declared
• Declaring a variable gets it ready for use in the program
• The data type is given to the variable – the programming language (compiler) can
check that it is being used correctly
• Examples of declaring and using variables
DECLARE sidesInShape : INTEGER
DECLARE name : STRING
sidesInShape ← 4
Name ← "Jody"
Data types and operations
Unit 9 Key programming concepts
Constants
• As well as variables, you can define constants in a program
CONSTANT Pi ← 3.14157926535
CONSTANT VAT ← 0.2
CONSTANT MaxPlayers ← 6
Constants
• Why declare a constant instead of a variable?
• This prevents the value from being changed accidentally by a part of code
• It shows a programmer that the value should stay the same throughout the program
Arithmetic operators
• The operators +, -, * and / are used for addition, subtraction, multiplication and
division
• ^ is used for an exponent (power of)
• The operator DIV is used for integer division, also known as quotient
• MOD (modulus) is used to find the remainder when dividing one integer by another
• What is the result of the following operations?
• weeks ← 31 DIV 7
• daysLeft ← 31 MOD 7
Data types and operations
Unit 9 Key programming concepts
• daysLeft ← 31 MOD 7
• The variable daysLeft is assigned the value 3
• This is because the remainder after the division is 3
Data types and operations
Unit 9 Key programming concepts
Orders of precedence
• Remember BIDMAS
• Brackets
• Indices
• Division
• Multiplication
• Addition
• Subtraction
• Calculate: x ← (5 – 2) + (16 – 6 / 2)
y ← 7 * 3 + 10 / 2
Data types and operations
Unit 9 Key programming concepts
Concatenating strings
• Concatenating means joining together
• The + concatenate operator is used to join together strings
firstname ← "Rose"
surname ← "Chan"
fullname ← firstname + " " + surname
OUTPUT fullname
• What will be output? , Rose Chan
• What will be output by the program?
x ← "6" + "3"
OUTPUT x 63
Data types and operations
Unit 9 Key programming concepts
If statements
• If statements allow different branches to be executed
based on the result of a Boolean expression
IF average >= 60
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Data types and operations
Unit 9 Key programming concepts
Nested if statements
• If statements may be nested:
IF member = "child"
THEN
IF day = "Saturday"
THEN
swimPrice ← 2.00
ELSE
swimPrice ← 2.50
ENDIF
ELSE
swimPrice ← 4.00
ENDIF
Total = 0
For Count 1 to 10
Output " Enter temp in Farenheit"
Input TempF
TempC= ( TempF- 32)/1.8
Total = Total + TempC
Next Count
Output Total / 10
Secondary storage
Unit 3 Computer architecture and storage
Total = 0
Output "input no of hours"
Input hours
For Count 1 to hours
Output " Enter temp in Farenheit"
Input TempF
TempC= ( TempF- 32)/1.8
Total = Total + TempC
Next Count
Output Total / hours
Secondary storage
Unit 3 Computer architecture and storage
Next Count
3 6 9 12 15
Construct pseudocode using For loop
• Write an algorithm in the form of pseudocode which takes temperatures input in Fahrenheit over a 100-day
period (once per day). Convert the temperature in Celsius with the help of following formula:
• count of number of days when the temperature was below 20C and the number of days when the
temperature was 20C and above.
WHILE … ENDWHILE
For loop Alternative while loop
Total = 0
Total 0 Count = 1
FOR Count 1 TO 7 WHILE Count < = 7 DO
INPUT Temp Input Temp
Total Total +
Total = Total +Temp
Temp
NEXT Count Count = Count +1
ENDWHILE
Algorithms and pseudocode
Unit 8 Algorithm design and problem solving
Count = 1
Count = 1 CountMore=0
WHILE Count < = 10 DO WHILE Count < = 10 DO
output “ Enter the output “ Enter the
score “ score “
Input Score Input Score
Count = Count +1 Count = Count +1
ENDWHILE if Score > 80
CountMore= CountMore+1
Endif
ENDWHILE
Print CountMore
Algorithms and pseudocode
Unit 8 Algorithm design and problem solving
REPEAT … UNTIL
• Use this when you want to keep on executing a loop
UNTIL a certain condition is TRUE
• The condition is tested at the end of the loop so it is also
known as a post-condition loop
• Rewrite this algorithm using a REPEAT UNTIL loop
instead of a WHILE loop
Output “ Enter Password”
INPUT Password
WHILE Password <> "rE5Bh9dP" DO
Output “ Enter Password”
INPUT password
ENDWHILE
OUTPUT "Correct password"
Algorithms and pseudocode
Unit 8 Algorithm design and problem solving
Repeat
Output “ Enter Password”
INPUT password
UNTIL Password = "rE5Bh9dP"
OUTPUT "Correct password"
Three types of Iterations/ Loops/ Repetitions
For loop ( Fixed Repeat Until ( Condition Controlled) While loop ( condition
iterations) controlled)
Executes fixed number of time Checks Condition at the end Check condition at the beginning
Don’t have to increment the Executes instructions inside this loop at least once Doesn’t Execute instructions inside this loop
counter Runs until condition is False at all if condition is not met.
Runs until Condition is True