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

c2_problemsolvingandalgorithm

Uploaded by

ai240024
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c2_problemsolvingandalgorithm

Uploaded by

ai240024
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Chapter 2

Problem Solving and Algorithm

BIC 10204 : ALGORITHM AND PROGRAMMING


Introduction to Computer Programming

PROGRAMMING COMPONENT

HISTORY

PROGRAMMING LANGUAGES DEVELOPMENT LIFECYCLE


Introduction to Computer Programming

Maintai
Proble
n&
Update m

Test & Analysi


Verify s

Implem
ent Design
DEVELOPMENT LIFECYCLE
Gain a clear understanding of what are
required for its solution.
Problem
State the problem clearly

What is the problem?

PROBLEM
Is it possible to solve the
problem with
programming?
EXAMPLE 1

Problem

Identify the problem

PROBLEM Given the value of x is 10 and a is 12,


find the result of the following equation:
y = 2x + a - 6

How to solve the


problem?
EXAMPLE 1
Given the value of x is 10 and a is 12,
find the result of the following equation: Problem
y = 2x + a - 6

SOLUTION Proble Find the value of


m: y.
Data: x = 10, a = 12
Solution Calculate y
:
Analysis

Input
data

Analyze
IPO Process
requirements/constraints
problem

Output
desired results
EXAMPLE 1
Given the value of x is 10 and a is 12,
find the result of the following equation: Analysis
y = 2x + a - 6

Input
data x = 10, a =
12
Process
Requirements/
Replace x and a
Analyze constraints y = 2x + a - 6
problem
Output
Value of
Desired results
y
Uncle Chen wants to buy 5 cans of paint from
Linda’s shop. The price for each of the paint can is
RM 15.60. Calculate the total price of all Example 2
the paint cans.

PROBLEM ANALYSIS
Problem What is the Input 1 can paint =
price for 5 cans RM 15.60
of paint? No. of cans = 5
Data 1 can paint = Process Total price cans of
RM 15.60 paint = RM 15.60 x
5
Solution Calculate the Output Total price 5
total price cans of paint
Madam Sue needs to determine her students grade for

Example 3
programming subject based on the mark scored during final
examination. The ‘A’ grade will be given if the mark scored is
between 85 to 100. If a student has scored 90 marks, what is the
grade should Madam Sue give to the student?

PROBLEM ANALYSIS
Problem What is the grade Input Mark = 90
for student who
scored 90 marks
Data Process A = if 85 < mark
Student marks
< 100
Solution Determine Output Grade for
grade based students with 90
on marks marks
Exercise 1

Given the following formula, calculate the porosity of rock pure


volume = 12.5 while total rock volume = 55:

porosity = (pure volume /total rock volume) x 100%

Specify and analyze the problem


Exercise 2

Compute and display the total cost of apples,


if given the number of pounds of apples
purchased and the cost of apples per pound
Design

Solve
Algorith List of problem
Design m steps
algorithm

devel Verify it solves the


op problem
Real System Example

List of steps
• Insert ATM card
• Select language

BAN • Enter pin


number
• Select
K withdrawal
• Enter amount
ATM of money to
withdraw
• Get returning
bank
Techniques to Design

Pseudocode Flowchart
Simple codes which
A text-based
resembles program
design tool that
code that will be
Natural Language helps programmers
written during
to develop
implementation phase
algorithm
(program coding)

Pseudocode
Consists of a statement of instructions in sequence.

Every steps consists of keyword which explain the actions/


operation/process to be executed by computers

Pseudocod
Every steps should be written in different steps, if continued,
the next row must be indented.

e Rules Can use if/else for conditional statement and while, do/while
and for repetition statement

Every steps should use clear statements and easy to


understand.

Use start to represent the beginning of the operation and end


to show the end of the operation.
Given the value of x is 10 and a is 12,
find the result of the following equation: Example 1
y = 2x + a - 6

Input Pseudocod
data
e?
x = 10, a = 12

Process
start
Requirements/ read the value of x
constraints
read the value of a
Replace x and a
y = 2x + a - 6 compute the value of y as y = 2x + a -6
display/print the value of y
Output
Desired results
end

Value of y
Uncle Chen wants to buy 5 cans of paint from
Linda’s shop. The price for each of the paint can is
RM 15.60. Calculate the total price of all Example 2
the paint cans.

Pseudocod
ANALYSIS e?
Input 1 can paint = start
RM 15.60
set price as 15.60
No. of cans = 5
read the number of cans
Process
Total price cans of compute the total price as price per can
paint = RM 15.60 x times by number of cans
5
Output display/print the total price
Total price 5
cans of paint end
Madam Sue needs to determine her students grade for

Example 3
programming subject based on the mark scored during final
examination. The ‘A’ grade will be given if the mark scored is
between 85 to 100. If a student has scored 90 marks, what is the
grade should Madam Sue give to the student?

Pseudocod
ANALYSIS e?
Input Mark = 90
start
read the student mark
Process if mark is greater than 85 and mark
A = if 85 < mark is less than 100, then
< 100 set grade as A
Output display/print the grade
Grade for
students with 90 end
marks
Given the following formula, calculate the porosity of rock if the pure
volume = 12.5 while total rock volume = 55:

porosity = (pure volume /total rock volume) x 100% Exercise 1


Specify and analyze the problem

