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

Lec 5 Algorithm

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

Lec 5 Algorithm

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

COMPUTER

FUNDAMENTALS AND
PROGRAMMING
James Patrick L. Galvan
Algorithm
1 Understand what algorithm is

TODAY'S 2
Understand the different algorithm
used in programming
OBJECTIVE
Understand why algorithm is
3 necessary in programming

Identify the importance in using


4 algorithm
Programming Algorithm

Set of Instructions/Rules to be followed by a computer program, which should


contain an Input, Process and an Output. Depending on the problem to be
solved.
Wen I go on
an online
shopping
spree -
Jannah
IPO Model

Input Process Output


Wen I go on
an online
shopping
spree -
Jannah
Variables

• Math:
A letter that can represent any number

• Programming: Wen I go on
an online
A letter/word that serve as a temporary storage that can represent
shopping
spree -
any value
Jannah
Programming Problem:
Create a program that will display the sum of 2 numbers
inputted by the user

Algorithm:
1. Declare 3 Variables, 2 for the Addends and 1 for the Sum.
2. Let the user Input the Addends.
3. Perform the Addition between the addends and Assign it to
the Sum.
4. Display the Sum
Pseudocode

• A Method that allows a programmer to represent the Algorithm in a more


programming related way.

• Pseudocodes are also called false codes because it tends to look


Wen I go on
like a
programming language but can still be understood by a person
an online that has little
shopping
understanding in programming spree -
Jannah
Pseudocodes: Do’s and Don’ts

DO:
Proper Naming Convention
Simple and Concise

DON’TS:
Don’t make it Abstract
Don’t make it Generalized
Programming Problem:
Create a program that will display the sum of 2 numbers
inputted by the user

Algorithm:
1. Declare 3 Variables, 2 for the Addends and 1 for the Sum.
2. Let the user Input the Addends.
3. Perform the Addition between the addends and Assign it to
the Sum.
4. Display the Sum.
Programming Problem:
Create a program that will display the sum of 2 numbers
inputted by the user

Pseudocode:
Let numOne = 0, numTwo = 0 and sum = 0
Input numOne and numTwo
sum = numOne + numTwo
Output sum
Programming Problem:
Create a program that will display the average of 2 numbers
inputted by the user
Algorithm:
1. Declare 4 Variables, 2 for the Addends, 1 for the Sum and 1 for
Average.
2. Let the user Input the Addends.
3. Perform the Addition between the addends and Assign it to the
Sum.
4. Perform the Sum divided by 2 and Assign it to the Average
5. Display the Average.
Programming Problem:
Create a program that will display the average of 2 numbers
inputted by the user

Pseudocode 1:
Let numOne = 0, numTwo = 0, sum = 0 and ave = 0
Input numOne and numTwo
sum = numOne + numTwo
ave = sum/2
Output ave
Programming Problem:
Create a program that will display the average of 2 numbers
inputted by the user

Algorithm:
1. Declare 3 Variables, 2 for the Addends and 1 for Average
2. Let the user Input the Addends.
3. Perform the Addition between the addends then divided by 2
and Assign it to the Average.
4. Display the Average.
Programming Problem:
Create a program that will display the average of 2 numbers
inputted by the user

Pseudocode 2:
Let numOne = 0, numTwo = 0, and ave = 0
Input numOne and numTwo
ave = (numOne + numTwo) / 2
Output ave
Programming Problem:
Create a program that will display “Even” or “Odd” depending
on the number inputted by the user.

Pseudocode:
Let num = 0
Input num
If num is an even number
output “Even”
Else
output “Odd”
Flowchart

• A method that allows a programmer to represent the Algorithm in a Diagram


or an Illustration.

• Flowcharts represents the sequence of a programming algorithm


Wen I go on
by using
standard graphic symbols that will represent the Input, Process
an online and the
shopping
Output. spree -
Jannah
• Basic Symbols
• These symbols are the ones often used to create a diagram
that represents an algorithm that a computer program must
follow.

Ellipse Hexagon Rectangle Parallelogram Diamond


