You are on page 1of 89

Introduction to programming

By: Mrs R. Bailey -Johnson


Problem solving
Objectives
Outline the steps in problem solving
Define a problem by decomposing it into its significant
components
Distinguish between variables and constants
List the different data types.
Explain the concept of algorithms
What is a problem
A problem is a discrepancy
between what is required and
what exist, problem can range
from simple to complex.
Step in problem solving

(a) define the problem;


(b) propose and evaluate solutions;
(c) determine the most efficient
solution;
(d) develop the algorithm; and,
(e) test and validate the solution.
Defining the problem
Defining the problem
is the first step towards
solving a problem. It is
the most important
step since it leads to a
clearer understanding
of what is given and
what is required.
If the programmer does not
fully understand what is
required, then he/she cannot
produce the desired solution.
This step involves decomposing the
problem into three key components:
Inputs: what is given (source data)
Outputs: the expected results
Processing: the tasks/actions that
must be performed
The defining diagram( I.P.O chart)
A table with three columns which
represents the three components: input,
processing and output.
An I-P-O Chart

A table that shows the inputs, the


processing steps and the output related to a
particular problem. An I-P-O Chart is also
known as a defining diagram.

There is no column for storage. Storage is


represented by the names of the variables
that are stated in the input and output
column.
Inputs can be identified by the keywords that precede
them. These are: GIVEN, ENTER, READ, or ACCEPT.
 
Outputs can be identified by the keywords: PRINT,
DISPLAY, FIND, PRODUCE, or OUTPUT.
 
Processing can be determined by asking;
“What do I have to do with the inputs in order to produce
the desired output?” The actions/tasks determined must be
listed in a logical sequential order.
Example
Given two numbers find
and print their product.
Answer
INPUT PROCESSING OUTPUT

2 numbers 1. Read two PRODUCT


numbers
say num1, 2. Find the
num2 product
3. Print the
product
Calculate the total salary for an
individual.
 

PROBLEM 1:
Write a solution to read three
numbers and calculate and print
the total.
Breaking down the problem
State the input
State what should be processed
What should be outputted
Solution
Input Processing Output
Get 3 numbers Total
3 numbers
Eg. NUM1 Add 3
NUM2 NUM 3 numbers
Print total
PROBLEM 2

Given three numbers representing


the age of three boys respectively,
write a solution to find their average
age.
 
Solution
Input Processing Output
Read/accept/get Average
3 integers
e.g.age1, age2, 3 integers
age3 Add the 3 ages
Divide total by
3
Print the
average
Problem 3- Activity-homework
Draw up the I.P.O Chart for the following problems
Given two(2) numbers, write a solution to find the
sum.
Given the length and width of a rectangle write a
solution to find the area.
 Susan owns and runs a Boat Hire Service that
specializes in renting out boats. She needs a
program that reads in the number of hours, that a
boat is rented, calculates the total cost and prints
out the total.
Homework
Input Processing Output
Get 2 numbers sum
2 numbers
Num 1 num 2 Add 2
numbers
Print sum
No.2
Input Processing Output
Length
Read length area
width Read width
Area=length*width
Print area
No.3
Input Processing Output

Number of hours
Set price per hour Total
Read number of Cost
hours
Calculate total
cost
total
Print total Cost
Distinguish between variables and
constants
In processing data, values that are manipulated
need to be stored in a memory location. Because of
the large number of storage location in memory,
we need to have an identifier to label each
location.
variables

Variables is an area of storage whose


value can change during processing.
Variables are needed to store
All data that will be provided by the
user.
All computation that will take place
during process
INITALIZATION OF VARIABLES

Variables that are used as counters or used to store


totals should always be assigned an initial value of
zero (0) before they are used.
 This is called initialization, e.g.  Count = 0
 This ensures that the variable is cleared of any values
that may have been assigned in a previous execution of
the program
Initialization means giving the variable an initial or
starting value.

Sometimes it is necessary to give variables a starting


value. This ensures that any data previously stored in
that memory location, from a previous operation, is
erased and not used mistakenly in a new operation.
Initialization is also necessary whenever a value needs
to be incremented (added to by a value, usually one).
Before a variable can be used by
a program it must be declared.
To declare a variable, the
program must have an:
Identifier
Data type
Constants
A constant is a named location in
memory whose value remains the
same during program execution.
The value of a constant never
changes.
Constant are useful when storing
Conversion rates, taxes, discount
rates
Before a constant can be used by a
program it must be declared. To declare
a constant, the program must have an
Identifier
A value
 
