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

CPE3-Week-2

The document provides an introduction to computers, covering essential concepts such as software, hardware, and the basic functions of input, processing, and output. It explains programming logic, algorithms, and various types of algorithms including searching methods like linear and binary search. Additionally, it emphasizes the importance of correct logic and syntax in programming to achieve desired outcomes.

Uploaded by

easdeathsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CPE3-Week-2

The document provides an introduction to computers, covering essential concepts such as software, hardware, and the basic functions of input, processing, and output. It explains programming logic, algorithms, and various types of algorithms including searching methods like linear and binary search. Additionally, it emphasizes the importance of correct logic and syntax in programming to achieve desired outcomes.

Uploaded by

easdeathsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

TOPIC:

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

Simple logical errors and produce incorrect


output as a result. For a program to
work properly, you must develop
Logic correct logic; that is, you must write
program instructions in a specific
sequence, you must not leave any
instructions out.

8
Let’s have an example:

Suppose you instruct someone to bake a cake:


▣ Get a bowl
▣ Stir
▣ Add two eggs
▣ Add a gallon of Gasoline Don’t do this
▣ Bake at 350o for 45mins
▣ Add three cups of flour

9
Let’s have an example:

Suppose you instruct someone to bake a cake:


❑Get a bowl
❑Stir
❑Add two eggs
❑Add a gallon of Gasoline Don’t do this
Even though the instruction is written
❑Bake at 350 for 45mins
o
in understandable English language,
❑Add three cups of flour
the logic of baking the cake is still
erroneous.

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:

Input number Set answer Output answer


When the user types an After getting the input, The processed number
input(i.e. number), it will the computer/program will be stored in the main
be stored in a variable. A will process it depending memory. Then, the
variable is a container of on the requirements output devices will show
a value. We usually use given by the programmer or print the answer if the
this to represent an input to the computer. In our user wishes to retrieved
that may change as we example, it just doubles it from the computer.
progress in our program myNumber and assign it
code. to another variable called
myAnswer.

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.

▣ Algorithms are often written without regard to the underlying


programming languages; that is, an algorithm can be written in
more than one computer language.

▣ Algorithm can be done in many ways

13
Qualities of a good algorithm
Input and output should be defined precisely.

Each step-by-step procedure in algorithm should be clear and


unambiguous.

Algorithm should be most effective among many different ways


to solve problem.

An algorithm shouldn’t have computer code. Instead, the


algorithm should be written in such a way that, it can be used
similar to programming language.

14
Basic Algorithms

Write an algorithm to get the sum of 5 and 10. Display it as result.


Here’s the algorithm:

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.

Step2 – it states that in the problem,


we need to get the sum of 5 and 10.
This is where we put the process to be
taken and how to do it.

Step3 – shows the result as output of


the process since it states in the
problem that the sum should be
displayed.

Step4 – is where the algorithm stops.

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!

Write an algorithm to find out if a given number is even or odd.

Step1: Start
Step2: Declare variable num that will hold the number.

Step3: Assign a value for num

Step4: Divide num by 2 and check if the result is a whole number. If


num/2 is a whole number then num is even, otherwise it’s an odd
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

What if we assign the following values to our variables:


num1=100, num2=20, num3=30
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.and proceed to step 7.
Step5: Compare the value of num2 if it’s greater than num1 and
num3, if yes display num2 is the largest.and proceed to step 7.
Step6: Compare the value of num3 if it’s greater than num2 and
num1, if yes display num3 is the largest.
Step7: Stop.
21
Another approach for finding the largest value
among the three variables.
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 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

Search − Algorithm to search an item in a set of data.

Sort − Algorithm to sort items in a certain order.

Insert − Algorithm to insert item in a set of data.

Update − Algorithm to update an existing item in a set of data.

Delete − Algorithm to delete an existing item from a set of data.

23
Searching Algorithms

Searching Algorithms are designed to check for an element or


retrieve an element from any list of data where it is stored.
One of the most commonly used searching algorithm is the
Linear search.

Linear Search – linear search or sequential search is a method


for finding an element within a list. It sequentially checks each
element of the list until a match is found or the whole list have
been searched.

24
Linear Search

For example, we have a list of numbers: 12, 25, 10, 8, 6. In


Linear search, the numbers are checked one-by-one. Let’s look
for 8.

12 25 10 8 6

25
Algorithm for Linear Search
Step1: Start

Step2: Declare variables x(the number we want to locate), n as the maximum


number of elements in a list, a as the list containing the elements/numbers, i = 1
( 1 indicates the positioning of an element or initially at first place).

Step3: Accept a value for x.

Step4: If i ≤ n and x ≠ ai , add 1 to the value of i. Repeat until one or both


condition becomes false.

Step5: If i > n, display x is not in the list, else display x is located at position i.

Step6: Stop.

26
Linear Search

Let’s locate 5 on this list of number:


a = 1, 11, 15, 5, 8, 20
a1 a2 a3 a4 a5 a6

x i ai n i≤n? x≠ai? i=i+1

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

Binary search is a search algorithm that finds a position of a


target value within a sorted list. Binary search compares the
target value to the middle element of the list.

Let’s say we have a list of numbers: 8, 1, 12, 4, 15, 2, 7, 3. In


order for binary search to work, we need to sort them in
ascending or descending order. To make it simple, let’s use
ascending order: 1, 2, 3, 4, 7, 8, 12, 15.
Binary Search

Let’s look for 1 in our sorted list: 1, 2, 3, 4, 7, 8, 12, 15

1 2 3 4 7 8 12 15
Jump Search

Jump search is a searching algorithm used to find an element in


a sorted list of elements. It is similar to binary search in that it
requires the input data to be sorted, but it uses a different
approach to find the target element efficiently.
Let’s try it also to our previous list of numbers used in our
binary search: 1, 2, 3, 4, 7, 8, 12, 15
Jump Search
Let’s look for 8 in our sorted list: 1, 2, 3, 4, 7, 8, 12, 15
First, get the 8. The value inside the square root depends on how many elements
are there in the list. 8 = 2.83 or just 3. The value 3 is the number of jumps from
our number to the next(excluding our current number).

1 2 3 4 7 8 12 15
Thanks!
Any questions?
We’ll continue next meeting

32

You might also like