CPE3-Week-2
CPE3-Week-2
Introduction to
Computers and Logic
CPE 3L – Programming Logic and Design
Computer
Software Hardware
is computer instructions that tell is the equipment, or the physical
the hardware what to do. devices, associated with a
Software are programs, which are computer. For example, keyboards,
instruction sets written by mice, speakers, and printers are all
programmers. hardware.
2
Software
System Software
Application Software
3
Our computer is easy to use
Input
Process
Ouput
4
Data items enter the
computer system and
are placed in memory,
INPUT where they can be
processed.
5
PROCESS
Processing data items may involve
organizing or sorting them,
checking them for accuracy, or
performing calculations with them.
The hardware component that
performs these types of tasks is the
central processing unit, or CPU.
6
OUTPUT
After data items have been
processed, the resulting
information usually is sent to
a printer, monitor, or some
other output device so
people can view, interpret,
and use the results.
7
A program with syntax errors
cannot be fully translated and
cannot execute. A program with no
Understanding syntax errors is translatable and can
execute, but it still might contain
8
Let’s have an example:
9
Let’s have an example:
10
Double the number
Most simple computer programs include steps that perform input,
processing, and output. Suppose you want to write a computer program
to double any number you provide. You can write the program in a
programming language such as Visual Basic or Java, but if you were to
write it using English-like statements, it would look like this:
input myNumber
set myAnswer = myNumber * 2
output myAnswer
11
The number-doubling process includes
three instructions:
12
Algorithm
▣ An algorithm is a step-by-step technique that specifies a collection
of instructions that must be carried out in a specific order to get
the desired result.
13
Qualities of a good algorithm
Input and output should be defined precisely.
14
Basic Algorithms
Step1: Start
Step2: Get the sum of 5 and 10 using addition operation and assign it
to result.
result = 5 + 10
Step3: Display result
Step4: Stop.
15
Explanation for example 1
Step1 – serves as a guide on where
should the reader starts the algorithm.
16
Basic Algorithms
Write an algorithm that gets the sum of two numbers as input from
the user and display the result. Here’s the algorithm:
Step1: Start
Step2: Declare variables num1 and num2 that will hold user inputs
respectively and result that will contain the sum of these variables.
Step3: Assign numbers to num1 and num2.
Step4: Get the sum of these variables and assign it to result.
result = num1 + num2
Step5: Display result.
Step6: Stop.
17
In Step 2, we declare/create a variable. It’s just like in your senior high school days
wherein you declare variable in math(i.e. let x = 5, let y = 10).
In Step 3, we will assume that numbers were assigned to our variables. Let say num1 is
10 and num2 is 3. You can think of another number to assign to those variables since
we’re just assuming values to try if the algorithm really works for every input.
Step 4, in this step we will carry out the process to be taken in order for us to get the
result(output). Remember that, we need to assign the answer to a variable called
result. To do that, we just need to follow this formula:
result = num1 + num2
Step 5, we just need to display the result. So, whatever the value of the variable result
has yield after the process, that would be displayed on our algorithm.
If we assume that num1 is 10 and num2 is 3 then after the algorithm, result should be
13.
Step 6, stop or end of our algorithm.
18
TRY THIS!
Step1: Start
Step2: Declare variable num that will hold the number.
Step5: Stop.
19
Another Example
Write an algorithm to find and display the largest among the three
different numbers entered by the user.
Step1: Start
Step2: Declare variables num1, num2 and num3 that will hold the
user’s input.
Step3: Assign a value for num1, num2 and num3.
Step4: Compare the value of num1 if it’s greater than num2 and
num3, if yes display num1 is the largest.
Step5: Compare the value of num2 if it’s greater than num1 and
num3, if yes display num2 is the largest.
Step6: Compare the value of num3 if it’s greater than num2 and
num1, if yes display num3 is the largest.
Step7: Stop.
20
Optimization
Step2: Declare variables num1, num2 and num3 that will hold the user’s input.
Step4: Compare the value of num1 if it’s greater than to num2, if yes proceed to step5,
otherwise proceed to step6.
Step5: Compare the value of num1 if it’s greater than to num3. if yes, display num1 is the
largest otherwise display num3 is the largest. Proceed to Step7.
Step6: Compare the value of num3 if it’s greater than num2. if yes, display num3 is the
largest otherwise display num2 is the largest.
Step7: Stop.
22
Categories of Algorithm
23
Searching Algorithms
24
Linear Search
12 25 10 8 6
25
Algorithm for Linear Search
Step1: Start
Step5: If i > n, display x is not in the list, else display x is located at position i.
Step6: Stop.
26
Linear Search
5 1 1 6 true true 2
5 2 11 6 true true 3
5 3 15 6 true true 4
5 4 5 6 true false
Binary Search
1 2 3 4 7 8 12 15
Jump Search
1 2 3 4 7 8 12 15
Thanks!
Any questions?
We’ll continue next meeting
32