0% found this document useful (0 votes)
0 views23 pages

Algorithms

The document outlines the principles of structured programming, detailing the steps involved in writing a program including problem description, logic mapping, coding, testing, and documentation. It introduces algorithms, pseudo code, and flow charts as tools for developing and visualizing programming logic, along with examples of exercises to illustrate these concepts. Additionally, it explains decision structures and nested if statements for handling conditional logic in programming.

Uploaded by

isaacokwir98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views23 pages

Algorithms

The document outlines the principles of structured programming, detailing the steps involved in writing a program including problem description, logic mapping, coding, testing, and documentation. It introduces algorithms, pseudo code, and flow charts as tools for developing and visualizing programming logic, along with examples of exercises to illustrate these concepts. Additionally, it explains decision structures and nested if statements for handling conditional logic in programming.

Uploaded by

isaacokwir98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Structured Programming

CSC 1203

1
Introduction
• A program is a set of instructions to execute or solve a
specific problem
• A program is written in one of the computer languages and
it specifies what a computer can do at a specified time

2
Steps in Writing a Program
• Description of the Problem: A programmer starts with a good
description of the problem statement.ie identifying and defining
what the program should do
• Mapping out the Program Logic: The programmer creates
algorithms where complex computation is required and puts
some mathematical and logical expressions to represent the
logic. Tools like program flow chart and pseudo code are used
• Program Coding: this is where the programming logic is
translated into any of the programming languages. The syntax
and semantics are abided by as the selected programming
language dictates.
3
Steps in Writing a Program
– Syntax: These are the rules governing the creation of program
statements
– Semantics: These are the rules governing the extracted
meanings of the program statements
• Program Testing: After the program is coded, it is
checked to establish whether it performs the
intended tasks
• Documentation: After program testing and
verification, documentation is made to guide the
users on how to use the program (User
Documentation). Another document is written to
guide software engineers on how to maintain the
program (Technical Documentation)
4
ALGORITHMS
• An algorithm is a procedure for solving a problem in terms of
the actions to be executed and the sequence in which actions
will be executed
• An algorithm is a well-defined set of steps that provides a
solution to a specific problem
• Characteristics:
– An algorithm should have zero or more input.
– An algorithm should exhibit at least one output.
– An algorithm should be finite.
– Each instruction in an algorithm should be defined clearly.
– Each instruction used in an algorithm should be basic and
easy to perform.

5
ALGORITHM TOOLS
• Pseudo codes and program flow charts
PSEUDO CODE
• Pseudo code is an artificial and informal language that helps
programmers develop computer algorithms
• It is English that has been formalized and abbreviated to look
like high-level computer programming languages
FLOW CHART
• A program flow chart is a graphical representation of a
computer algorithm, showing all the steps leading to a
solution of a given task, through the use of standard
symbols.

6
PSEUDO CODE TERMS
• READ, INPUT
– Input process
• PRINT, OUTPUT, WRITE
– Output process
• SET, LET, ASSIGN
– Computing process
• IF, CASE
– Branching / evaluation of a condition
• DO, LOOP, WHILE, REPEAT, FOR
– Iteration / repetition of a process
7
FLOW CHART SYMBOLS
• Oval: start / stop execution

• Parallelogram: Input / Output action

• Rectangle: Computing process

• Kite: Evaluates a condition

• Arrow: Direction of processing

• Small Circle: Junction of processes

8
Pseudocode & Algorithm
• Exercise 1: Write an algorithm to determine a student’s final
grade and indicate whether it is PASS or FAIL. The final grade
is calculated as the average of four marks.

Pseudocode:
– Input a set of 4 marks
– Calculate their average by summing and dividing by 4
– if average is below 50
Print “FAIL”
else
Print “PASS”
end if
9
Exercise 1
Flowchart
START

Input Step 1: Input M1,M2,M3,M4


M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
GRADE(M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then
Print “FAIL”
N IS Y
GRADE<50 else
Print “PASS”
PRINT PRINT
“PASS” “FAIL” endif

STOP

10
Exercise 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
• Input the length in feet (Lft)
• Calculate the length in cm (Lcm) by
multiplying Lft with 33
• Print length in cm (Lcm)

11
Exercise 2
Algorithm Flowchart
• Step 1: Input Lft
START

• Step 2: Lcm  Lft x 30


Input

• Step 3: Print Lcm Lft

Lcm  Lft x 30

Print
Lcm

STOP

12
Exercise 3
Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its
area.

Pseudocode
• Input the width (W) and Length (L) of a rectangle
• Calculate the area (A) by multiplying L with W
• Print A

13
Exercise 3
Flowchart
Algorithm START

• Step 1: Input W,L Input


W, L
• Step 2: A  L x W
• Step 3: Print A ALxW

Print
A

STOP

14
DECISION STRUCTURES
• The expression A>B is a logical expression
• it describes a condition we want to test
• if A>B is true (if A is greater than B) we take the
action on left
• print the value of A
• if A>B is false (if A is not greater than B) we take the
action on right
• print the value of B

15
DECISION STRUCTURES

Y is N
A>B

Print A Print B

16
IF–THEN–ELSE STRUCTURE
• The structure is as follows
If condition then
true alternative
else
false alternative
endif

17
IF–THEN–ELSE STRUCTURE
• The algorithm for the flowchart is as follows:

If A>B then
print A
else Y is N
A>B
print B
endif Print A Print B

18
Exercise 5
• Write an algorithm that reads two values, determines the
largest value and prints the largest value with an identifying
message.

ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX

19
Exercise 5
START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”, MAX

STOP

20
NESTED IF
• One of the alternatives within an IF–THEN–ELSE statement
– may involve further IF–THEN–ELSE statement

If < condition 1 > Then


statements
Else
If < condition 2 > Then
statements
Else
If < condition 3 > Then
statements
Else
Statements
End If
End If
EndIf
21
Exercise 6
 Write an algorithm that reads three numbers and prints the
value of the largest number.
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
BIG N1 [N1>N2, N1>N3]
else
BIG N3 [N3>N1>N2]
end if
else
if (N2>N3) then
BIG N2 [N2>N1, N2>N3]
else
BIG N3 [N3>N2>N1]
end if
end if
Step 3: Print “The largest number is”, BIG
▪ Exercise: Draw the flowchart of the above Algorithm
22
Exercise 7
• Write and algorithm and draw a flowchart to read an
employee name (NAME), overtime hours worked
(OVERTIME), hours absent (ABSENT) and determine the
bonus payment (PAYMENT).

Bonus Schedule
OVERTIME – ABSENT Bonus Paid
>40 hours $50
>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy