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

Algorithm - Flowchart

The document discusses the fundamentals of programming and problem solving. It explains that programming involves solving problems through algorithms, which are step-by-step processes. The problem solving process involves three phases: 1) analyzing the problem by outlining requirements, designing an algorithm using tools like flowcharts and pseudocode, and testing the algorithm. 2) Implementing the algorithm in a programming language. 3) Maintaining the program if requirements change. It provides examples of analyzing a problem to convert a student mark to a letter grade and making a cup of tea.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Algorithm - Flowchart

The document discusses the fundamentals of programming and problem solving. It explains that programming involves solving problems through algorithms, which are step-by-step processes. The problem solving process involves three phases: 1) analyzing the problem by outlining requirements, designing an algorithm using tools like flowcharts and pseudocode, and testing the algorithm. 2) Implementing the algorithm in a programming language. 3) Maintaining the program if requirements change. It provides examples of analyzing a problem to convert a student mark to a letter grade and making a cup of tea.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming Fundamentals

Ch1. Problem Solving

Programming Fundamentals --> 1


Ch1. Problem solving
Problem Solving

Programming is a process of problem


solving (Problem Solution by computer)

Algorithm ?
Step-by-step problem-solving process
Solution achieved in finite amount of time

Programming Fundamentals --> 2


Ch1. Problem solving
Problem Solving Process

Phase 1 - Analyze the problem


Outline the problem and its requirements
Design (algorithm) to solve the problem ( Flow chart, pseudo code)
Algorithm tracing

Phase 2 - Implement the algorithm


Implement the algorithm in code (in Programming Language
Program)
Verify that the algorithm works

Phase 3 - Maintenance
Use and modify the program if the requirements changes

Programming Fundamentals --> 3


Ch1. Problem solving
Analyze the Problem (1)
Outline the problem and its requirements

Understand the problem


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

Programming Fundamentals --> 4


Ch1. Problem solving
Analyze the Problem (1)
Design Algorithm
1. Flowcharts
2. pseudo-code

Programming Fundamentals --> 5


Ch1. Problem solving
A Flowchart is
An algorithm graphical representation.
Written as a combination of the following graphical
notations:
Flow Chart Symbols

Start and End Selection Data


Flow

Input / output Calculation

Programming Fundamentals --> 6


Ch1. Problem solving
Pseudo-code:
<Algorithm name>
// input ? The comment lines //
// function?
// Output?
Begin
<data definition>
<actions>
End

Programming Fundamentals --> 7


Ch1. Problem solving
Analyze the Problem (1)
Algorithm Tracing
Draw flowchart
Find all possible paths
Check each path with appropriate input data
Observed Outputs not conform to the expected ones
error in the algorithm.

Programming Fundamentals --> 8


Ch1. Problem solving
Analyze the Problem (2)
Problem Example.
Convert a student mark from decimal mode to ABC mode.

Understand problem requirements


Is the problem statement complete and unambiguous?
Does program require user interaction? Input the mark
Does program manipulate data? covert mark
What is the output? The mark converted to A or B or C or
error
Is there subproblem? No

Programming Fundamentals --> 9


Ch1. Problem solving
Analyze the Problem (2)
Efficiency:
- an algorithm may work correctly but be inefficient by taking
more time and using more resources than required to
solve the problem.
- becomes more important for larger programs.

Programming Fundamentals --> 10


Ch1. Problem solving
Analyze the Problem (3)
Design Algorithm Algorithm Discovery
To discover an algorithm is to solve the problem!
1. Working backwards (modification of an old solution)
Example: Min of a and b is known. Deduce Max a and b.
2. Look for a related problem that has been solved before (similar
solutions reuse)
The precedent algorithm of mark conversion is for single student. It
may be generalized to many students
3. Stepwise Refinement (new solution)
Break the problem into several sub-problems
Solve each subproblem separately
Produces a modular structure

Programming Fundamentals --> 11


Ch1. Problem solving
Analyze the Problem (3)
Example. Making tea. Suppose we have a robot
which carries out household tasks. We wish to
program the robot to make a cup of tea. An initial
attempt at an algorithm might be:

