100% found this document useful (1 vote)
30 views

Week 3 - Problem Solving and Algorithm

Here is the problem broken down into its components: Inputs: Three numbers entered by the user Processing: Add the three numbers and divide the sum by 3 Output: The average of the three numbers This defines the key inputs, processing, and output required to solve the problem algorithmically. Breaking a problem down into these components helps ensure all aspects are addressed when developing the solution.

Uploaded by

fallon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
30 views

Week 3 - Problem Solving and Algorithm

Here is the problem broken down into its components: Inputs: Three numbers entered by the user Processing: Add the three numbers and divide the sum by 3 Output: The average of the three numbers This defines the key inputs, processing, and output required to solve the problem algorithmically. Breaking a problem down into these components helps ensure all aspects are addressed when developing the solution.

Uploaded by

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

Introduction to

Visual and Interactive Programming


(IVIP)

Week 3
Problem Solving and Algorithms
Topics

What is a problem? – Type of problems


Problem solving in everyday life
What is problem solving
Strategies
Program
Algorithm

CT005-4-0-IVIP Problem solving and algorithm


Learning Outcome

Describe pseudocode,
At the end of this
flowchart, and algorithm
lecture, YOU will be Define algorithm
to develop, test and
able to:-
debug a program

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Introduction

• Computers everywhere
Ovens, washing machines and toys. Banks and
Hospitals, Transport Reservations, Signaling
Aircraft and industrial plant controllers, Missiles
and satellites, and many more.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Six Basic Computer
Operations
• A computer can
– receive information.  input
– produce information.  output
– perform arithmetic.
– assign a value to a piece of data.
– compare two pieces of information process
` and select one of two alternative actions.
– repeat a group of actions.

CT005-4-0-IVIP Problem solving and algorithm


General Problem-Solving Concepts :
Problem Solving in everyday life

What is Problem ?

A Problem is a state of difficulty that need to be resolved

While solving a problem there is a desire to attain some specific goal


.

Will I get proper transport to go to my workspace ?


Here is some examples we face in
Should I wear shoes today ?
day-to-day life can be important or Should I go to a movies ?
can be least importance . Which Cell phone should I buy?

CT005-4-0-IVIP Problem solving and algorithm ‹#›


General Problem-Solving Concepts :
Six steps of Problem Solving

01 02 03 04 05 06
Define the Determine the Develop Select a Implement the Evaluate the
Problem Root Cause(s) Alternative Solution Solution Outcome
of the Problem Solutions

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Types of Problem

Problem Based on Sequence of instruction .


For solving some problem , series of
algorithmic solution : actions are taken to reach the solution.

The Solution that can not reached


through direct set of steps are called
Problem based on heuristic solutions .

heuristic Solutions : With heuristic solution problem solver


must follow six steps of problem
solving for more than once .

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Problem solving with computer

Solution : Instruction that must be followed to produce the


best result .

Results: It is an outcome or the completed computer -assisted


answer .

Program : Computer program are set of instructions executed


to obtain solution to certain problem. These programs are
written in some specific programming language.
Computer deal with the problem having algorithmic solution.
The field of computer that deal with solving the heuristic
problems is called artificial intelligence .

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Difficulties with problem solving

We do not understand the problem correctly .

Sometimes we do not define the problem correctly or adequately

People get afraid of taking decisions while solving the problems

Sometimes the list of alternatives is incomplete .

The sequence of solutions to the problems is not logical many


times .
When solving problem on the computer , one of the most difficult
tasks for the problem solver is writing the instructions

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Steps to Developing a Program

Define Define the problem.

Outline Outline the solution.

Develop Develop the outline into an algorithm.

Test Test the algorithm for correctness.

Code Code the algorithm into a specific programming language.

Run Run the program on the computer.

Document and maintain Document and maintain the program.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


1. Define The Problem

Divide the problem into THREE separate


Inputs: a list of source data
components
Outputs: a list of the
Processing: a list of actions
needed to produce the
provided to the problem outputs required
required outputs

The above three items can be presented in a defining


diagram.- IPO Chart

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Analyse the problem

Understand Understand Divide


Thoroughly understand the Understand problem If the problem is complex,
problem requirements divide it into subproblems
• Does program require user • Analyze each subproblem as above
interaction?
• Does program manipulate data?
• What is the output?
• Are all possible circumstances
handled?

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Analyse the problem

Purpose: How
To describe in detail a solution to • Study and understand the problem
a problem and information • Identify
needed in solving the problem. • The needed input.
• The required output.
• The needed process.
• For example: scientific
formula or hypothesis.

CT005-4-0-IVIP Problem solving and algorithm


Defining Diagram

Input Processing Output

CT005-4-0-IVIP Problem solving and algorithm


Analyse the problem

Problem 1
Write a program that will calculate and
display the average of 3 numbers

Input Process Output

CT005-4-0-IVIP Problem solving and algorithm


Analyse the problem

Problem 2
Write a program that will convert length
in kilometer to meter and centimeter

Input Process Output

CT005-4-0-IVIP Problem solving and algorithm


2. Outline The Solution

During this stage, certain details


are identified from the problem
by analyzing it further, such as:
• major processing tasks involved.
• major subtasks (if any)
• major control structures.
• major variables
• mainline logic
CT005-4-0-IVIP Problem solving and algorithm
Strategies

• Ask questions!
– What do I know about the problem?
– What is the information that I have to process in order to find
the solution?
– What does the solution look like?
– What sort of special cases exist?
– How will I recognize that I have found the solution?

CT005-4-0-IVIP Problem solving and algorithm ‹#›


3. Develop The Algorithm

A detailed step by step algorithm is written


out.

Pseudocode
Often use one of three Flowcharts
tools: Nassi-Schneiderman diagrams –
will not be covered in this module

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Before a computer can perform a task, it must have an
algorithm that tells it what to do.

Informally: “An algorithm is a set of steps that define how a


task is performed.”

Formally: “An algorithm is an ordered set of unambiguous


executable steps, defining a terminating process.”
• Ordered set of steps: structure!
• Executable steps: doable!
• Unambiguous steps: follow the directions!
• Terminating: must have an end!

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Algorithmic thinking definition

• Identifying the steps involved in solving a problem.


– What are the steps involved with making a cup of tea?
– What are the steps involved in the calculation 200÷12?
– What are the steps involved in checking if a player has hit a
target in a game?
– What are the steps involved in getting dressed for school?

CT005-4-0-IVIP Problem solving and algorithm ‹#›


What is Algorithm?

Algorithms are the practical application of algorithmic thinking.

Algorithms are simply steps/procedures used to get the intended result.

Algorithms state the steps required to get to the desired result.

Creating algorithms is all about thinking logically and so to think computationally is to think in a
structured, organized and logical manner.

Algorithms are vital to programming. Without one, coding a solution is almost impossible…but with
one, coding becomes an easy job (if you know your programming syntax that is!)

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Algorithm in a real world

Search engines such as Bing or Google use algorithms to put a set of


search results into order,

Your Facebook news feed is derived from your friends’ status updates
and other activity, but it only shows that activity which the algorithm
thinks you’ll be most interested in seeing.

The recommendations you get from Amazon, Netflix and eBay are
algorithmically generated, based in part on what other people are
interested in.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Algorithms in your lessons

A lesson plan can be regarded as an algorithm for teaching a lesson.

There will be a sequence of steps pupils follow for many activities, such as getting ready for
lunch or going to PE.

In Food, we can think of a recipe as an algorithm.

In English, we can think of instructional writing as a form of algorithm.

In science, we might talk about the method of an experiment as an algorithm.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Writing Algorithms

Algorithms are independent of any language.

Too time consuming


Pointless – could just code
Writing an algorithm in a specific language: Cannot be taken by a programmer using a different language
Too complex – need knowledge of syntax (code specific to a language)

Too time consuming


Writing an algorithm in everyday language: Open to interpretation – thus resulting in different outcomes

So, we use Flowcharts and pseudocode to write algorithms so that they are concise,
accurate and easy to understand so that a programmer of any language could understand
the steps required to solve a task

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Program Design

Problem 1
Write a program that will calculate and
display the average of 3 numbers

ALGORITHM

CT005-4-0-IVIP Problem solving and algorithm


Program Design

Problem 2
Write a program that will convert length
in kilometer to meter and centimeter

ALGORITHM

CT005-4-0-IVIP Problem solving and algorithm


Pseudo code - About

No standard pseudocode.

A pseudo code is an informal way to describe a program

Pseudo code is not a computer program

Pseudo code can use natural language or compact mathematical


notation

It is a rough sketch of the actual program

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Pseudo code - Syntax

Algorithms can be written in ‘everyday English’ as well as a flowchart

Pseudo-code has keywords such as IF, ELSE and FOR and so mimics a programming language and therefore the logic is easy to
follow and easy to turn into code.

Although there are no STRICT STANDARDS for pseudocode there are only really a few keywords that you need.

No standard for pseudo code syntax exists

We do not have to follow any strict syntax like computer programming language

Pseudo code vary in style from author to author

Pseudo code commonly borrows its syntax from popular programming languages like C, Fortran, Pascal, Java, Python etc.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Pseudo-code Start and End
“Key Words”
• Pseudocode begin with a START and ends with END
• The algorithm goes in between.
• You will need to DECOMPOSE the problem set in the
question to work out what comes in between
• Pseudocode and their statements
START
……………………………
END

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Pseudo-code Input / Output “Key Words”

At times, your program will most certainly ask the user for inputs and output values too.

Inputs and Outputs (like “Name?” or “…display age”) are indicated using the following words.

Usually, a programmer will choose one and stick with it throughout their algorithm.

READ
OBTAIN
INPUTS: GET
INPUT

PRINT
DISPLAY
OUTPUTS: SHOW
OUTPUT

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Pseudo-code Process “Key
Words”
Most of the time pseudocode will outline the logical sequence of instructions to be carried out.

Simple processes will often use the key words shown below (like “CALCULATE X*2” or “INCREMENT X by 1)”

You don’t have to always use these words, for example the logic statements such as “Add 1 to x” or “append x to List”
are fine too.

Example:
Compute Calculate Determine Increment; ++ or += Decrement; -- or -=

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Pseudo-code More Keywords

Variable Assignment “Key Words”

• At times, your program will assign values to variables.


• In pseudocode this is done using the following key words.
• SET

Decision/Selection “Key Words”

• At times, your program will be programmed to make a decision based on certain conditions.
• Decisions (like “IF X = 3, THEN …”) are shown using, the following key words.
• IF
• THEN
• ELSE
• ELSE-IF
• ENDIF

Loops / Iterations “Key Words”

• Programs will often loop in places while certain conditions occur (infinitely) or for a set number of times (finitely).
• Loops use the following key words:
• FOR
• WHILE / ENDWHILE
• REPEAT / UNTIL

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Program Design – Pseudo-code

Problem 1
Write a program that will calculate and
display the average of 3 numbers

Pseudo code

CT005-4-0-IVIP Problem solving and algorithm


Program Design – Pseudo-code

Problem 2
Write a program that will convert length
in kilometer to meter and centimeter

Pseudo code

CT005-4-0-IVIP Problem solving and algorithm


Program Design – Pseudo-code

• Example 3:
– A central heating system will try to keep the temperature
between 2 values (19 and 21)
– If the temperature falls below 19 It will turn the heating system
on
– If the temperature rises above 21 it will switch the heating
system off.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Program Design - Flowchart

Flowchart is a pictorial way to Visual representation of the logic Limited range of symbols to Arrows to show the order of
express algorithm or process. of a program describe processes instructions

So, instead of writing down the


algorithm in some programming
language like Snap!, C, C++, Flowchart as the name indicates,
Easier to follow and identify
Java, C#, PHP, Python, Ruby etc. is about the flow of execution of
issues than in pseudocode.
Use flowchart to express the our algorithm.
algorithm which gives us a
general view about the algorithm.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart and their symbols

• Start and Stop Symbols


– All flow charts begin with a Start Symbol and at the end of the
flow chart (or at various end points of the chart) we place a
Stop Symbol.
– There are drawn as a rectangle with curved ends

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart and their symbols

• Process Symbols
– Most of the time a flow chart will demonstrate the sequence of
instructions to be carried out.
– Simple processes (like “Add 1 to x” or “append x to List”) are
shown using a standard rectangle.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart and their symbols

• Input / Output Symbols


– At times, your program will most certainly ask the user for
inputs and output values too.
– Inputs and Outputs (like “Name?” or “…display age”) are shown
using a parallelogram.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart and their symbols

• Decision Symbols
– At times your program will be programmed to make a decision
based on certain conditions.
– Decisions (like “IF X = 3” or “While Y > 3”) are shown using a
diamond.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart and their symbols

• Flow Symbols
– Show direction of flow.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart - Rules

Flowchart is generally drawn from top to bottom

All boxes of flowchart must be connected with


arrow.

All flowchart start with a Terminal or Process


symbol.

Decision symbol have 2 exit points, one for YES


(TRUE) and another for NO (FALSE).

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Flowchart

Problem 1
Flowchart

CT005-4-0-IVIP Problem solving and algorithm


Flowchart

Problem 2
Flowchart

CT005-4-0-IVIP Problem solving and algorithm


4. Test Algorithm For Correctness

One of the most important in the development of a program, and


yet it is the step most often forgotten.

The main purpose of desk checking the algorithm is to identify


major logic errors early, so that they may be easily corrected.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


5. Code the Algorithm

Code the algorithm


into a specific
programming
language.
CT005-4-0-IVIP Problem solving and algorithm
6. Run the Program

Use a program compiler or


interpreter and programmer-
designed test data to machine-
test the code for both syntax
and logic errors.

CT005-4-0-IVIP Problem solving and algorithm


Testing and Debugging

Definition
Using a set of data to discover Testing Process
errors and to ensure accuracy of
the program. Diagram indicates the
process of testing.

Output
Input (functioning
Executing
sample of well or error
Program
data set discovered)

CT005-4-0-IVIP Problem solving and algorithm


Testing and Debugging

TESTING

• Assume that a program to find the average of 3 numbers has


been coded.
• Then, execute the program.
• Using a few numbers, test the program to verify whether the
result is as expected

Testing 1:
From the output,
Input: 4, 5, 8 has the program
produced the
expected result?
Testing 2:
Input: 7, 8, 6

CT005-4-0-IVIP Problem solving and algorithm


Testing and Debugging

DEBUGGING

Definition

• An error is known as bug


• Debugging is a process of identifying and correcting
errors.
• There are 3 types of error:
• Syntax error - (grammatical rule violations)
• Logic error – faulty logic
CT005-4-0-IVIP
• Runtime error – causes errors during execution.
Problem solving and algorithm
Testing and Debugging

• Logic error is an error that occurs


• Occurs when the rules of pro- because logical statement in pro-
gramming language are not ap- gram is wrong
plied. • Cannot be traced by compiler.
• Correction is done during the • Corrected during the problem solv-
program coding. Syntax error ing process
(grammatical Logic error • Also known as run time error.
• The bug can be traced during the error) • Example output for average is 4,
compilation process (i.e. when
but when it runs, the output is 2.
running the compiler on the pro-
Why this is happen?
gram)
• Also known as compile-time er-
ror
• Must be corrected before execut-
ing and testing the program

CT005-4-0-IVIP Problem solving and algorithm


Document and Maintain the
Program

Program documentation

• should not be listed as the last step


• Really an ongoing task from the initial definition of the problem to
the final test result.
• Involves both external documentation (such as hierarchy charts,
the solution algorithm, and test data results) and internal
documentation which may have been coded in the program.

Program maintenance refers to changes which may


need to be made to a program throughout its life.

CT005-4-0-IVIP Problem solving and algorithm ‹#›


Documentation

Definition:
Note: that you don’t
A written or graphical perform documentation
report of the steps taken all at the end of the
during the development of programming life cycle,
a program. instead what usually
happens is that you
write documentation
Purpose along the way and at
It will be useful in the the last step, double
future, in case of check to make sure
modification or that all documentation
maintenance. have been completed

CT005-4-0-IVIP Problem solving and algorithm


Maintenance

DEFINITION

Activity that verifies whether the operational system is performing as planned or


an activity to modify the system to meet the current requirement.

The process of changing a system after it has been applied to maintain its
ability.

The changes may involve simple changes such as error correcting

CT005-4-0-IVIP Problem solving and algorithm


Maintenance

Adjustment
Testing
Adding needs to new system.
Test the ability of the system.

Repair How to do Measurement


For example: An old system maintenance? Access data time. Example,
cannot update the new data. time to save, print and others.

Updating Replacement
Update the database. Replace the old system to new
system.

CT005-4-0-IVIP Problem solving and algorithm


Documentation

Content of Documentation:
Description of the program
Test results
Specification of program requirement
User’s manual book
Program design such as pseudo code
and flowchart Program capabilities and limitation

List of program and comments (to


explain about the program)

CT005-4-0-IVIP Problem solving and algorithm


Preview for next class

Thank You
Variables and Boolean

CT005-4-0-IVIP Problem solving and algorithm ‹#›

You might also like