General Principles, Connecting Computational Thinking and Program
General Principles, Connecting Computational Thinking and Program
the complexity of the main procedure is reduced (only the abstraction of the sub-procedure, the calling command, is displayed)
this allows easier understanding of the working of the procedure
each sub-procedure has its own task
if many procedures use the sub-procedure, code duplication can be avoided → reduced risk of bugs in the code
easier management of the code as each function can be developed on their own
4.1.4 Identify when decision-making is required in a
specified situation.
• Every time there are two possible ways for the algorithm to go.
For example, decision making is required to decide if the
entered data is valid in a system.
4.1.5 Identify the decisions required for the
solution to a specified problem
• Lets take the example that a system needs the age of the users.
To prevent invalid values to be entered, a validation algorithm
is used. The decisions required for this algorithm:
• Is a negative value entered?
• Is the value not greater than 150?
• Depending on the system, is the age below 18?
4.1.6 Identify the condition associated with a
given decision in a specified problem
A computer needs a set of instructions, called programs, to perform the I-P-O cycle.
A program needs to be written in a specific language, called a programming language.
Program
A program specifies:
Input to be provided
Process to be done
Output expected
Machine language code:
Collection of binary digits 0 and 1
Represents Data and Instructions
Machine code to represent the commands, ADD and SUBTRACT
• Command
• Machine Code
• ADD
• 1010000100
• SUBTRACT
• 1010001000
•
Characteristics of an algorithm:
Has fixed number of steps
Each specifies the action to be performed
Accepts input data
Generates one or more outputs after the input is processed
Let us see how to
write an algorithm
to withdraw a
required amount
from an ATM.
An algorithm is a sequence of instructions in English language written to solve a given
problem.
A flowchart is a graphical representation of the steps to be followed for solving a problem.
Pseudocode is a detailed yet readable description of what an algorithm must do, expressed
in a formally styled natural language rather than in a programming language.
The algorithm to add two numbers The following figure represents the pseudocode for the
and display their sum is: algorithm alongside
1. Start the algorithm.
2. Accept two numbers.
3. Calculate the sum of numbers.
4. Display the sum.
5. End the algorithm.
Algorithm to withdraw an amount from an ATM will be:
1. Start the algorithm.
2. Go to any local ATM.
3. Insert/swipe your ATM card.
4. Press the language button that you want to choose.
5. Enter the ATM PIN.
6. Press the account type (Savings or Current) button.
7. Press the cash withdrawal button.
8. Enter the amount you want to withdraw.
9. Collect the amount from the ATM machine.
10.A transaction slip confirming the successful transaction will be printed.
Collect the transaction slip from the ATM machine.
11.Collect your ATM card.
12.End the algorithm.
Let us look at
another algorithm to
troubleshoot the ‘No
sound’ problem of a
television.
Algorithm to troubleshoot the ‘No sound’ problem:
1. Start the algorithm.
2. Ensure that the Mute button is not pressed.
3. Ensure that the desired volume is set.
4. If sound is still inaudible, call the television engineer.
5. End the algorithm.
Flowchart:
A graphical representation of an algorithm
Consists of a set of symbols
Each symbol represents a specific kind of activity
Pseudocode:
Written in a formally-styled English language
Used as an initial in the process of developing a program
Provides a detailed template to programmers
You are asked to write a step-by-step procedure to solve a problem that involves a lot of nested conditions. Which among the following
tools should be the ideal tool to be used in this situation?
1. Flowchart
2. Pseudocode
Solution:
Flowchart
Let’s Practice
Flowcharts:
Represent programming logic in a graphical form
Depict logic in a step-by-step process
Use a set of symbols
Use arrows to indicate the flow of process
Drawing Flowcharts
Solution:
Off page connector
Make a flowchart to determine whether a
number is Even or Odd.
Advantages
• Helps in analyzing the problems effectively
• Acts as a guide during the program development phase
• Helps in easy debugging of logical errors
Limitations
Flowchart extended over multiple pages reduces readability
Drawing a flowchart is time consuming
Changes in a single may cause redrawing the entire
flowchart
Pseudocode:
Written in a formally-styled English language
Used as an initial in the process of
developing a program
Provides a detailed template to programmers
Advantages Limitations
Easier and faster to write Does not provide a
graphical representation
Does not need to be rewritten if Depiction of too many
any change is are made nested conditions
makes it difficult to
Can be easily converted to a understand
program
You are asked to write a step-by-step procedure to solve a
problem that involves a lot of nested conditions. Which
among the following tools should be the ideal tool to be
used in this situation?
1. Flowchart
2. Pseudocode
Solution:
Flowchart
• Write an algorithm to accept two numbers, divide the first number by
the second, and display their quotient.
(Allocated time to solve the problem is 10 minutes.)
• Write an algorithm that accepts distance in kilometers, converts it into
meters, and then displays the result.
(Hint: Distance in meters = Distance in kilometers * 1000
Allocated time to solve the problem is 10 minutes.)
• Write an algorithm that accepts five numbers and displays the sum and
average of the numbers.
(Allocated time to solve the problem is 15 minutes.)
Pseudocode:
Represents the logic of the solution to a problem
Can be converted into a program using:
Vocabulary of a programming language
Syntax of a programming language
Keywords used to write a pseudocode:
begin……end
accept
compute
display
if……else
repeat...until
while
for( ; ; )
call subroutine_name
Variable:
Refers to a memory location
Can store only one value at a time but the value can vary
Constant:
Refers to a memory location
Holds a fixed value
4560 4562 4564 Address of memory
location
Write the pseudocode to accept five numbers and display the sum and
average of the numbers.
Write the pseudocode to accept item name, price, and quantity from a
user and display the item name and its total value. The total value can
be calculated as the product of price and quantity.
Thinking ahead
4.1.9 Identify the inputs and outputs required in a solution
4.1.10 Identify pre-planning in a suggested problem and solution
4.1.11 Explain the need for pre-conditions when executing an
algorithm
Algorithms make decisions based on some pre-conditions. So, they need
some initial conditions after which they direct their working. Pre-conditions
are often given as parameters to the algorithm.
4.1.12 Outline the pre- and post-conditions to a specified problem
4.1.13 Identify exceptions that need to be considered in a specific
problem solution
4.1.14 Identify the parts of a solution that could
be implemented concurrently
Selection sort: The algorithm starts with the first element in the array. Then it loops through the
rest of the array and finds the smallest element and switches it with the first one. Then it goes on
to the second element and repeats the procedure until it reaches the end of the array.
Example: an array with content 4|2|1|6|3|5 should be sorted:
4 2 1 6 3 5 Minimum is
1, so switch
first and
third
element
1 2 4 6 3 5 Minimum is
2. Because
it is already
at the first
position
nothing is
changed
1 2 4 6 3 5 Switch 4
and 3
1 2 3 6 4 5 Switch 6
and 4
1 2 3 4 6 5 Switch 6
4.2.2 Outline the standard operations of collections
Lists: A finite ordered collection of values, where the same value may occur more than
once.
Operations:
•constructor for creating an empty list
•testing whether or not a list is empty
•prepending an entity to a list
•appending an entity to a list
•determining the first component (“head”) of the list
•referring to the list consisting of all components except for the first one (this is the “tail” of
the list)
Stack: A special kind of collection where the only operations are the
addition or removal of an entity to the collection, known as push and pop.
This makes a stack a Last-In-First-Out (LIFO) data structure.
Queue: A special kind of collection where the entities are kept in order and
the only operations permitted are the addition or removal on an entity,
where the first added entity is removed first. This makes a queue a First-
In-First-Out (FIFO) data structure.