Lec 5 Algorithm
Lec 5 Algorithm
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
• 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
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
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
int main()
{
string name = "Licensed Civil Engineer";
int age = 24;
float average = 98.5;
char letterA = 'A’;
bool isReal = false;
}
return 0;
Arithmetic Operators
int main()
{
int numOne = 5;
int numTwo = 5;
int sum;
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)
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
Commonly used loops are called For Loops and While Loops
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
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