1. Put tea leaves in pot


2. Boil water
3. Add water to pot
4. Wait 5 minutes
5. Pour tea into cup
Programming Fundamentals --> 12
Ch1. Problem solving
Analyze the Problem(3)
Design Algorithm Algorithm Discovery Strategies
Stepwise Refinement
These steps are probably not detailed enough for the robot. We
therefore refine each step into a sequence of smaller steps:

1. Put tea leaves in pot might be refined to


1.1 Open box of tea
1.2 Extract one spoonful of tea leaves
1.3 Tip spoonful into pot
1.4 Close box of tea
Programming Fundamentals --> 13
Ch1. Problem solving
Analyze the Problem(3)
Design Algorithm Algorithm Discovery Strategies
Stepwise Refinement

2. Boil water might be refined to


2.1. Fill kettle with water
2.2 Switch on kettle
2.3 Wait until water is boiled
2.4 Switch off kettle
..
5. Pour tea into cup might be refined to
5.1. Pour tea from pot into cup until cup is full
Programming Fundamentals --> 14
Ch1. Problem solving
Programming Fundamentals --> 15
Ch1. Problem solving
Analyze the Problem(3)
the designer must know when to stop refining.
In this example the instruction Switch on kettle is
directly executable by the robot, but that Fill kettle
with water is not.
Experience will tell us when a step is directly
implementable or not.
The above algorithm consists of a sequence of
steps, executed exactly once and in order

Programming Fundamentals --> 16


Ch1. Problem solving
Analyze the Problem(3)
What happens if the tea-box is empty?

Control Requirements: IF
If the tea-box is empty we wish to specify an extra step:
Get new box of tea from cupboard
This step would not be carried out unless the tea-box is empty,
We can express this by rewriting step 1.1 as
1.1.1. Take tea box from shelf
1.1.2. If box is empty
then get new box from cupboard
1.1.3. Remove lid from box
Step 1.1.2 expresses both the step to be selected and
the
condition under which this selection should be made.
More complicated conditions can use -->
Programming Fundamentals AND, OR, NOT 17
Ch1. Problem solving
Analyze the Problem(3)
Control requirements: while
Another common requirement is the need for
iteration.
Example. Repeat the mark conversion for a
class room of 30 students. Each student having a
unique student number from 1 to 30.

Programming Fundamentals --> 18


Ch1. Problem solving
Implement the algorithm
Use any programming language (C++) to write a program to solve the
problem. Where program is a sequence of statements.

Use any programming language


(C++) to write a program to solve the
problem. Where program is a
sequence of statements.

Programming Fundamentals --> 19


Ch1. Problem solving
Computer Stage: after completing the
program, the computer must do the
following:
- Compilation: the computer compile the
program. If it has errors, then an error list is
produced to be corrected by the programmer.

- Execution: when the program become


dont has errors, the computer execute it to
produce the output.

Programming Fundamentals --> 20


Ch1. Problem solving
Implement the algorithm
Verify that the algorithm works source of
errors
Many errors made in:
- analyzing the problem,
- developing an algorithm, and/or
- coding the algorithm

Programming Fundamentals --> 21


Ch1. Problem solving
Implement the algorithm

Errors are of three types:


. syntax errors
. run-time errors
. logic errors
Syntax errors: detected by the C compiler
. source code does not conform to one or more of Cs
grammar rules
. examples of syntax errors: undeclared variable,
Often one mistake leads to multiple error messages
can be confusing

Programming Fundamentals --> 22


Ch1. Problem solving
Implement the algorithm
Run-time errors. detected and displayed by computer during
execution.
- Occur when program directs computer to perform illegal
operation. Example: int x=y/0;
- will stop program execution and display message
Logic errors.
- caused by faulty algorithm
- sign of error: incorrect program output
- cure: thorough testing and comparison with expected
results
A logic error is referred to as a bug, so finding logic errors is
called debugging.
Programming Fundamentals --> 23
Ch1. Problem solving
Conclusions (Contd)

Programming Fundamentals --> 24


Ch1. Problem solving

You might also like