Chapter 1
Introduction to Problem Solving
Program Development Steps
1. Definition of the problem: Given it you by the customer
2. Analysis: Describe inputs & outputs of problem and the relationship
between them (also includes requirements)
Example Problem: Find the area of a triangle
Inputs: [Link], [Link]
Output: [Link]
Relationship: Area = (Base x Height) / 2
UML Diagrams: Includes tools to perform more complex analysis and
present them
3. Design Solution of the Problem
• Involves design of ALGORITHMS
• A logical order of steps which are used to reach the solution
• 2 ways:
1. Pseudo Code: Textual way to describe an algorithm. Includes some
codes somewhere between the communication language &
programming language
2. Flow Chart: Graphical way
Pseudo Code
Fixed texts (representing a special command) and their meanings
• BEGIN
• END
• GET: used to get inputs (from where? Not important)
• PRINT: used to present outputs
• IF: used to take a decision on a condition
• GO: used to go at the beginning of another step
Example Problem: Find the area of a
triangle
Pseudo Code of the Solution:
Step-01: BEGIN
Step-02: GET base, height (they are called variables)
Step-03: Area = (Base x Height) / 2
Step-04: PRINT Area
Step-05: END
Flow Chart
• Better way to define an algorithm
• BEGIN & END BEGIN END
Condition
• GET IF (comparison)
• PRINT
• For Calculations/Assignments Connectors
Flow Chart
BEGIN
base, height
Area = (Base x Height) / 2
Area END
Example-2:
Develop an algorithm to convert a total time in seconds taken from keyboard
to its equivalent representation in hour, minute, seconds and print this
representation.
Analysis: S-05: min = total / 60
- inputs: [Link] (sec) S-06: sec = total – min*60
- outputs: [Link], [Link], 3. sec S-07: PRINT hour, min, sec
Pseudo Code: S-08: END
S-01: BEGIN
S-02: GET total
S-03: hour = total / 3600
S-04: total = total – hour * 3600
Example-2: Flow Chart
BEGIN 1
total sec = total – min*60
hour = total / 3600 hour, min, sec
total = total – hour * 3600 END
min = total / 60
1
Decisions (Condition Checking) in
Algorithms
• Pseudo Code
IF <condition> THEN <true_statements>
ELSE <false_statements>
• Flow Chart
T
condition
F
Example-3: Revised problem in Example-2
Also print whether the total time is less than a day or not
• Pseudo Code
S-01: BEGIN S-08: IF hour<24 THEN
S-02: GET total PRINT "Total time is less than a day"
S-03: hour = total / 3600 ELSE PRINT "Total time is NOT than a
S-04: total = total – hour * 3600 day"
S-05: min = total / 60 S-09: END
S-06: sec = total – min*60
S-07: PRINT hour, min, sec
Example-3: Flow Chart
BEGIN 1
total sec = total – min*60
hour = total / 3600 hour, min, sec
total = total – hour * 3600 T "Total time is less
hour < 24
than a day"
F
min = total / 60
"Total time is
1 greater than a day"
END
Example-4: Revise area of triangle problem so that we also print a
message about whether triangle is a large, regular or a small one
Assume, large (area>500), small (area<100), o.w. regular
Pseudo Code of the Solution:
Step-01: BEGIN
Step-02: GET base, height (they are called variables)
Step-03: Area = (Base x Height) / 2
Step-04: PRINT Area
Step-05: IF area>500 THEN PRINT "This is a LARGE triangle"
ELSE IF area<100 THEN PRINT "This is a SMALL triangle"
ELSE PRINT "This is a REGULAR triangle"
Step-06: END
BEGIN
base, height
Area = (Base x Height) / 2
Area
1
1
T " This is a LARGE
Area>500
triangle "
F
T " This is a SMALL
Area<100
triangle "
F
" This is a REGULAR
triangle "
END
Example-5: Revise previous problem so that it is solved
for all height and base values until a non-positive value is
entered for one of them
Pseudo Code of the Solution:
Step-01: BEGIN
Step-02: GET base, height (they are called variables)
Step-03: Area = (Base x Height) / 2
Step-04: PRINT Area
Step-05: IF area>500 THEN PRINT "This is a LARGE triangle"
ELSE IF area<100 THEN PRINT "This is a SMALL triangle"
ELSE PRINT "This is a REGULAR triangle"
Step-06: IF base > 0 AND height > 0 THEN GO Step-02
Step-07: END
Classroom Exercise: Solve by yourself!
Develop an algorithm that takes an integer input
and prints a proper message regarding that
integer is a positive number, negative number or
zero
S-01: BEGIN
S-02: GET number
S-03: IF number > 0 THEN PRINT "It is a positive number"
ELSE IF number < 0 THEN PRINT "It is a negative number"
ELSE PRINT "It is ZERO"
S-04: END
Flow Chart
1
BEGIN
"It is ZERO"
number
T
" It is a positive END
number > 0 number "
F
T " It is a negative
number < 0 number "
F
1
Program Development Steps
4. Implementation: Coding
5. Testing: Debugging
6. Deployment
7. Maintenance
Example: Your solution works on android devices (2014: Android 4)
For new versions of Android, you need to revise your solution
Steps of Programming (Coding)
1. Creating a Source File (Text Files: Notepad, gedit, etc.)
Filename: [Link]
Possible extensions:
- Powerpoint: pptx
- Word: docx
- Executable files: exe
- Standart Text Files: txt
- C source files: C
- Java source files: java
- Python source files: py
Steps of Programming (Coding)
2. Compilation: Error checking is done
If no error -> an object file is created (.obj)
Types of Error:
a) Compile-time error: occurs if statements in source file are not valid
w.r.t. Syntax
b) Run time errors: example, division by zero
c) Logical errors: algorithmic
Steps of Programming (Coding)
3. Linking: done by Linker program
Linker includes some predefined programs&resources to your object
file so that it becomes an executable (exe) file
4. Calling: involves using of your solution which presented as an
executable file
For Linux:
./filename