1-Intro-to-Computers-and-Logic
1-Intro-to-Computers-and-Logic
Software
Hardware
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.
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:
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
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
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:
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:
- **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.
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]]