0% found this document useful (0 votes)
11 views

Problem Solving & Algorithms Lesson

Information Technology CSEC

Uploaded by

ainnoxid123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Problem Solving & Algorithms Lesson

Information Technology CSEC

Uploaded by

ainnoxid123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Problem Solving & Algorithms

Problem Solving
❖ Mathematical problems such as calculating a sale
invoice
❖ Search for patient information at a hospital

❖ Storing information about customer

❖ Sort items in order in a shop


Steps in Problem Solving
❖ five (5) steps in program development
❖ 1.Define the Problem
❖ 2.Propose and evaluate solutions
❖ 3.Determination of the most efficient solution
❖ 4.Develop and represent algorithm
❖ 5. Test and Validate the solution
Step 1-Define the problem

❖ Carefully read and reread the problem until you


understand completely what is required.
❖ Ensure the problem statement is unambiguous
❖ May use a defining diagram for more clarity
Example of Defining Diagram
(IPO)
INPUT PROCESSING OUTPUT

3 NUMBERS 1. Read! get 3 numbers TOTAL

Say numl , num2, num3 2. Add numbers together

3. Print total
Step 2-Propose and evaluate
solutions
Once a solution is found, we can then review it to
see how it can be optimized or made more
efficient.
❖ Introduce possible solutions
❖ Compare solutions using advantages and
disadvantages
Step 3- Determination of the most
efficient solution
❖ We may have to go through a series of refinements
in order to get the most efficient solution.
Step 4- Develop and represent
algorithm
❖ The solution outlined in step 3 is then expanded
into an algorithm. The use of a pseudocode or
flowchart maybe used to implement the algorithm.
Step 5 – Test and Validate the
solution
❖ This is one of the most important steps. The main
purpose of desk checking the algorithm is to
identify major logic errors early, so that they may
be easily corrected.
What is an algorithm
❖ It is a set of detailed, unambiguous and ordered
instructions developed to describe the processes
necessary to produce the desired output from a
given input.
❖ Characteristics:
✔ Finite steps
✔ Precise instructions
✔ Unambiguous
✔ Flow of control from one process to another
Algorithm
❖ In simple terms, an algorithm is a series of
instructions to solve a problem (complete a task)

❖ Problems can be in any form


– Business
• Need to order products from the supermarket
• Allocate manpower to maximize profit
– Life
• I am hungry. How do I order pizza?
• Explain how to tie shoelaces to a five year old child
Types of Algorithms
❖ Pseudocode - imitation program structured like
English. It is English and mathematical
expressions that has been formalised and
abbreviated to look like high-level computer
languages.
❖ Flow chart - is a formalized graphic representation
of a program logic sequence(instructions) of a
solution to a problem
Important Terms
❖ Variable – name given to a memory location(cells)
, designed to stored a particular data item.
Variable value may change during program
execution.
❖ Constant–is a data item with a name and a value
that remain the same during the execution of the
program.
Program Data –Data Types
❖ Integer
❖ Real
❖ Character
❖ Boolean
❖ String
Examples
Name Type of Data Examples
❖ String Holds Text 'New York', 'Evan'
❖ IntegerHolds whole number 3, 6, 1024
❖ Real Holds Decimal 3.14, 503.2
❖ Boolean Holds True or False TRUE, FALSE

❖ Char Holds a single character 'A', 'E'


Naming Variables

❖ All variable names must be meaningful. The name


should be transparent enough to adequately
describe the variable.
❖ If you use more than one word in a variable name,
then underscores are useful word separators.
❖ An alternative is to join words together with the
use of a capital letter
Naming Variables
NB. Try to keep your variable names under 8
characters

Invalid Names valid Names


Num 1 num1
stud-name stud_name
Stud Age studAge
1sum sum
Declaring Variables
❖ Num as integer

❖ Name as string

❖ Avg as real
Input Statement
❖ Allows the user/programmer to input data into the
algorithm
❖ provide the means of transferring data from
external media to internal process
❖ Input statement is written with Keyword and
variable.
❖ E.g. Read Num
Input Name
Practice Questions
❖ Write an instruction to accept the price and colour
of a car.
Ans: Read Price, colour
❖ Write an instruction to input three numbers.
❖ Write an instruction to read the names and ages of
two students.
❖ Write an instruction to read the radius of circle.
Parts of an algorithm
❖ Input statement
❖ Output statement
❖ Process (assignment & Arithmetic)
❖ Control process (If structure & Loop Structure)
Assignment & Calculation
❖ This statement is used to store a value into a
variable.
❖ The variable on the left (Lvalue) take the value of
the variable on the right (Rvalue)
Eg.
Sum = 0
❖ 0 Memory location
Assignment and Arithmetic
operators
❖ The arithmetic operators are:
+ Addition
- Subtraction
* Multiplication
/ Division
Mod or % Modulus (remainder)
❖ These operators can be used with simple
expressions (e.g. variables, literal values) to form
compound expressions
Assignment statement
❖ On the previous slide, we saw a couple of
examples of perfectly legal statements that don’t
make sense algebraically:
num = num + 1
num = 15 / num
❖ Remember, the operator “=” is pronounced “gets,”
not “equals,” in algorithm
– The expression to the right of the operator is evaluated
first
– The variable to the left of the operator gets, or is
assigned to store, the value of the expression
Assignment statement
❖ Once a variable is declared, it can be assigned
values multiple times in a program; for example:

num = 3 // num assigned 3

num = num + 2 // num assigned 3 + 2 (5)

num = 20 / num // num assigned 20 / 5 (4)


Activity – write the value of the
memory location
A = 10

B = 15

B = A

Sum = B+A
Example
❖ x = 4, y = 9, z

❖z=x+y*2 // result is 22
❖ z = (x + y) * 2 // result is 26
❖y=y–1 // result is 8
❖ Z = y mod x // result is 0
Practice questions - Teacher
❖ Practice writing input, assignment and calculation
instructions
Questions
1. Write an algorithm to accept two number and
find their total.
2. Write an algorithm to accept the length of a
square and calculate the area.
Practice questions - Students
❖ Write an algorithm to accept three numbers and
find their average.
❖ Write an algorithm to accept the diameter of a
circle and calculate the circumference
Practice questions - Students
1. Write an algorithm to accept the days in a month
and calculate the number of hours in that month
Practice questions - Students
❖ Write an algorithm to accept the diameter of a
circle and calculate the area of the circle.
Practice questions - Students

❖ Write an algorithm to accept a hour and calculate


the amount of seconds in that value

I hope you understand assignments and


calculations
Doing Happy
Dance
Output Statement
❖ This statement allow communication with the user
by providing feedback though prompt or display
results on the screen
❖ Prompt is providing some instructions for the user
Eg. Print “ Enter a number”
❖ Display value after an algorithm uses the variable
and expressions to manipulate data, the results are
displayed on the screen.
❖ Eg. Print “ The total is” , Sum
How output statement works
❖ All instruction between quotes in the output
statement is displayed directly on the screen
❖ Screen
❖ Print “ Enter a Number”
Enter a Number
__
Sum = 20
❖ Print “ The total is” , Sum
The total is 20
Practice Question - Teacher
❖ Write an algorithm to prompt the user enter two
values and print their product.
Practice Questions - Students
❖ Write an algorithm to accept the names and ages
of two students and print the average age.
❖ Write algorithm to accept a number and print the
square of the number.
❖ Write an algorithm to accept a student test score
which was marked out of 25. print the percentage
value of the score.

You might also like