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

1-Intro-to-Computers-and-Logic

The document provides an introduction to computers, explaining the distinction between software and hardware, and detailing the processes of input, processing, and output. It covers the importance of algorithms, their qualities, and examples of basic algorithms for common tasks such as finding sums and searching for elements in lists. Additionally, it discusses various searching algorithms, including linear search, binary search, and jump search.

Uploaded by

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

1-Intro-to-Computers-and-Logic

The document provides an introduction to computers, explaining the distinction between software and hardware, and detailing the processes of input, processing, and output. It covers the importance of algorithms, their qualities, and examples of basic algorithms for common tasks such as finding sums and searching for elements in lists. Additionally, it discusses various searching algorithms, including linear search, binary search, and jump search.

Uploaded by

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

(1) Intro to Computers and Logic

Software

Is computer instructions that tell the hardware what to do.


Software programs are instruction sets written by programmers.

Hardware

Is the equipment or the physical devices associated with a computer.


For example, keyboards, mice, speakers, and printers are all hardware.

System Software/Application Software

Our computer is easy to use,

INPUT
Data items enter the computer system and are placed in memory, where they can
be processed.
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 tasks is the central processing unit,
or CPU.
OUTPUT
After data items have been processed, the resulting information is usually sent to
a printer, monitor, or some other output device so people can view, interpret, and
use the results.

Understanding Simple Logic

A program with syntax errors cannot be fully translated and cannot execute.
A program with no syntax errors is translatable and can execute, but it still might
contain logical errors and produce incorrect output.
For a program to work properly, you must develop correct logic; that is, you must write
program instructions in a specific sequence and not leave any instructions out.

Example:

Suppose you instruct someone to bake a cake:


Get a bowl
Stir
Add a gallon of gasoline
Add two eggs
Add three cups of flour
Bake at 350°F for 45 minutes
Don’t do this:
"Add a gallon of gasoline."
Even though the instruction is written in understandable English, the logic of baking the
cake is still erroneous.

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 = my Number * 2
output myAnswer

The number-doubling process includes three instructions:


INPUT number:
When the user types an input (e.g., number), it will be stored in a variable. A
variable is a container for a value that may change as we progress in our program
code.

SET answer:

After getting the input, the computer/program will process it depending on the
requirements given by the programmer. In our example, it just doubles myNumber
and assigns it to another variable called myAnswer.

OUTPUT answer:
The processed number will be stored in the main memory. Then, the output
devices will show or print the answer if the user wishes to retrieve it from the
computer.

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.
An algorithm can be done in many ways.

Qualities of a good algorithm

Input and output should be defined precisely.


Each step-by-step procedure in the algorithm should be clear and unambiguous.
The algorithm should be most effective among many different ways to solve the
problem.
An algorithm shouldn’t have computer code. Instead, the algorithm should be written in
a way that it can be used similarly to programming language.

Basic Algorithms

1. Write an algorithm to get the sum of 5 and 10. Display it as a result. Here’s the
algorithm:
- Step 1: Start
- Step 2: Get the sum of 5 and 10 using the addition operation and assign it to the
result.
python result = 5 + 10
- Step 3: Display result
- Step 4: Stop.
Explanation:
Step 1:
- Serves as a guide on where the reader should start the algorithm.
Step 2:
- 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.
Step 3:
- Shows the result as output of the process since it states in the problem that the sum
should be displayed.
Step 4:
- Is where the algorithm stops.
2. Write an algorithm that gets the sum of two numbers as input from the user and
displays the result. Here’s the algorithm:
- Step 1: Start
- Step 2: Declare variables num1 and num2 that will hold user inputs respectively and
result that will contain the sum of these variables.
- Step 3: Assign numbers to num1 and num2.
- Step 4: Get the sum of these variables and assign it to result.
python result = num1 + num2
- Step 5: Display result.
- Step 6: Stop
Explanation:
Step 1:
- Serves as a guide on where the reader should start the algorithm.
Step 2:
- We declare/create a variable. It’s just like in your senior high school days where you
declare a variable in math (e.g., let x = 5, let y = 10).
Step 3:
- We assume that numbers were assigned to our variables. Let’s 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 works for every input.
Step 4:
- 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 display the result. So, whatever the value of the variable result yields 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, the result should be 13. Step 6, stop or
end of our algorithm.
Examples:

1. Write an algorithm to find and display the largest among three different numbers
entered by the user.

Step1:
Start
Step2:
Declare variables num1, num2, and num3 that will hold the user’s input.
Step 3:
Assign a value for num1, num2, and num3.
Step 4:
Compare the value of num1 to see if it’s greater than num2 and num3; if yes,
display num1 as the largest.
Step 5:
Compare the value of num2 to see if it’s greater than num1 and num3; if yes,
display num2 as the largest.
Step 6:
Compare the value of num3 to see if it’s greater than num2 and num1; if yes,
display num3 as the largest.
Step 7:
Stop.

Optimization:
What if we assign the following values to our variables:

num1=100, num2=20, num3=30

- **Step 1:**
Start
- **Step 2:**
Declare variables num1, num2, and num3 that will hold the user’s
input.
- **Step 3:**
Assign a value for num1, num2, and num3.
- **Step 4:**
Compare the value of num1 to see if it’s greater than num2 and
num3; if yes, display num1 as the largest and proceed to step 7.
- **Step 5:**
Compare the value of num2 to see if it’s greater than num1 and
num3; if yes, display num2 as the largest and proceed to step 7.
- **Step 6:**
Compare the value of num3 to see if it’s greater than num2 and
num1; if yes, display num3 as the largest.
- **Step 7:**
Stop.

2) Another approach for finding the largest value among the three variables:
- **Step 1:**
Start
- **Step 2:**
Declare variables num1, num2, and num3 that will hold the user’s
input.
- **Step 3:**
Assign a value for num1, num2, and num3.
- **Step 4:**
Compare the value of num1 to see if it’s greater than num2; if
yes, proceed to step 5, otherwise proceed to step 6.
- **Step 5:**
Compare the value of num1 to see if it’s greater than num3. If
yes, display num1 as the largest; otherwise, display num3 as the largest. Proceed
to Step 7.
- **Step 6:**
Compare the value of num3 to see if it’s greater than num2. If
yes, display num3 as the largest; otherwise, display num2 as the largest.
- **Step 7:**
Stop.

##### Categories of Algorithms


- **Search**
Algorithm to search for an item in a set of data.
- **Sort**
Algorithm to sort items in a certain order.
- **Insert**
Algorithm to insert an item in a set of data.
- **Update**
Algorithm to delete an existing item from a set data.

##### Search 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 algorithms is the **Linear search**.
1) **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 has been searched.
- 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.
![[Pasted image 20240830155335.png]]
**ALGORITHM for Linear Search**
**Step 1:**
- Start
**Step 2:**
- 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 the first place).
**Step 3:**
- Accept a value for $x$.
**Step 4:**
- If $i \leq n$ and $x \neq a_i$, add 1 to the value of $i$. Repeat until
one or both conditions become false.
**Step 5:**
- If $i > n$, display $x$ is not in the list; else, display $x$ is
located at position $i$.
**Step 6:**
- Stop.
![[Pasted image 20240830155853.png]]

2) **Binary Search**
- **Binary search** is a search algorithm that finds the 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$.
- A formula of $(low + high) // 2$ can be used as an example. Wherein the
output number will not be rounded off.
- The algorithm always starts by examining the middle element of the
current search range.
![[Binary-Linear.gif]]

3) **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 with our previous list of numbers used in our binary
search: 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).
![[Pasted image 20240830160641.png]]

You might also like