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

lec2_308

The document outlines the problem-solving process in programming, detailing steps such as defining the problem, analyzing it, designing solutions, implementing code, debugging, and documentation. It emphasizes the importance of algorithms, their characteristics, and the distinction between correct and incorrect algorithms. Additionally, it covers flowcharts as a visual representation of algorithms and provides examples of pseudocode and flowchart creation.

Uploaded by

Muhammed
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)
5 views

lec2_308

The document outlines the problem-solving process in programming, detailing steps such as defining the problem, analyzing it, designing solutions, implementing code, debugging, and documentation. It emphasizes the importance of algorithms, their characteristics, and the distinction between correct and incorrect algorithms. Additionally, it covers flowcharts as a visual representation of algorithms and provides examples of pseudocode and flowchart creation.

Uploaded by

Muhammed
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/ 35

Programming and

Algorithms
CSE308
LECTURE 2: PROBLEM-SOLVING

BY: DR. MOSTAFA ABDELRAZIK


What is problem solving?

 Problem-solving is a process of working


through the details of a problem to
reach a solution.
Problem Solving Steps

1) Define and Identify the


Problem
2) Analyze the Problem
3) Design the Solution
4) Implement the Code
5) Run and Debug
6) Documentation
The Problem-solving Process

Analysis
Problem
specification
Design

Algorithm
Implementation
Program

Compilation
Executable
(solution)
Analyze the Problem

We need to read it till we understand every detail

We need to dissect the problem into its component


parts (e.g. problems and sub-problems)

We need to remove any ambiguity, extra


information

We need to determine our knowns and our


unknowns

We need to be aware of any assumptions we are


making.
Analyze the Problem

Example. Making tea. Suppose we have a robot that


carries out household tasks. We wish to program the
Analyze the
robot to make a cup of tea. An initial attempt at an
algorithm might be:
Problem
 1. Put tea leaves in a pot
 2. Boil water
 3. Add water to the pot
 4. Wait 5 minutes
 5. Pour the tea into cup
Design the solution

Design
Developing the algorithm that solves the
problem
the
• Identify alternative ways to solve the problem
solution
• Select the best way to solve the problem from the list
of alternative solutions
• List instructions that enable you to solve the problem
using selected solution

The algorithm is expressed a s flowchart or


pseudo-code
Design the solution
Implement the Code

 Algorithm translated into a code using a high-


level language.
 The choice depends on the type of the
problem
 To get the exact result of the problem, proper
coding should be written
 The codes written should be efficient and
concise.
 It should be small enough.
What is debugging?

 Programming errors are called bugs and the process of tracking them down
and correcting them is called debugging.
 Three kinds of errors can occur in a program:
1. Syntax errors
▪ A program can only be executed if it is syntactically correct; otherwise, the process
fails and returns an error message.
▪ syntax refers to the structure of a program and the rules about that structure.
2. Runtime errors
▪ So called because the error does not appear until you run the program.
▪ These errors are also called exceptions because they usually indicate that something
exceptional (and bad) has happened.
3. Semantic errors
▪ If there is a semantic error in the program, it will run successfully, in the sense that the
computer will not generate any error messages, but it will not do the right thing. It will
do something else. Specifically, it will do what the programmer told it to do.
▪ But the written program does not solve the original problem. The meaning of the
program (its semantics) is wrong.
Testing the Algorithm

• Important distinction

➢ Mathematics
We test the answer
➢ Programs
We test the process

11
Testing the Algorithm

It should be done to report that whether


the problem has met its desired
TESTING requirements or not.

The process of proving that the program


produces correct and meaningful
results for all possible data.

It should use cases that work with all


possible parts of the program.
DOCUMENTATION

• It is a Continuous process.

• Two kinds of documentation


* User-level documentation
One needs in order to use the program.

* Technical documentation
It is addressed to the programmer who may modify the program later.
Algorithms

Algorithm
A set of unambiguous instructions for solving a
problem or subproblem in a finite amount of
time using a finite amount of data
15
Algorithms

A tool for solving a well-specified


computational problem
Input Algorithm Output
 Algorithms must be:
 Correct: For each input produce an appropriate
output
 Efficient: run as quickly as possible, and use as little
memory as possible – more about this later
16
What is a good algorithm?

❑ It must be correct
❑ It must be finite (in terms of time and size)
❑ It must terminate
❑ It must be unambiguous
▪ Which step is next?
❑ It must be space and time efficient

 A program is an instance of an algorithm, written in some


specific programming language
17
Correct and incorrect algorithms

 Algorithm is correct if, for every input instance, it


ends with the correct output. We say that a correct
algorithm solves the given computational problem.

 An incorrect algorithm might not end at all on some


input instances, or it might end with an answer
other than the desired one.

 We shall be concerned only with correct


algorithms.
From Algorithms to Programs

Problem
Algorithm: A sequence
of instructions describing
how to do a task (or
process)

C++ Program
Pseudocode & Algorithm

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”
Pseudocode & Algorithm

 Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart

 (Dictionary) A schematic representation of a


sequence of operations, as in a manufacturing
process or computer program.
 (Technical) A graphical representation of the
sequence of operations in an information system or
program.
 Information system flowcharts show how data flows from
source documents through the computer to final
distribution to users.
 Program flowcharts show the sequence of instructions in a
single program or subroutine. Different symbols are used to
draw each type of flowchart.
Flowcharts

 Flowcharts is a graph used to depict or show a step


by step solution using symbols which represent a
task.

 The symbols used consist of geometrical shapes that


are connected by flow lines.

 It is an alternative to pseudocoding; whereas a


pseudocode description is verbal, a flowchart is
graphical in nature.
Flowchart Symbols

Terminal symbol - indicates the beginning and


end points of an algorithm.

Process symbol - shows an instruction other than


input, output or selection.

Input-output symbol - shows an input or an output


operation.

Disk storage I/O symbol - indicates input from or output to


disk storage.

Printer output symbol - shows hardcopy printer


output.
Flowchart Symbols cont…

Selection symbol - shows a selection process


for two-way selection.

Off-page connector - provides continuation of a


logical path on another page.

On-page connector - provides continuation


of logical path at another point in the same
page.

Flow lines - indicate the logical sequence of


execution steps in the algorithm.
Flowchart – sequence control
structure

Statement 1

Statement 2

Statement 3

:
Flowchart – selection control
structure

No Yes
Condition

else- then-
statement(s) statement(s)
Flowchart – repetition control
structure

yes Loop
Condition Statement(s)

no
Example

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

PRINT PRINT
“PASS” “FAIL”

STOP
Example 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 30
 Print length in cm (LCM)
Example 2

Flowchart
Algorithm
START
 Step 1: Input Lft
 Step 2: Lcm  Lft x 30 Input
Lft
 Step 3: Print Lcm

Lcm  Lft x 30

Print
Lcm

STOP
Example 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
Example 3

Algorithm
START
 Step 1: Input W,L
 Step 2: AL x W Input
W, L
 Step 3: Print A

ALxW

Print
A

STOP
Flowchart – example 4

Begin

Read birth date

Calculate
Age = current year – birth date

Display
age

End
Flowchart – example 5

Begin

Read age

YES Age > 60? NO

print “Retired” print “Working”

End
Flowchart – example 6

Begin

sum = 0
current_number = 1

NO
current_number <= 10? print sum

YES
End
sum = sum + current_number
current_number = current_number + 1

You might also like