cs111: programming language1€¦ · a typical programming task can be divided into two phases:...
TRANSCRIPT
Agenda
Problem Solving Techniques
Pseudocode
Algorithm
Flow charts
Examples
Dr. Amal Khalifa, 2014
2
How People Solve Problems
A Problem exists when what we have (Data) is not
the same as what we want (information)
People create a solution (called an Algorithm)
which manipulates Data into Information
People do this quickly and often in a complex
way
Dr. Amal Khalifa, 2014
3
How Computers Solve Problems
Computers also use Algorithms to solve problems,
and change data into information
Computers can only perform one simple step at a
time
Complex “Human” Algorithms must be broken
down into simple step-by-step instructions BEFORE
they can be translated into computer code
Dr. Amal Khalifa, 2014
4
What is Programming?
Dr. Amal Khalifa, 2014
5
Planning or scheduling the performance of a task.
Consciously thinking about each step
Example: Accelerating in a car
1. Move right foot to gas pedal
2. Apply pressure to gas pedal with right foot
3. If speed is too high, apply less pressure.
4. If speed is too low, apply more pressure.
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into
two phases:
Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
Implementation phase
implement the program in some programming
language
Dr. Amal Khalifa, 2014
6
Algorithms
(source : wikipedia) In mathematics, computing, linguistics and related disciplines, an algorithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-state.
Algorithm: A step-by-step problem-solving process in which
a solution is arrived at in a finite amount of time
Dr. Amal Khalifa, 2014
7
Problem Solving
Problem Solving is the ability to understand what
you have, what you want, and creating a set of
instructions to change what you have into what you
want
Good Problem Solving Skills are based on
knowledge, experience and logic
Good Programmers NEVER make assumptions
Dr. Amal Khalifa, 2014
8
Problem Solving Approach
Dr. Amal Khalifa, 2014
10
In the programming environment, the problem-solving process involves the following steps:
1. Analyze the problem and outline the problem and its solution requirements.
2. Design an algorithm to solve the problem.
3. Implement the algorithm in a programming language, such as Java.
4. Verify that the algorithm works.
5. Maintain the program by using and improving it, and modifying it if the problem domain changes.
Expressing the Algorithms
A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily
There are standards in programming:
PSEUDOCODE
FLOWCHARTS
Dr. Amal Khalifa, 2014
12
Pseudo Code
“Pseudo” means “pretend” or “false”
Pseudo Code is pretend or false computer code;
generic English-like terms that are somewhat like
computer code
Pseudo Code is not as standardized as
flowcharts, and does not facilitate the breaking
down of problems as well as a flowchart does
Dr. Amal Khalifa, 2014
13
Pseudocode (wikipedia)
Pseudocode (derived from pseudo and code) is
a compact and informal high-level description of
a computer programming algorithm that uses the
structural conventions of programming
languages, but omits detailed subroutines,
variable declarations or language-specific
syntax. The programming language is
augmented with natural language descriptions of
the details, where convenient.
Dr. Amal Khalifa, 2014
14
design an algorithm to find the perimeter and area of a rectangle
Pseudo Code Example 15
Dr. Amal Khalifa, 2014
Flowcharts
Graphical representations of algorithms
Tool to translate algorithms into software
A Flowchart uses easy-to-understand symbols to
represent actions on data and the flow of data
Flowcharts aid in breaking down a problem into
simple steps
Dr. Amal Khalifa, 2014
16
Flowchart Symbols
Oval
Parallelogram
Rectangle
Diamond
Hybrid
Name Symbol Use in Flowchart
Denotes the beginning or end of the program
Denotes an input operation
Denotes an output operation
Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Flow line Denotes the direction of logic flow in the program
Dr. Amal Khalifa, 2014
17
Example
Example 1: Write an algorithm and draw a
flowchart to convert the length in feet to
centimeter.
hint: I feet = 30 cm
Dr. Amal Khalifa, 2014
19
Pseudocode
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT
with 30
Print length in cm (LCM)
Dr. Amal Khalifa, 2014
20
Algorithm
Step 1: Input Lft
Step 2: Lcm Lft x 30
Step 3: Print Lcm
START
Input
Lft
Lcm Lft x 30
Lcm
STOP
Flowchart
Dr. Amal Khalifa, 2014
21
Example 2
Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its
area.
Dr. Amal Khalifa, 2014
22
Example 2
Pseudocode
Input the width (W) and Length (L) of
a rectangle
Calculate the area (A) by multiplying
L with W
Print A
23
Dr. Amal Khalifa, 2014
Example 2
Algorithm
Step 1: Input W,L
Step 2: A L x W
Step 3: Print A
START
Input
W, L
A L x W
A
STOP
Dr. Amal Khalifa, 2014
24
Example 3
Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
Hint: the roots are:
x1 = (–b + d)/2a and
x2 = (–b – d)/2a
where, d = sqrt ( )
2 0ax bx c
2 4b ac
Dr. Amal Khalifa, 2014 25
Example 3
Pseudocode:
Input the coefficients (a, b, c) of the quadratic equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Dr. Amal Khalifa, 2014
26
Algorithm:
Step 1: Input a, b, c
Step 2: d sqrt ( )
Step 3: x1 (–b + d) / (2 x a)
Step 4: x2 (–b – d) / (2 x a)
Step 5: Print x1, x2
Example 3
Dr. Amal Khalifa, 2014
27
4b b a c
START
Input
a, b, c
d sqrt(b x b – 4 x a x c)
x1 ,x2
STOP
x1 (–b + d) / (2 x a)
X2 (–b – d) / (2 x a)