Pseudocod
ANALYSIS e?
Input Pure volume = start
12.5 total rock read the number of pure volume
volume = 55 read the total rock volume
Process Porosity = (pure compute the value of total porosity as
volume/total rock total porosity = (number of
volume) x 100%
pure volume divided by total rock volume)
Output Porosity of the given
pure volume and
times by 100%
total rock volume display/print the total porosity
end
Compute and display the total cost of apples,
if given the number of pounds of apples
purchased and the cost of apples per pound
Exercise 2

Pseudocod
ANALYSIS e?
Input No. of pounds of start
apples read the number of apples purchased
purchased and read the cost of apples per pound
Process costcost
Total perof
pound
apples compute the total cost as number of
= no. of pounds x apples purchased
cos per pound
times by cost of apples per pound
Output Total cost of apples for the
given no. of pounds of display/print the total cost
apples and the cost per
pounds
end
A box has height, width and length.
Write the pseudo code to calculate the volume
of a box.
Exercise 3

Pseudocod
ANALYSIS e?
Input height start
width read the height
length read the width
Process Box volume = read the length
height x width x compute the box volume as height
length
times by width times by length
Output Total volume of display/print the total volume of box
box end
Write the pseudo code to calculate salary of
an employee for a month. Exercise 4

Pseudocod
ANALYSIS e?
Input Salary per day
start
No of day per
month read the salary per day
read the no. of day per month
Process Salary = salary per
day x no. of day per compute the total salary as salary per day
month times by no. of day per month
Output Total salary display/print the total salary
end
1. Helps to visualize
the content/steps
better or to find flaws Schematic
in process Symbol representation of an
algorithm or process
2. Illustrate the steps in
the process

Flowchart
Start/Stop
The terminator symbol marks
the starting or ending point of
the system. It usually contains
the word "Start" or "End."

Action or Process
A box can represent a single
step ("add two cups of flour"), or
and entire sub-process ("make
bread") within a larger process.

Flowchart Symbols
Decision
A decision or branching point.
Lines representing different
decisions emerge from
different points of the diamond.
Input/Output
Represents material or
information entering or leaving
the system, such as customer
order (input) or a product
(output).

Flowchart Symbols
Flow Line
Lines indicate the sequence of
steps and the direction of flow.

Connector
Indicates that the flow
continues where a matching
symbol (containing the same
letter) has been placed.

Flowchart Symbols
Off Page
Indicates that the process
continues off page.

Database
Indicates a list of information with
a standard structure that allows
for searching and sorting.

Flowchart Symbols
Example
Flowchar
t
Given the value of x is 10 and a is 12,
find the result of the following equation: Example 1
y = 2x + a - 6

Pseudocod
e?
start
read the value of x
read the value of a
compute the value of y as y = 2x + a -6
display/print the value of y
end
Uncle Chen wants to buy 5 cans of paint from
Linda’s shop. The price for each of the paint can is
RM 15.60. Calculate the total price of all Example 2
the paint cans.

Pseudocod
e?
start
set price as 15.60
read the number of cans
compute the total price as price per can
times by number of cans
display/print the total price
end
Madam Sue needs to determine her students grade for

Example 3
programming subject based on the mark scored during final
examination. The ‘A’ grade will be given if the mark scored is
between 85 to 100. If a student has scored 90 marks, what is the
grade should Madam Sue give to the student?

Pseudocod
e?
start
read the student mark
if mark is greater than 85 and mark
is less than 100, then
set grade as A
display/print the grade
end
Given the following formula, calculate the porosity of rock if the pure
volume = 12.5 while total rock volume = 55:

porosity = (pure volume /total rock volume) x 100% Exercise 1


Specify and analyze the problem

Pseudocod
e?
start
read the number of pure volume
read the total rock volume
compute the value of total porosity as
total porosity = (number of
pure volume divided by total rock volume)
times by 100%
display/print the total porosity
end
Compute and display the total cost of apples,
if given the number of pounds of apples
purchased and the cost of apples per pound
Exercise 2

Pseudocod
e?
start
read the number of apples purchased
read the cost of apples per pound
compute the total cost as number of
apples purchased
times by cost of apples per pound
display/print the total cost
end
A box has height, width and length.
Write the pseudo code to calculate the volume
of a box.
Exercise 3

Pseudocod
e?
start
read the height
read the width
read the length
compute the box volume as height
times by width times by length
display/print the total volume of box
end
Write the pseudo code to calculate salary of
an employee for a month. Exercise 4

Pseudocod
e?
start
read the salary per day
read the no. of day per month
compute the total salary as salary per day
times by no. of day per month
display/print the total salary
end
REFERENCES

1. Tan, H.H. dan D’Orazio, T.B. 1999. “C Programming for Engineering


& Computer Science”. McGraw-Hill International Editions. Call
number: QA76.73 .C15 .T36 1999.
2. Hanly, J. R. dan Koffman, E.B. 2010. “Problem Solving and Program
Design in C ”. Pearson Education.
3. Deitel, P. J. & Deitel H.M. 2010. “C How to Program”. 6th ed..
Pearson Education Inc. Call number: QA76.73.C15 .D45 2010.
4. Goyal, A. K. 2008. “The C Programming Language”. Alpha Science.
Call number: QA76.73.C15 .G69 2008.

You might also like