Draw.io

draw.io
Programming Problem:
Create a program that will display the sum of 2 numbers
inputted by the user

Pseudocode:
Let numOne = 0, numTwo = 0 and sum = 0
Input numOne and numTwo
sum = numOne + numTwo
Output sum
Programming Problem:
Create a program that will display the average of 2 numbers
inputted by the user

Pseudocode 1:
Let numOne = 0, numTwo = 0, sum = 0 and ave = 0
Input numOne and numTwo
Sum = numOne + numTwo
ave = sum/2
Output ave
Programming Problem:
Create a program that will display the average of 2 numbers
inputted by the user

Pseudocode 2:
Let numOne = 0, numTwo = 0, and ave = 0
Input numOne and numTwo
ave = (numOne + numTwo) / 2
Output ave
Programming Problem:
Create a program that will display “Even” or “Odd” depending
on the number inputted by the user.

Pseudocode:
Let num = 0
Input num
If num is an even number
output “Even”
If num is an odd number
output “Odd”
Common Concepts of Programming

• Programming languages regardless of their variation they almost have the


same structures, sometimes a language can be more complicated or even
Wen I go on
simpler depending on its syntax but still they have plenty anofonline
common logic.
shopping
spree -
Jannah
Variables

• A word / letter that serves as temporary storage of values depending on its


datatype.

• Every programming language has a variable to store temporary data


Wen I go on
for use
in a certain program. an online
shopping
spree -
Jannah
Datatypes
Are types of data to be used in a program

Common Datatypes are:


1. String - Texts
2. Integer - Positive and Negative Numbers (No Decimal)
3. Float - Decimal Numbers
4. Character - Single Letter or Symbols
5. Boolean - True / False
Declaring Variables

int main()
{
string name = "Licensed Civil Engineer";
int age = 24;
float average = 98.5;
char letterA = 'A’;
bool isReal = false;
}
return 0;
Arithmetic Operators

Programming languages will always have arithmetic operators.


Arithmetic operators may be used between variables, for
example numOne – numTwo. The following are arithmetic
operators.
Operator Description Usage
+ Add Numbers 5 + 5 = 10
- Subtracts Number 5–5=0
* Multiply Numbers 5 * 5 = 25
/ Divides Numbers 5/2=2
% Divides then gets the remainder 5%2=1
Using an Arithmetic Operator

int main()
{
int numOne = 5;
int numTwo = 5;
int sum;

sum = numOne + numTwo;

return 0;
}
Conditional Statements
Conditional Statements are used so that the program itself can
decide what to do in a certain situation by the use of
IF - ELSE - ELSE IF Conditions (which are often present through
all programming languages)

Conditional Statements works by comparing a value with


another value if the condition is met then run this step if not
skip this step.
Example:
Is 2 an even number? - Yes then proceed.
Is 4 an odd number? - No then skip.
Logical Operators

Are often used in combination with the conditional statements


to compare values. Logical Operators are applicable with
variables.
Operator Description Usage T/F
== Equals to 2==2 TRUE
Example: != Not Equal to 2!=2 FALSE
numOne == numTwo > Greater than 5>3 TRUE
< Less than 5<5 FALSE
>= Greater than or 5>=5 TRUE
equal
<= Less than or equal 4<=5 TRUE
Special Logical Operators
Are used when you need 2 or more condition in a certain
statement

AND Operator Description


5==5 && 3==2 FALSE && And
5<6 && 6<7 TRUE ll Or

