ProgrammingAlgorithm - BCK INFORMATION TECHNOLOGY

advertisement
Topics:
Developing Algorithm
What is an Algorithm?
An algorithm is the step-by-step instructions
required to obtain the solution to a problem.
Attention should be paid to the sequence of
operations in order to arrive at the correct
solution.
Characteristics of an Algorithm
An algorithm must:
 have a finite number of steps
 be precise
 be unambiguous
 have a flow of control from one process to another
 be terminated
 have a set of rules
Data
Data is fact and figures that can be categorized as:
 Constant – Data that is always the same value and
does not change, for examples pi, number of minutes
in an hour, number of days in a week, number of
meters in a kilometer, etc.
 Variable – Data that changes or varies, for example
price of an item, salary of an employee , the age of a
person, etc.
Data Type
Data type can be:
 Integer (examples: 12, 356, 5678 …)
 Characters
Char (examples: c, t, y, w …)
o String (examples: Mary, student, credit …)
 Float point /Real (example 12.34, 234.065 …)
o
Representations of algorithms
An algorithm can be represented by using one of the
following:
 Pseudocode - A Pseudocode is short, English
phrases used to express specific tasks within an
algorithm .
 Flowchart - A flow chart is a graphical or
symbolic representation of an algorithm.
An algorithm is made up of input, processing, output
and storage statements.
Data Input and Storage
 Data to be processed must be entered into the
computer and stored. Such data can be entered from
the keyboard.
 As the data is entered into the computer, it is stored in
the computer’s memory in variables or it may be stored
in a table so that it can be access when needed for
processing.
Input Statement/Instruction
 The command that is use to enable input is the word
“Read”, which is followed by the variable name.
 Example:
 Read price
 Read quantity
 While entering data, messages may appear on the screen
notifying the user of what data is to be entered. These
messages are called prompts or captions. Prompt
statements begin with the command “Print” or “Write”.
 Example:
 Print “Enter price of item”
 Print “Enter quantity purchased”
Printing Information
The output statement/instruction
 Output is needed in most programs. Output can
be used to send information or data to the screen
or the printer.
 The command that is used to produce the output
is the word “Print” or “Write”, which is followed by
the data or information to be printed.
 Examples:
Print amountdue
 Print “My first pseudocode”
 Print “Amount due is ”, amountdue

Processing Statements
Processing may involve:
 Calculation
 Selection statements (IF)
 Loop Statements (FOR and WHILE)
Processing Involving Calculations
• Calculation can be done by using the arithmetic operations
o Addition (+)
o Subtraction (-)
o Multiplication (*)
o Division (/)
• When calculations are to be performed, numeric variables
or numeric constants must be used. The result is usually
stored in a variable for future use.
• Examples:
Amountdue = price * quantity
Sum = fristno + secondno
Inches = feet * 12
Problem 1
Write a computer program to input
the price of an item and the quantity
purchased. The program should
compute and print the amount due.
Identifying Operations
The activity can be broken down into four operations as
follows:
 Input the price
 Input the quantity
 Calculate the amount due
 Printing the amount due
IPO Chart
Input
Price
Processing
Calculate the amount due
(amountdue = price *
Quantity quantity)
Output
Amountdue
Pseudocode
START
Declare price, quantity, amountdue as real
Print “A program that accepts price and quantiyt and
then calculates the amount due”
Prompt or caption
Print “Enter price of item: ”
Read price
Input and storage
Statement
Print “Enter quantity purchased: ”
Processing and storage
Read quantity
Statement
amountdue = price * quantity
Print “Amount due is ”, amountdue
STOP
Output Statement
Flowchart
Some of the symbols used are:
 Start or stop symbol
 Input or output symbol
 Process symbol
 Flow line
Flowchart
START
Read price,
quantity
Amountdue =
price * quantity
Print
amountdue
STOP
Problem 2
Read the regular price of an item;
compute discount amount at 20% of
the regular price and the item’s
discounted price. Display the
discount amount and the discounted
price
Operations
The operations are:
 Input regular price
 Calculate discount amount
 Calculate discounted price
 Print discount amount
 Print discounted price
IPO Chart
Input
Processing
Regularprice Calculate the discount
amount (discount =
regularprice * 0.2)
Calculate the discounted
price (discountedprice =
regularprice – discount)
Output
Discount
Discountedprice
Pseudocode
Flowchart
Topics:
Practice Questions
Practice Questions
1. Write an instruction that prompts the user to enter
the name of a student.
 Answer: Print “Enter name of student: ”
2. Write an instruction to read the name of a student.
 Answer: Read name
3. Write an instruction to input the age of a student.
 Answer: Read age
Practice Questions
4. Write an instruction to display the name of a student.
 Answer: Print “Student’s name is ”, name
5. Write an instruction to calculate the sum of two numbers.
 Answer: Sum = number1 + number2
