0% found this document useful (0 votes)
19 views44 pages

Topic 1-Update

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

Topic 1-Update

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

Topic 1 – Introduction to Problem

Solving and Algorithm

Python Programming
CT108-3-1-PYP
TOPIC LEARNING OUTCOMES

Define the
steps in
developing a
At the end of this program:
topic, you should be • define a problem
able to: • outline a solution
using algorithms
• Sequence
• basic
mathematics
CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 2
Contents & Structure

• Introduction to algorithms
• Program design using pseudocodes and simple notations of flowchart
– Sequence
– basic mathematics

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 3


Overview of computer programming

Computer programming -> art.

People -> believe -> programmer -> good at mathematics -> have a memory for
figures and technical information -> be prepared to spend many hours sitting at a
computer, typing programs.

Given the right tools and steps to follow -> anyone can write well-designed programs.

A task that worth doing -> both is stimulating and fulfilling.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 4


The need of Program Design

Jumping straight into the coding phase without first designing a proper
solution usually results in a program that contains many errors.

Often the programmer then needs to spend a significant amount of time


finding these errors and correcting them.

A more experienced programmer will design a solution to the program


first, desk check this solution, and then code the program in a chosen
programming language.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 5


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 .

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

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 6 ‹#›


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

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 7 ‹#›


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.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 8 ‹#›


1. Define The Problem

This step involves carefully reading and rereading the


problem until understand completely what is required.

Divide the problem into THREE separate Processing:


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

The above three items can be presented in a defining


diagram.- IPO Chart

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 9 ‹#›


Example 1 – Add three numbers

A program is required to read three numbers, add them together and


print their total.

First steps: underline the nouns and adjectives -> will establish the
input and output components, as well as any objects required.

A program is required to read three numbers, add them together and


print their total.

Second steps: underline the verbs and adverbs used in the


specifications -> establish the action required.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 10


Defining Diagram

Input Processing Output

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 11


Exercise 1 – Find average temperature

A program is required to prompt the terminal operator


for the maximum and minimum temperature readings
on a particular day, accept those readings as integers,
and calculate and display to the screen the average
temperature, calculated by (maximum temperature +
minimum temperature)/2.
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

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 13


3. Develop The Algorithm

A set of precise steps that describe exactly the


tasks to be performed and the order in which they
are to be carried out.

Tools that often used

• Pseudocode
• Flowcharts

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 14


‹#›
What is an Algorithm?

Like a recipe ->it lists the steps involved in accomplishing a task.

set of detailed
It can be defined in programming terms as a Unambiguous
ordered instructions developed

to describe the processes necessary to produce the desired output from a given input.

Written in simple English and is not a formal document.

However, to be useful, there are some be simple, precise and unambiguous


principles that should be adhered to. An give the correct solution in all cases
eventually end.
algorithm must:

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 15


Example - Instruct someone to add up a list of
prices on a pocket calculator.

• The algorithm might be written as follow:


Turn on calculator
Clear calculator
Repeat the following instructions
Key in dollar amount
Key in decimal point (.)
Key in cents amount
Press addition (+) key
Until all prices have been entered
Write down total price
Turn off calculator

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 16


Pseudo code - About

It is easy to read and write

Allows the programmer to concentrate on the logic of the problem.

Pseudocode is structured English.

It is English that has been formalized and abbreviated to look like the high-level computer languages.

No standard pseudocode.

Authors seem to adopt their own special techniques and sets of rules, which often resemble a particular
programming language.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 17


‹#›
Pseudo code - Syntax

Pseudo code
We do not have to commonly borrows its
No standard for follow any strict syntax Pseudo code vary in syntax from popular
Statements are written
pseudo code syntax like computer style from author to programming
in simple English.
exists programming author languages like C,
language Fortran, Pascal, Java,
Python etc.

Each set of
Groups of statements Keywords and
instructions is written Each instruction is
may be formed into indentation are used
from top to bottom, written on a separate
modules, and that to signify particular
with only one entry line.
module given a name. control structures.
and one exit.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 18 ‹#›


Pseudocode – How to write?

Designing a solution algorithm -> necessary to keep in mind the fact ->
computer -> perform the set of instructions you write.

Using words and phrases in the pseudocode that correspond to some


basic computer operations -> translation from the pseudocode algorithm
to a specific programming language becomes easy.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 19


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.
• Each operation can be represented as a straightforward
instruction in English, with keywords and indentation to signify
a particular control structure.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 20


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

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 21 ‹#›


A computer can receive information (input)

When a computer is required to receive information or input from a particular source, whether it be
a terminal, a disk or any other device, the verbs are used in the pseudocode.

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

Sample of input keyword


READ OBTAIN GET INPUT

Read -> used when the algorithm is to receive input from a record on a file

Get -> used when the algorithm is to receive input from the keyboard.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 22


A computer can produce information (Output)

When a computer is required to supply information or output to a device, the verbs are used in
the pseudocode.
Sample of output keywords
• PRINT
• DISPLAY
• SHOW
• OUTPUT
• PUT

Print -> used when the output is to be sent to the printer

Write -> used when the output is to be written to a file.

Put, Output or Display -> used when output is to be written to the screen

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 23


A computer can perform arithmetic (Process)

• Programs require the computer to perform some sort of mathematical


Use actual mathematical symbols calculation
or the words for those symbols if • Or to apply a formula

• add number to total


Example: • total = total + number

To be consistent with high-level • + for add


• – for subtract
programming languages, the • * for multiply
following symbols can be written • / for divide
in pseudocode: • ( ) for parentheses