Objective 2.4: Use appropriate data types
Each piece of data stored in memory is of a specific
data type. In programming there are five (5) basic data
types.
Data types
Main data types used in programming includes:
Integer: whole number such as 0, 5,10,1024 and -50
Floating point (real) - numbers with decimal point
such as 0.2, 1.5 and -50.4
Character: units such including letters, numbers
and symbol such as A, a, 9, $ and @.
Boolean – store two possible vales eg. Yes/No
true/false
String – store multiple characters eg frank, Ruth
678-8901
Data Type Description Examples
Integer Positive and negative whole 23,-45, 0
numbers including zero
Real All numbers including fractions 15.7, -19.25, 8
Character Any key on the keyboard ‘A’, ‘z’, ‘8’, ‘?’

String Characters put together ‘Hello world’,


‘Marcus’
Boolean True or False TRUE or
FALSE
Choosing Identifier Names
Choose names that reflect the kind of data that is being
stored. It helps any reader to understand the solution
better, if the identifier reflects what they store.

For example, the variable name product indicates that


the value stored in that memory location is a product.
If instead, X was used, this does not convey the
contents of that memory location to the reader of the
solution
Important points to note :

A computer is a digital data


processor that accepts data in a
digital format, process that data and
then the output.
Data and program instruction are
stored in main memory (RAM).
continue
A program uses variables and constants as
containers for storing data in RAM. Each
of these containers must be provided with:
An indication as to whether the container
will store a value that is fixed( constant) or
one that will change ( variable).
Name ( called an identifier
Data types
Relational Operators Meaning
= Equal to
<> or ≠ Not equal to
>  Greater than
<  Less than
>= Greater than or equal to
<= Less than or equal to
Develop and represent the solution
as an algorithm
What is an algorithm
An algorithm is a sequence of
precise instructions which, if
followed, produces a solution to
a given problem in a finite
amount of time
ALGORITHMIC  STRUCTURE

Header            :  Algorithm’s name or title


Declaration    :  Brief description of algorithm and
variables used. 
                            i.e.  A statement of purpose as well as
the initialization of variables
Body                :  Sequence of steps
Terminator     :  An end statement
 
ALGORITHM EXAMPLE  1

Write an algorithm that prompts the user to enter his/her


name and prints a welcome message to the user.
Algorithm   Welcome                                                                                                        {Header}
This algorithm prints a welcome message for the user and displays his/her
name.      {Declaration}
name – stores the name which the user
inputs                                                                {Variable}

Start
Print ‘Please enter your name’                                                                                           Body
Input name
Print ‘Welcome to the world of problem-solving’,name     
 
Stop                                                                                                                                   {Terminat
or}
Characteristics of an algorithm
Have a set number of steps
Are precise
Are unambiguous
Have instructions that pass the flow from
one process to the next
Eventually terminate.
Ways of representing an algorithm
Algorithms can be represented using
pseudocode or a flowchart.

It cannot be executed by the computer.


Pseudocode uses English–like
statements that models or resembles a
programming language. A flowchart
uses geometrical objects.
Flow charts
Flowchart is a graphical
representation of an algorithm.
Flow charts symbols
The terminal

The terminator shows


the start and stop points
in a process

Use only two terminator


for each flow chart

Label the first


terminator BEGIN and
the second one END.
Input/output
The data represent
the input to the
output from a
process.

The symbol must


be labeled with
either INPUT or
OUTPUT
Process
Program instructions
that transforms
inputs into output is
recorded here

Each symbol must


be labelled with an
operations that
includes the
assignment operator
( )
Decision
Indicates a question or
branch in the process
flow.
Eg. X>0?

No need to write IF------


THEN in the symbol.
Label one branch with YES
and the other NO.
Flow Lines
Shows the direction that
the process flows
Connectors
Whenever flowchart
becomes complex or it
spreads over more than
one page, it is useful to
use connectors to avoid
any confusions. It is
represented by a circle. 
Advantages of Flowchart:

Flowcharts are better way of communicating the


logic of system.
Flowcharts act as a guide for blueprint during
program designed.
Flowcharts helps in debugging process.
With the help of flowcharts programs can be easily
analyzed.
It provides better documentation.
Flowcharts serve as a good proper documentation.
Disadvantages of Flowchart:

It is difficult to draw flowchart for


large and complex programs.
In this their is no standard to determine
the amount of detail.
Difficult to reproduce the flowcharts.
It is very difficult to modify the
Flowchart.
Develop an algorithm that accept two numbers.
The algorithm should add the two numbers and
output the result.
BEGIN

Input
Num1, num2

sum num1 + num 2

Output
Sum

End
Develop an algorithm that
accept two numbers. The
algorithm should find the
product of the two numbers
and output the result.
BEGIN

INPUT
num1,num2

Product num1 *
num1

Output
Product

End
Develop and algorithm that accept the age of three
boys. The algorithm must compute the average and
print the result.
BEGIN

INPUT
age1,age2,age3

sum age1+age2+age3

Average sum/3

Output
average

End
question
Develop an algorithm to accept the length and width of
a football field. The algorithm should compute the area
and output the result.
BEGIN

Output
Please enter length
and width

Input length, width

Area Length *
Width

Output
‘the area
is’,area

End
Develop an algorithm to perform the following task
1. Set a tax rate to 10%
2. Read an item price
3. Compute the tax payable on the item
4. Output the price after tax is added.
BEGIN

TaxRate 10%

Input ItemPrice

TaxPayable ItemPrice*TaxRate

After Tax Price


(Item Price + Tax Payable)

Output Aftertax
Price

End
Using Pseudocode to represent
algorithm
Representing Algorithm using Pseudocode
Way of documenting an
algorithm using text, numbers
and special symbols .
Input Data
Verbs/construct to use
INPUT OR READ

How to use it
INPUT name
READ name
Initialise a variable
Assign a new value to a variable
Verbs/construct to use
STORE

How to use it
STORE 0 TO age
STORE age +1to age
Output Data and information
Verbs/construct to use
OUTPUT, PRINT, OR WRITE

How to use it
OUTPUT ‘Please enter a number’
PRINT ‘End of processing’
OUTPUT Total
WRITE ‘The total is’,total
Make a decision
Verbs/construct to use Verbs/construct to use

IF…THEN IF…THEN ….ELSE


How to use it How to use it
IF X>5 THEN IF passmark >50 THEN
PRINT X PRINT ‘Pass’
ENDIF ELSE
Print ‘Fail’
ENDIF
Develop an algorithm that
accept two numbers. The
algorithm should add the two
numbers and output the result.
Solution using Pseudocode
BEGIN BEGIN
Print ‘Enter two numbers’ Print Enter two numbers
Input num1, num2 Input num1, num2
Set sum to num1 +num2 Set sum to num1 +num2
Print sum Print ‘the sum is: ‘,sum
END END
Develop an algorithm that
accept two numbers. The
algorithm should find the
product of the two numbers and
output the result.
Flowchart Pseudocode
BEGIN
INPUT num1,num2
SET product TO
num1*num2
OUTPUT product
END
OR
BEGIN
INPUT num1
INPUT num2
STORE num 1*num2 TO Product
OUTPUT Product

END
Develop and algorithm that accept the
age of three boys. The algorithm must
compute the average and print the result.
Flowchart Pseudocode
BEGIN
Print ‘Enter three ages’
Input age1, age2, age3
Set Sum to
age1+age2+age3                       
             
Set Average to Sum/3
Print ‘The sum is:   ‘,Sum
Print ‘The average is:  ‘,
Average

Stop
Develop an algorithm to accept the length and width of
a football field. The algorithm should compute the area
and output the result.
Flowchart
Pseudocode

BEGIN

OUTPUT ‘Please enter the length and width of football


field’
INPUT length,width
STORE length*width to area
OUTPUT ‘the area is’ area

END
Develop an algorithm to perform the following task
1. Set a tax rate to 10%
2. Read an item price
3. Compute the tax payable on the item
4. Output the price after tax is added.
Using pseudocode
BEGIN
STORE 10% to taxrate
INPUT itemPrice
STORE itemprice * taxrate to Taxpayable
STORE itemprice + taxpayable to AfterTaxPrice
OUTPUT AfterTaxPrice
END
Selection
A selection is a construct is
used to evaluate a condition(ask
a question) and carry out one or
more actions based on the result
of the outcome. It is also called
decision construct.
INPUT Score

Score > 50

OUTPUT Fail OUTPUT Pass

STOP
Using flow charts

You might also like