Week 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

Object-Oriented

Programming
WHAT IS ALGORITHM?

 An algorithm is a set of instructions for completing a task or solving a problem.


 An algorithm is a series of well-defined steps or rules that must be followed in order to
achieve a specific result.

Abu Abdullah Mahammad ibn Musa al-Khwarizmi


Rules in Writing Algorithm
 Input and output must be well specified.
 Each stage of the algorithm should be simple and straightforward.
 Among the numerous various approaches to address an issue, algorithms should be the most
effective.
 Computer code should not be included in an 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

 USER-DEFINED WORD = SHORT BUT DESCRIPTIVE


= LETTER
 OPERATOR => number1 = 2number
=last_name
 OPERANDS - A-Z =Last_Name

number1+number2 = sum
+ , -, *, / MDAS 5 + 8 = 13

A+B = EXPRESSION

WRITE AN ALGORITHM THAT WILL COMPUTE FOR THE SUM OF TWO NUMBERS.

3 variable = 1st number = number1, num1, n1


2nd number = number2, num2, n2
sum, total
2. WRITE AN ALGORITHM THAT WILL COMPUTER FOR THE PRODUCT OF 3 INPUT NUMBERS

IPO

1ST VARIABLE – 1ST NUMBER - N1, NUM1, NUMBER1


2ND VARIABLE – 2ND NUMBER - N2, NUM2, NUMBER2
3RD VARIABLE – 3RD NUMBER – N3, NUM3, NUMBER3
4TH VARIABLE – PRODUCT – PRODUCT

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

1ST NUMBER – 1ST VARIABLE - QUIZ1 AVERAGE=(QUIZ1+QUIZ2+QUI3)/3


2ND NUMBER - 2ND VARIABLE - QUIZ2
3RD NUMBER – 3RD VARIABLE - QUIZ3 SUM = QUIZ1+QUIZ2+QUIZ3
AVERAGE AVERAGE=SUM/3
SUM
5. WRITE AN ALGORITHM THAT WILL DISPLAY JRCC 5 TIMES

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

9. WRITE AN ALGORITHM THAT WILL GET THE PRODUCT OF 5 INPUT NUMBERS.

Num1, num2, num3,num4,num5,


Product

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

2. WRITE AN ALGORITHM THAT WILL display the “Programming I” 4 times

Step 1: Start
Step 2: Display “Programming I”
Display “Programming I”
Display “Programming I”
Display “Programming I”
Step 3: End
Algorithm

2. WRITE AN ALGORITHM THAT WILL compute for the average of 3 quizzes.

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

Notation Meaning Notation Meaning


+ Addition = Equal to
- Subtraction > Greater than
* Multiplication < Less than
/ Division <> Not equal to
^ Exponentiation >= Greater than or equal
mod Modulus <= Less than or equal
| Logical OR & Logical AND
What is Flowchart?

 A flowchart is a graphic representation of a procedure, system, or computer algorithm. They're frequently


utilized in a variety of fields to document, analyse, plan, enhance, and convey often complex processes in clear,
simple diagrams.

 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

PRINT “SUM” DISPLAY


“PRODUCT”

Functions:
Any input/output operation can be performed with this symbol. This indicates that the computer will
produce results.
Symbols & Functions

Symbol: Process

COMPUTE THE SUM


(SUM=N1+N2)

Functions:

Any form of internal operation within the processor or memory is indicated by this symbol.
Symbols & Functions

Symbol: Flow lines

Functions:

Shows direction of the flow


Symbols & Functions

Symbol: On Page Connector

Functions:

On a flowchart page, it's used to substitute long lines.


Symbols & Functions

Symbol: Off Page Connector

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)

STEP 3: DISPLAY “SUM”


STEP 4: END.
COMPUTE FOR THE SUM OF TWO
NUMBERS (SUM = NUM1+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

INPUT Q1, Q2, Step 1: Start


Q3 Step 2: Input Age
If Age >= 18
Step 3: Display “Allowed to
Vote”
COMPUTE FOR THE AVE Else
AVE=(Q1+Q2+Q3)/3 Display “Not Allowed to Vote”
Step 4: End.

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:

Step 1: Define the value of PI equal to 3.1416


Step 2: Accept the value of radius (R)
Step 3: Calculate the Area (A = PI * R * R)
Step 4: Print out the radius (R) and the value of Area (A)
Step 5: End.
Flowchart

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

Print “Odd” End


R = N mod 2

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

Read score Ave = sum/10

Sum = sum + score Print Average

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

Bonus = Sal * 0.50

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:

Accept an integer (N)


Get the remainder when the number is divided by 2
If the remainder is equal to 0
then Print “Even”
else Print “ODD”
End.
Quiz 2

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

You might also like