Computer Programming
Lecture 2 (Part I)
Outlines
• Problems
• Common Steps in Problem Solving
• Solution Representations
• Pseudocode
• Flowchart
• Solving sequential computational problems
• Selection
Problems
• Problems are something the result are not readily available.
Common Steps in problem solving
• Understanding the problem at hand precisely.
• Looking for possible similar or related solutions at hand. (Gathering information about the
problem)
• Specifying possible of set of solutions
• Selecting the best possible solution
• Implementing and evaluating the solution
• Monitoring the solution to ensure it is effective
Representing a solution
• A solution to a problem needs to be carefully represented by outlining each individual steps of a
solution precisely so that it can be understood, communicated, and reproduced by other
interested parties.
• A computational solution can be represented:
• Pseudocode: is a text-based detail design tool that allows the programmer to represent the
implementation of an algorithm in a way that can be understood by anyone with basic programming
knowledge.
• Flowchart: is a diagrammatic representation of the steps of an algorithm.
• Source code: a source code can be used to represent a computational solution using programming
language.
Pseudocode
• Pseudocode is an informal language that helps programmers develop algorithms.
• Common instructions in a program:
• Input: get data from keyboard, file, network and so on.
• Output: displaying the output on screen, save it in a file, send it over the network, and so on.
• Math: perform mathematical operations
• Sequential execution: execute instructions in orderly manner
• Conditional execution: check for certain condition and run the appropriate code.
• Repetition: perform an action repeatedly
Exercise
Write a step by step instruction for cooking pasta with tomato sauce
Pseudocode
Goal: Computing hourly wage of an Goal: Compute the area of a circle
employe
Input hourly_rate, total_hours Input radius
Compute wage <- hourly_rate * Compute area <- PI * radius * radius
total_hours
Output wage Output area
Stop
Stop
Pseudocode Exercise
Write a pseudocode for the following computational problems:
- Compute circumference of a circle (2 * PI * radius)
- Compute the area of a rectangle
- Compute the average of four numbers
Flowchart
• Flowchart is a diagrammatic representation of the steps of an algorithm.
• Flowchart types:
• Program flowchart: They contain the steps of solving a problem unit for a specific result.
• System flowchart: contain the solutions of many problem units together that are closely related to each
other and interact with each other to achieve a goal.
Program flowchart
• A program flowchart is an extremely useful tool in program development.
• Any error or omission can be more easily detected from a program flowchart than it can be from a
program because a program flowchart is a pictorial representation of the logic of a program.
• Program flowchart can be followed easily and quickly.
• It serves as a type of documentation, which may be of great help if the need for program modification
arises in future.
Flowchart Symbols
Symbol Name and descriptions
Terminal: used to show the beginning and end of
a set of computer-related processes
Input/Output: used to show any input/output
operation
Computer processing: used to show any processing
performed by a computer system
Flowchart Symbols
Symbols Name and descriptions
Flow line: used to connect the symbols
Comment: used to write any explanatory statement
required to clarify something
Flowchart example
Goal: Computing hourly wage of an
employe
Input hourly_rate, total_hours
Compute wage <- hourly_rate *
total_hours
Output wage
Stop
Flowchart Exercise
Draw a flowchart for the following computations:
- Compute circumference of a circle (2 * PI * radius)
- Compute the area of a rectangle
- Compute the average of four numbers
The first five students to complete both exercises correctly get 1 bonus mark
More flowchart symbols
Symbols Name and description
Predefined processing: used to indicate any
process not specially defined in the flowchart
Document Input/Output: used when input comes
from a document and output goes to a document.
Decision: used to show any point in the process
where a decision must be made to determine
further action
More flowchart symbols
Symbols Name and description
On-page connector: used to connect parts of a
flowchart continued on the same page
Off-page connector: used to connect parts of a
flowchart continued to separate pages
Flowchart rules
• Only the standard symbols should be used in program flowcharts.
• The program logic should depict the flow from top to bottom and from left to right.
• Each symbol used in a program flowchart should contain only one entry point and one exit
point, with the exception of the decision symbol. This is known as the single rule.
• The operations shown within a symbol of a program flowchart should be expressed
independently of any particular programming language.
• All decision branches should be well-labeled.
Selection
• This process of decision-making is
implemented through a logic structure
called selection.
• A predicate (condition) is tested, if it is
true or false.
• If true, a course of action will be specified
for it.
• If false, an alternative course of action will
be specified for it
Selection
Goal: Determine if number is odd or even
Input A
Compute R <- Remainder of (A/2)
If R = 0
Then Print “It is an even number”
Else
Print “It is an odd number”
End-If
Stop
Selection
Goal: Output the maximum of two
numbers
Input A,B
If A > B
Then G <- A
Else
G <- B
End-If
Print G
Stop
Selection Exercise
- Write a pseudocode for a computation that accepts two numbers and tells if the first number is
a multiple of the second number
- Draw a flowchart for the pseudocode
The first 5 students to return correct answer for the questions get 1 bonus mark
Selection
Goal: Determine loss or profit
Input CP,SP
If CP <= SP
Then
If CP = SP
Then print “No profit or loss”
Else
Compute P <- SP – CP
print “The profit is ”, p
End-if
Else
Compute L <- CP – SP
print “The loss is ”, L
End-if
Stop
More Selection Exercise
- Write a pseudocode to determine the maximum of 3 numbers
- Draw a flowchart for the pseudocode
The first 5 students to complete the exercise with a correct answer get bonus 1 mark