OR
5==5 ll 5>6 TRUE
10>9 ll 9>7 TRUE
int main()
{
int numOne = 5;
int numTwo = 5;
Conditional if (numOne == numTwo) {
Statements printf ("Equal“);
} else {
printf ("Not Equal“);
}
return 0;
}
Loop Statements

Loop statements are used to run something on the program


repeatedly until a certain condition is met.

Commonly used loops are called For Loops and While Loops

Loops works like this:


While the indicated Condition is not met keep looping this part
until we meet the condition
int main()
{
Loop While int i = 0;
Statement
while (i <= 5) {
printf (“ A “);
i = i+1;
}
return 0;
}
Functions or Methods

These are used to group certain functionalities that are assigned


to do a task and even return a value.

For example we have separate functions for each arithmetic


operation: addition, subtraction, multiplication and division,
each with their own function and return value.
Creating a Function / Method

int add (int numOne, int numTwo) {


return numOne + numTwo;
}

int main() {
printf ("add(5,7)";
return 0;
}
Indepth Algorithms
Wen I go on

Programming an online
shopping
spree -
Jannah
Pseudocodes Naming Convention WHILE - indication of a loop
statement
LET / INITIALIZE - initialization of END WHILE - indication of the
variables end of a loop statement
INPUT - indication of a variable to BEGIN - indication of the start of
be used in a process a function
OUTPUT/PRINT - indication of a END - indication of the end of a
value to be displayed function
IF/ELSE - indication of a CALL - indication of a function
conditional statement call
ENDIF - indication of the end of a
conditional statement *Indentations are very IMPORTANT
when it comes to special statements
Common Types of Program Algorithm

1. Sequential
2. Conditional / Selection
Wen I go on
an online
* These types can be mixed and matched with each other
shopping
spree -
Jannah
Sequential

An algorithm that runs from top-down without interruption


until it finishes the program thus naming it sequential for
sequenced.
Wen I go on
an online
shopping
spree -
Jannah
Problem Example

Create a Pseudocode and Flowcharts for a program that


computes the salary of an employee. Salary is computed by
multiplying hours worked and rate per hour. Display the salary.

Pseudocode:
Let rate = 0; hours = 0; salary = 0;
INPUT rate, hours
salary = rate * hours
OUTPUT salary
Sequencing
Let rate = 0; hours = 0; salary = 0;
INPUT rate, hours
salary = rate * hours
OUTPUT salary
Conditional / Selection

An algorithm that affects the output based on a certain


condition given which makes the program more smart and
complex thus extending the possibilities.
Logical Operators Operators

Are often used in combination with the conditional statements


to compare values. Logical Operators are applicable with
variables.
Operator Description Usage
== Equals to 2==2 TRUE
Example: != Not Equal to 2!=2 FALSE
numOne == numTwo > Greater than 5>3 TRUE
< Less than 5<5 FALSE
>= Greater than or equal 5>=5 TRUE
<= Less than or equal 4<=5 TRUE
Problem Example

Create a pseudocode and flowchart for a program that


computes the salary of an employee. Salary is computed by
multiplying hours worked and rate per hour. If the employee's
salary is higher that 10,000 display "High Pay" if not display "Low
Pay“.
Pseudocode:
Let salary = 0; rate = 0; hours = 0;
INPUT rate, hours
salary = rate * hours

IF salary > 10000


OUTPUT “High Pay”
ELSE
OUTPUT “Low Pay”
ENDIF
Activity

1. Write an algorithm, pseudocode, and draw a flowchart to find


the area of a rectangle.

2. Write an algorithm, pseudocode, and draw a flowchart to


determine whether the students have passed or failed a course; a
student needs an average of 75 on five tests to passed.
Activity
3. Create an algorithm, pseudocode, and draw a flowchart that
reads three numbers and multiplies them together, and prints out
their product.
4. Create an algorithm, pseudocode, and draw a flowchart that
performs the following:
Ask a user to enter a number. If the number is at 0 to 40, write the
word BLUE. If the number is at 41 to 80, write the word RED. if the
number is at 81 to 100, write the word YELLOW. If it is any other
number, write it is not the correct color option.
Asynchronous Activity

5. Create an algorithm, pseudocode, and draw a flowchart that


performs the following number system grading:

1.00 = 97-100 2.00 = 85-87 3.00 = 75


1.25 = 94-96 2.25 = 82-84 5.00 = 74 below
1.50 = 91-93 2.50 = 79-81 Error = 100 above
1.75 = 88-90 2.75 = 76-78
THANK
YOU! Have a
great day
ahead.

You might also like