Professional Documents
Culture Documents
Lecture 1
Programming (1)
1 2021-2022
Lecture Outlines
Types of Problems
Problem Solving Steps
Specification of needs
Problem analysis
Design and algorithmic representation
Implementation
Testing and verification
Documentation
Problem Solving Strategies
Algorithm Design Techniques
Review Flowcharts
Programming (1)
1 2021-2022
Introduction
Last
Think step !!
Design
Algorithm
Write Code
Introduction to Programming
Brainstorming
Abstraction
Documentation
At this stage, you should also determine the format in which the
results should be displayed (for example, as a table with specific
column headings) and develop a list of problem variables and
their relationships.
If Steps 1 and 2 are not done properly, you will solve the wrong
problem.
Compute and display the total cost of apples given the number of
pounds of apples purchased and the cost per pound of apples.
Next, summarize the information contained in the underlined phrases:
Problem Inputs:
Problem Output:
Once you know the problem inputs and outputs, develop a list of
formulas that specify relationships between them.
Brute-Force:
◦ Try all possibilities
Don’t attempt to solve every detail of the problem at the
beginning; instead, discipline yourself to use top-down design.
Greedy:
Find the local optimal step at each stage.
Not necessarily globally optimal.
• Example:
With a goal of reaching the largest-sum, at each step, the
greedy algorithm will choose what appears to be the
optimal immediate choice, so it will choose 12 instead of 3
at the second step, and will not reach the best solution,
which contains 99.
14
1 -5
7
5 4
6
Example
2
4
3
d ( 2)
45 12
1 8
3
2
14
1 -5
7
5 4
6
Example
2
4
3 d ( 3)
45 6
1 8
3
2
14
1 -5
7
5 4
6
Computer Instructions
Input?
Point 1 coordinate
Point 2 coordinate
Output?
Distance between points
Constraint/condition?
Nil
Formula/equation?
Pythagorean theorem
Major Task:
1.Read Point 1 coordinate
2.Read Point 2 coordinate
3.Calculate distance
4.Display the computed distance
However, looking at the above algorithm, we can still further refine step
3, by introducing the formula to calculate the distance.
After refinement:
1.Read Point 1 coordinate
2.Read Point 2 coordinate
3.Distance = (𝑠𝑖𝑑𝑒1)2 + (𝑠𝑖𝑑𝑒 2)2
4.Display the computed distance
Programming (1) 2021-2022 35
Remember, the order of the steps in algorithm is
Programming (1)
36 2021-2022
Implementation
You may return to this program in future to use the whole of or a part of it
again.
Other programmer or end user will need some information about your
program for reference or maintenance.
You may someday have to modify the program, or may discover some
errors or weaknesses in your program.
Programming (1)
41 2021-2022