Week 2
Week 2
Week 2
Programming
WHAT IS ALGORITHM?
WRITE AN ALGORITHM THAT WILL COMPUTE FOR THE SUM OF TWO NUMBERS.
IPO – INPUT-PROCESS-OUTPUT
TERMINOLOGIES
VARIABLES - is a symbol which works as a placeholder for expression or quantities that may vary or
change
number1+number2 = sum
+ , -, *, / MDAS 5 + 8 = 13
A+B = EXPRESSION
WRITE AN ALGORITHM THAT WILL COMPUTE FOR THE SUM OF TWO NUMBERS.
IPO
3. WRITE AN ALGORITHM THAT WILL DETERMINE IF THE INPUT AGE IS QUALIFIED TO VOTE OR
NOT.
1 VARIABLE
4. WRITE AN ALGORITHM THAT WILL COMPUTE FOR THE AVERAGE OF THREE QUIZZES.
4, 5
6. Write an algorithm that will compute for the sum, difference, product, and quotient of 4 numbers.
Sum
Difference
Product
Qoutient
1st number
2nd number
3rd number
4th number
7. Write an algorithm that will determine the year level of a student. If the user input 1, it will display Junior, 2 for
Sophomore, 3 for Junior and 4 for Senior.
Year-level = 4
8. Write an algorithm that will accept values based on the following entries:
Name:
Age
Course
Address
Year-Level
MACHINE PROBLEM
10. WRITE AN ALGORITHM THAT WILL DISPLAY PROGRAM LOGIC DESIGN 5 times
Algorithm
1. WRITE AN ALGORITHM THAT WILL COMPUTE FOR THE SUM OF TWO NUMBERS.
Input-Process-Output
Step 1: Start/Begin
Step 2: Input two number (number1,number2)
Step 3: Compute for the sum of two numbers
(sum = number1 + number 2)
Step 4: Display “Sum”
Step 5: End/Stop
Algorithm
2. WRITE AN ALGORITHM THAT WILL COMPUTE FOR THE PRODUCT OF 3 INPUT NUMBERS
Step 1: Begin
Step 2: Input three numbers (num1, num2, num3)
Step 3: Compute for the product of three numbers
(product=num1*num2*num3)
Step 4: Display “Product”
Step 5: End
Algorithm
Step 1: Start
Step 2: Display “Programming I”
Display “Programming I”
Display “Programming I”
Display “Programming I”
Step 3: End
Algorithm
Step 1: Start
Step 2: Input three quizzes (quiz1,quiz2,qui3)
Step 3: Compute for the average of three quizzes
average=(quiz1+quiz2+quiz3)/3
Step 4: Display “Average”
Step 5: End.
Notations Commonly Used in Flowcharting
A flowchart is a diagram that depicts the actions and decisions required to complete a process. A diagram shape
is used to represent each step in the sequence. Connecting lines and directional arrows connect the steps. This
allows anyone to look at the flowchart and follow the procedure from start to finish.
Symbols & Functions
Symbol: Terminal
START/BEGIN END/STOP
Functions:
Indicates when the program, process, or interrupt program begins and ends.
Symbols & Functions
Symbol: Input
READ NUM1,
N2 INPUT N1, N2 ACCEPT N1, N2
Functions:
Any input/output operation can be performed with this symbol. This indicates that the computer will get
data
Symbols & Functions
Symbol: Output
Functions:
Any input/output operation can be performed with this symbol. This indicates that the computer will
produce results.
Symbols & Functions
Symbol: Process
Functions:
Any form of internal operation within the processor or memory is indicated by this symbol.
Symbols & Functions
Functions:
Functions:
Functions:
Used to indicate that the flow is continuing on a separate page in the flowchart. As labels, capital letters are
usually utilized in the symbol.
Symbol:
Predefined Process
Y
ACADEMIC ACADEMIC B
SUBJECTS?
SUBJECTS
N
Symbols & Functions
Symbol: Database
STUDENT EMPLOYEE
DATABASE DATABASE
Functions:
Indicates a list of data with a consistent structure that can be searched and sorted.
WRITE AN ALGORITHM AND DRAW A FLOWCHART THAT WILL COMPUTE FOR THE SUM OF TWO
NUMBER.
START
STEP 1:START
STEP 2:INPUT TWO NUMBERS (NUM1, NUM2)
COMPUTE FOR THE SUM OF TWO INPUT TWO
NUMBERS (NUM1,
NUMBERS (SUM=NUM1+NUM2) NUM2)
DISPLAY
“SUM”
END
START Symbols & Functions
INPUT AGE
Step 1: Start
Step 2: Input Age
If Age >= 18
Step 3: Display “Allowed to
IF AGE Y DISPLAY Vote”
>=18 “ALLOWED A Else
TO VOTE” Display “Not Allowed to Vote”
Step 4: End.
N
DISPLAY
“NOT
ALLOWED TO
VOTE”
STOP A
START Symbols & Functions
IF Y DISPLAY
AVE>= A
“PASSED”
75
DISPLAY
“FAILED” STOP A
PROBLEM 1. Create an algorithm and flowchart to compute the corresponding area of the circle and
print out the value of the input radius and the area.
Algorithm:
START
A
pi=3.1416
Print Radius
and Area
Accept radius
(R)
A
End
Area = pi*R*R
Flowchart
A
START
Y
If R = 0
Accept integer Print “Even”
(N) N
A
PROBLEM 3. Create an algorithm and flowchart that that will read scores of 10 students for a particular
examination. Also, determine the average score, and print it out.
Algorithm:
Step 1: Start
Step 2: Initialize counter and sum to zero (ctr = 0, sum =0)
Step 3 Read in the student’s score (score)
Step 4: Add the value of score to sum (sum = sum + score)
Increment counter by 1(ctr = ctr +1)
If the counter is less than 10
then Go to step 2
Compute for the average (Ave = sum/10)
Step 5: Display Average.
Step 6: End
START
Flowchart ctr = 0
A
Sum = 0
End
ctr = ctr + 1
N
If ctr = 0 A
Y
Loop Control
1. Initialization – the value of a counter used is initially set equal to zero (or one). The process is
always done outside the loop.
2. Test for limit condition – before logic flow gets out of a loop, a loop-terminating condition must first
be satisfied. The process is usually found at the beginning or at the end of a loop
3. Incrementation – after each loop is executed. 1 is added to thecounter variable. Thus the counter
reflects the number of times the operation has been performed.
PROBLEM 3. Create an algorithm and flowchart that that will read scores of 10 students for a particular
examination. Also, determine the average score, and print it out.
Algorithm:
Start
Read the employee’s name and salary
Check if the employee’s salary is less than 10,000.00 pesos
If the salary is less than 10,000.00 pesos
then bonus is 100% of the salary
else bonus is 50% of the salary
Print out the employee’s name and bonus
End
START
Flowchart
Read Name,
Sal
If Sal <10,000
Y
Bonus = Sal
Print Name,
End
Bonus
Problem 4. Create an algorithm and flowchart that will compute for the choice of sum, difference or
product of two input integers.
SUM
Algorithm
Compute for sum (sum = x + y)
Start Return the value of sum
Input two integers (x, y)
Input choice (ch) (s for sum, d for difference, and p for product) DIFFERENCE
If choice is equal to ‘s’
then call sum Compute for difference (difference = x-y)
If choice is equal to ‘d’ Return the value of difference
then call difference
If choice is equal to ‘p’ PRODUCT
then call product
Print the result Compute for product (product = x*y)
End. Return the value of product
START
A
Flowchart Input x, y
Y DIFFERENC
If ch = ‘d’
Input choice E
Y N C
If ch = ‘s’ SUM
Y
N If ch = ‘p’
PRODUCT
B
A
N D
END E
Flowchart
Sum = x + y
Display
“Sum”
E
Flowchart
Difference = x - y
Display
“Difference”
E
Flowchart
Product = x * y
Display
“Product”
E
PROBLEM 2. Create an algorithm and flowchart that accept an integer and determine if the input integer
is an ODD or EVEN
Algorithm:
Write an algorithm and draw a flowchart that will compute for the product of three input numbers
Create an algorithm and flowchart that accept an integer and determine if the input integer is an ODD or
EVEN