Lecture 1a - Algorithm
Lecture 1a - Algorithm
STRUCTURES
INF 191
SAMUEL N. N.
NORTEY
PROBLEM SOLUTION
ABSTRACT TRANSFORMED
MODEL MODEL
• ASSIGNMENT STATEMENT:
- Causes a value to be
assigned to a variable
- Has a general form:
variable name : =
expression
....Linking Learners Everywhere © 2013 All Rights Reserved
PROBLEM 2
• Algorithm to add two numbers,
named First and Second, and
assign the result to Sum.
• SOLUTION:
begin
Input First and Second;
Sum := First + Second:
end ....Linking Learners Everywhere © 2013 All Rights Reserved
BUILDING BLOCKS OF ALGORITHM
• CONTROL STATEMENT:
- It determines the order in
which statements in algorithm
are to be implemented.
- They are of 3 main types:
* Compound Statements
* Conditional Statements
* Iterative Statements
....Linking Learners Everywhere © 2013 All Rights Reserved
BUILDING BLOCKS OF ALGORITHM
• A COMPOUND STATEMENT:
- Is a list of statements to be
executed as a single unit.
- Takes the form:
begin
statement 1;
statement 2:
:
:
statement n;
end ....Linking Learners Everywhere © 2013 All Rights Reserved
PROBLEM 3
• An algorithm to interchange the
values assigned to two variables
One and Two.
• Solution:
begin
Input One and Two:
Store := One:
One := Two;
Two := Store;
end ....Linking Learners Everywhere © 2013 All Rights Reserved
BUILDING BLOCKS
• CONDITIONAL STATEMENT:
- Allows a choice between 2
alternatives
- It is of the form if – then or
if – then – else.
begin
if condition then statement
end
and
begin
if condition then statement
....Linking Learners Everywhere © 2013 All Rights Reserved
PROBLEM 4
• Algorithm to compute the
absolute value of n and store the
result in abs.
• SOLUTION
begin
Input n;
if n < 0 then abs : = -
n
else abs : = n;
output abs;
....Linking Learners Everywhere © 2013 All Rights Reserved
BUILDING BLOCKS
• ITERATIVE STATEMENT OR LOOPS:
- Can be one of the following forms
• SOLUTION
begin
sum := 0;
for i := 1 to n do
begin
j := i * i;
sum : = sum +j;
end
output sum;
end
....Linking Learners Everywhere © 2013 All Rights Reserved
SUMMARY LECTURE 1
• DISCRETE MATHEMATICS
• MATHEMATICAL MODELLING
• A GRAPH
• ALGORITHM
• PRIM’S ALGORITHM
• PSEUDOCODE
• ASSIGNMENT STATEMENT
• CONTROL STATEMENTS
• A COMPOUND STATEMENT
• A CONDITIONAL STATEMENT
• ITERATIVE STATEMENT
....Linking Learners Everywhere © 2013 All Rights Reserved
Contact
Phone:
Email:
snortey@gtuc.edu.gh ssamnot@
hotmail.com
Skype: snorteygtuc
Website: gtuc.edu.gh