Practice Questions
1. Write an instruction that asks the user to enter the height
of a student.
2. Write an instruction to read the height of a student.
3. Write an instruction that prints the height of a student.
4. Write an instruction to compute the average of three
grades .
5. Draw the flowchart symbol to represent the instructions
numbered 1 – 4 above.
Practice Questions
6. Input the length and width of a rectangle. Calculate
and display the area and perimeter of the rectangle.
Print the length and width entered for the rectangle.
Requirements:
a) List the operations needed to solve this problem
b) Draw the IPO chart
c) List the variable
d) Write the pseudocode
e) Draw the flowchart
Topics:
Quiz - Solution
1. Write an instruction to read the number of
students in a class.
Read studnum
2. Write the instruction to display the number of
students in a class.
Print “Number of students is ”, studnum
3.
Write the instruction to prompt the user to
enter the name of a fruit.
Print “Enter the name of a fruit”
4. Write an instruction to calculate the difference
between two numbers
difference = number1 – number2
A patient insurance company paid 80%
of her total medical bill.
Input the
name and total medical bill for the
patient.
Calculate and display the
amount paid by the insurance company
and the amount paid by the patient.
Print the name and total medical bill for
the patient.
Operations are:
 Input name of patient
 Input medical bill for patient
 Calculate amount paid by insurance company
 Calculate amount paid by patient
 Print name of patient
 Print medical bill for patient
 Print amount paid by insurance company
 Print amount paid by patient
IPO Chart
Input
Processing
name
Calculate amount paid by insurance
company
med_bill (ins_paid = med_bill * 0.8)
Calculate amount paid by patient
(pat_paid = med_bill – ins_paid)
Output
name
med_bill
ins_paid
pat_paid
Variable
 name
 med_bill
 ins_paid
 pat_paid
Pseudocode
START
Declare name as String
Declare med_bill, ins_paid, pat_paid as Read
Print “A program that accepts and display the name and medical bill for a
patient. Calculate and display amount paid by insurance company and
patient ”
Print “Enter name of patient: ”
Read name
Print “Enter medical bill for patient: ”
Read med_bill
ins_paid = med_bill * 0.8
pat_paid = med_bill – ins_paid
Print “Patient’s name is ”, name
Print “Medical bill for patient is ”, med_bill
Print “Amount paid by insurance company is ”, ins_paid
Print “Amount paid by patient is ”, pat_paid
STOP
Flowchart
START
Read name,
med_bill
Ins_paid =
med_bill * 0.8
pat_paid =
med_bill – ins_paid
Print name,
med_bill, ins_paid,
pat_paid
STOP
Statements in Pseudocode
 Prompt
Print “Enter name of patient ”
 Input
Read name
 Processing
ins_paid = med_bill * 0.8
 Output
Print “Patient’s name is ”, name
 Problem:
A tax of 17% is added to the price of an item. Input the name
and price of the item. Display the name and price of the
item. Calculate and display the tax amount and the price
after tax for the item.
 Requirements
a) List the operations needed to solve the problem
b) Draw the IPO chart
c) State the variables that will be used
d) Write the Pseudocode
e) Draw the flowchart
f) Identify the follow statements in Pseudocode in part (d)
(i) Prompt (ii) Input (iii) Processing
(iv) Output
Topics:
Selection Statements
Processing Involving Selecting
Instructions
 An instruction that allows deviation and selection to
take place uses the ‘IF’ command.
 The IF statement contains a condition that is made up
of three parts:
 Variable to be tested
 Relational operator
 Variable or constant to which the variable to be
tested is compared to.
Relational Operators
If statements:
 IF-THEN
 IF-THEN-ELSE
Flowchart Symbols:
 Decision making and branching
 Connector or joining two parts of a program
IF-THEN
 When one option is available and a selection
may or may not be made, the IF-THEN is used.
Pseudocode
IF (condition) then
One or more instructions which will be
carried out if the condition is true
ENDIF
Flowchart
Condition
Statement - True
Examples IF-THEN
 Problem 1:
If a student’s grade is greater than 85%, then she is
given 3 merits.
 Solution:
Pseudocode
IF (grade > 85) then
merit = 3
ENDIF
Flowchart
grade >
85
merit = 3
 Problem 2:
Read the time. If the time is 11:00, output “Ring the
bell”.
 Solution
Pseudocode
Read time
IF (time = 11) then
Print “Ring the bell”
ENDIF
 Problem 3:
Read a number N. If N is greater than or equal to
100, add 10 to the number and display the result.
Print the number.
 Solution:
Pseudocode
Read N
IF (N >= 100) then
result = N + 10
Print “Result is ” result
ENDIF
Print “Number is ” N
IF-THEN-ELSE
 When two options are available and a selection must
be made, the IF-THEN-ELSE is used.
 Pseudocode
IF (condition) then
One or more instructions which will be carried
out if the condition is true
else
One or more instructions which will be carried
out if the condition is false
ENDIF
 Flowchart
Condition
Statement - True
Statement - False
Examples IF-THEN-ELSE
 Problem 1:
If sales is greater than $7000, commission is $500
otherwise commission is $250
 Solution:
Pseudocode
IF (sales > 7000) then
commission = 500
else
commission = 250
ENDIF
Flowchart
Sales
> 7000
Commission = 500
Commission = 250
Examples IF-THEN-ELSE
 Problem 2:
Read the age of a person. If age is greater than 35,
output “old person” otherwise output “young person”
 Solution:
Pseudocode
Read age
IF (age > 35) then
Print “Old person”
else
Print “Young person”
ENDIF
 Problem 3:
Input employee’s salary. If the salary is less than or equal
to $44000, then a bonus of 15% is given, otherwise 10%
is given. Calculate and display the bonus amount given.
 Solution:
Pseudocode
Read salary
IF (salary <= 44000) then
bonus = salary * 0.15
else
bonus = salary * 0.10
ENDIF
Print “Bonus amount is ” bonus
Topics:
Loop Statements
344
 Problem 3:
If the price of an item is greater than or equal to
$500, then a discount of 20% is given. Calculate
and display the discount amount.
 Solution
IF (price >= 500) then
discount = price * 0.2
Print “Discount amount is ” discount
ENDIF
Download