• Compute
• Calculate
Used verbs is allowed • Determine
• Increment; ++ or +=
• Decrement; -- or -=

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 24


A computer can assign a value to a variable or
memory location (Process)

There are three instances to assign a value to a variable or


memory location:
• To give data an initial value in pseudocode, the verbs Initialize or Set are used.
• To assign a value because of some processing, the symbols ‘=’ or ‘←’ are written.
• To keep a variable for later use, the verbs Save or Store are used.

Example:

• Initialize total_price to zero


• Set student_count to 0
• total_price = cost_price + sales_tax
• total_price ← cost_price + sales_tax
• store customer_num in last_customer_num

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 25


A computer can compare two variables and
select one of two alternative actions (Process)

• Important computer operation -> is the ability to compare two


variables and select one of two alternative actions.
• Sample keywords
– IF, THEN and ELSE.
– ENDIF (to close the operation)
• Example:
IF student_attendance_status is part_time THEN
add 1 to part_time_count
ELSE
add 1 to full_time_count
ENDIF

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 26


A computer can repeat a group of actions
(Process)

• Programs will often loop in places while certain conditions occur


(infinitely) or for a set number of times (finitely).
• Sample keyword for repetition:
– FOR
– WHILE / ENDWHILE
– REPEAT / UNTIL
– DO/WHILE/ENDWHILE

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 27


Example: Pseudocode

 Execution sequence follow the steps flow.


Example: Algorithm for multiplying two numbers

1. Start
2. Get A
Execution
3. Get B
sequence
4. Calculate result
C=A*B
5. Display result C
6. End

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 28


‹#›
Flowchart

Is a pictorial way to express algorithm or process.

Instead of writing down our algorithm in some programming language like C, C++, Java,
C#, PHP, Python, Ruby etc. we use flowchart to express our algorithm which gives us a
general view about the algorithm.

Flowchart as the name indicates, is about the flow of execution of our algorithm.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 29 ‹#›


Flowchart – Basic Symbol

Terminal Input/Output Process Decision


• The terminal symbol • Represents an input or • Represents any single • Represents a decision in
indicates the starting or output process in an process in an algorithm, the logic involving the
stopping point in the logic. algorithm, such as reading such as assigning a value comparison of two values.
• Every flowchart should input or writing output. or performing a • Alternative paths are
begin and end with a calculation. followed, depending on
terminal symbol. • The flow of control is whether the decision
sequential. symbol is true or false.

Predefined process On-pagr Connector Flow


• Represents a module in an • Connectors are used to • Show direction of flow.
algorithm – that is, a connect breaks in the
predefined process that flowchart.
has its own flowchart.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 30


‹#›
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).

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 31 ‹#›


Example: Algorithm for multiplying two
numbers

Start

Get A
Get B

Calculate Resut
C=A*B

Display the
Result C

Stop

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 32


‹#›
The Flowchart Explanation

Start Terminal.
Start Program start
here

Read A Input.
Read B Enter values for
A and B

Calculate Resut
C=A*B Process

Display the
Result C Output

Stop Terminal
Stop Program end
here

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 33


Example: Use of connectors on the
same page.
Start

1 2

1- connection on the same


flowchart portion

2- connection on the different


Stop flowchart portion
1

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 34


Example: Use of connectors on the
different page.
Page 1 Page 2

Start

2
1

Yes 1

No Stop

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 35


4. Test Algorithm For Correctness

Test data needs to


be walked through
The main purpose of
One of the most each step in the
desk checking the
important in the algorithm to check
algorithm is to
development of a that the instructions
identify major logic
program, and yet it described in the
errors early, so that
is the step most algorithm will
they may be easily
often bypasses. actually do what
corrected.
they are supposed
to.

‹#›

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 36


5. Code the Algorithm

Code the algorithm


Only after all
into a specific
design
(chosen)
considerations in
programming
the previous
language.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 37


6. Run the Program

Uses a program compiler or interpreter and programmer-


designed test data to machine test the code for syntax errors
(those detected at compile time) and logic errors (those detected
at run time).

This is usually the most rewarding step in the program


development process.

If the program has been well designed, the time-wasting


frustration and despair often associated with program testing are
reduced to a minimum.

This step may need to be performed several times until you are
satisfied that the program is running as required.

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 38


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.
• Program -> well designed using structured programming techniques,
the code will be seen as self-documenting, resulting in easier
maintenance.
CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 39
‹#›
Sequential Structure

• A series of steps or statements that are executed in the order they are
written in an algorithm.

• Pseudo code - Mark the beginning & end of a block of statements.

1. Start
2. Statement_1
3. Statement_2
4. Statement_3
n. Statement_n+1
N+[Link]

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 40


Sequential Structure - trace Start

Input: Read
Length = 5 Length,
Width = 3 Width

Process: Calculate Area


Area = 5 * 3 = 15 Area=Length * Width

Process: Calculate Perimeter


Perimeter = Perimeter=
2* (5+3) = 16 2 * (Width+Length)

Output
Print
Area: 15 Area,
Perimeter
Perimeter: 16

Stop

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 41


Summary / Recap of Main Points

• Problem Solving Using Programmed Solutions


– Introduction to algorithms
– Program design using pseudo codes and simple notations of
flowcharts
• sequence
• basic mathematics

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 42


What To Expect Next Week

In Class Preparation for Class


• Introduction to control structure • Students are required to do
and iteration some reading
• Program design using
pseudocodes and simple
notations of flowcharts
• Decision Structure
– If
– If else
– If elif else
• Repetition Structure

CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 43


CT108-3-1-PYP Introduction to Problem Solving and Algorithm SLIDE 44

You might also like