KEMBAR78
Algorithms p2 | PDF | Numbers | Variable (Computer Science)
0% found this document useful (0 votes)
9 views9 pages

Algorithms p2

The document discusses how to represent algorithms for computers using narrative, pseudocode, and flowcharts, emphasizing the importance of using terms and symbols that computers understand. It explains the concepts of variables and constants, the basic data types recognized by computers, and how to translate human language into pseudocode for effective communication. Finally, it outlines a problem-solving example involving reading numbers, calculating their sum and average, and highlights the key concepts of pseudocode and programming fundamentals.

Uploaded by

9db7m95yxv
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)
9 views9 pages

Algorithms p2

The document discusses how to represent algorithms for computers using narrative, pseudocode, and flowcharts, emphasizing the importance of using terms and symbols that computers understand. It explains the concepts of variables and constants, the basic data types recognized by computers, and how to translate human language into pseudocode for effective communication. Finally, it outlines a problem-solving example involving reading numbers, calculating their sum and average, and highlights the key concepts of pseudocode and programming fundamentals.

Uploaded by

9db7m95yxv
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/ 9

Created by Turbolearn AI

Representing Algorithms
When representing algorithms, consider:

How to communicate with the computer.


Understanding input, process, and output.

Speaking to the computer involves using terms it understands, relying on:

Variables
Control structures

Ways to Represent Algorithms


1. Narrative:

Using your own words in English.


Similar to voice assistants (Siri, Google Home, Alexa).
Can be challenging for precise instructions.

2. Pseudocode:

English that resembles programming.


A middle ground between English and computer language.
More structured than narrative, but less rigid than programming code.

3. Flowcharts:

Using symbols to represent steps.


Helpful for those who understand symbols better than words.

Narrative Code Example


If the objective is to read three numbers and display their sum, narrative code would
look like this:

1. Start
2. Get the three numbers
3. Add the numbers

Page 1
Created by Turbolearn AI

The computer doesn't understand these instructions because it needs to "speak" in


code.

Pseudocode Explained
To get the computer to understand, use terms and symbols it can grasp.

Instead of number one, use n1.


Computers prefer the shortest possible way to get something done.

Computers use variables.

Variables are named storage locations in the computer's memory that can
hold values. These values can change during the execution of a program.

Constants are used when a value will not change.

For example, g = 9.81 (gravity).

Computers primarily understand ones and zeros. They prefer numbers and
mathematical operators:

Addition: +
Subtraction: -
Multiplication: *
Division: /

Shortening Terms for Computers


Shorten English terms into terms the computer understands. This is similar to
shortening words when texting to fit within 160 characters (a common practice in the
past to save money).

From Narrative to Pseudocode


To transition from narrative to pseudocode, use specific symbols and terms.
Relational operators are essential:

Page 2
Created by Turbolearn AI

Instead of "is less than," use <.


Instead of "greater than or equal to," use >=.

The goal is to use symbols, not words, because words are difficult for a computer to
understand. This is analogous to explaining something to a three-year-old using
simple terms rather than complex vocabulary.

Basic Terms in Pseudocode


The goal is to make the computer understand easily. Break down terms into smaller
components.

Instead of "is equal to," use =.

Input and Output Terms


When writing in pseudocode, you need to use terms that the computer can
understand to input and output data. Examples of such terms include:

read (e.g., read a)


get (e.g., get b)
input (e.g., input c)
print (e.g., print a or print b)
set
store
equal sign

The computer recognizes these terms as keywords.

How Computers Understand


Computers process everything as ones and zeros. To communicate effectively with a
computer, you need to represent information in a way it can understand, primarily as
numbers.

Data Types
Humans understand letters (A-Z, a-z), numbers (0-9), and special characters.
Computers primarily understand numbers, making it easier to represent data
numerically. The fundamental data types that computers recognize are:

Page 3
Created by Turbolearn AI

Integers
Numbers that can be positive or negative whole numbers (no decimal
places).

Examples:

20
-55
127

Floating Point (Real Numbers)


Numbers that can be positive or negative and include decimal places.

Even if a whole number is given, it will be represented with a decimal (e.g., 10


becomes 10.0, 9 becomes 9.0).

Examples:

10.0
9.0
9.2

Characters
Represented by numerical codes.

When a character (e.g., 'g') is entered, it is translated into a numerical code (e.g.,
001010). The computer does not inherently understand the character but recognizes
the numerical code assigned to it.

Example: The letter 'g' is translated to a numerical code like 01000

Building Blocks
Everything a computer does is built on these three data types: integers, floating-point
numbers, and characters. Complex operations, such as creating words, sentences,
games, and graphics, are all based on ones and zeros.

Page 4
Created by Turbolearn AI

Memory, Constants, and Variables


To communicate with a computer, it's necessary to use memory and understand the
concepts of constants and variables. In programming, constants are values that do
not change, while variables can change. These concepts enable the computer to
make decisions based on the state of the program.

In order to do that we have to create a world where constants exist for the computer
and where variables exist for the computer that means we create in a world where
things could change or things could stay the same so that the computer will be able
to follow our instructions based on the state of our.

Variables and Constants

Constants
A constant is a memory location with a fixed piece of data.
For example, gravity (g) can be defined as 9.81.

A constant is like a memory location where there's a fixed piece of data.

The computer associates the value (9.81) with the identifier (g).
Whenever the computer encounters g, it substitutes it with 9.81.

Variables
A variable is a location where the information can be changed.

Variables are like a location where the information could be changed or is


changeable.

For example, the number of siblings (n) can change.


The computer checks the content of the memory location to know the current
value of the variable.
Variables change (e.g., temperature, amount of gas in the car).

Relationship

Page 5
Created by Turbolearn AI

The distinction can be summarized in the following table

Feature Constant Variable

Definition Fixed piece of data Changeable information


Behavior Value remains the same Value can be updated
Example Gravity (9.81) Number of siblings

Data Types
We know that the numbers we could use are whole numbers, decimals, and
characters.
With data types, variables, and constants, we can start giving instructions to a
computer.
Computers understand numbers, characters, variables, and constants.
With these, we can speak the computer's language.

Pseudocode
Pseudocode is a type of code, representing instructions in a human-
understandable form.
It bridges the gap between English and actual code (ones and zeros).

Pseudocode means sudo is basically the meaning or type of type of code


right? type of code if it's a type of code then you are going to be seeing
the instructions to a computer in a way that you will understand but it's
not really code code as in a programming language you are actually
writing it in a form that's in between english and the actual code so that's
why it's called sudo code.

When writing a program, you express instructions using pseudocode terms the
computer understands.## Understanding Pseudocode and Computer
Instructions

From Narrative to Code


The goal is to translate human language into a format that a computer can
understand. For instance, instead of saying "get the value 4e," we use "read a".

Page 6
Created by Turbolearn AI

Sudoku exemplifies this by shortening commands to be computer-readable.

Variables and Computer Logic


Read a, read b, read c: These commands create placeholders (variables) that
the computer can recognize.
The computer understands that a, b, and c are locations to store data.
Instead of a narrative like "find the sum of the three numbers," we use "sum = a
+ b + c."
The equal sign is a relational operator, which the computer understands.
The terms are shortened, and variable names are used.
Addition signs are used instead of the word "sum".
Instead of saying "print out the value of sum," we use "display sum" or "print
sum".

Example Problem: Averaging and Multiplying Numbers


Problem: Read three numbers, find their average, and multiply the average by the
sum of the three numbers.

Solution:

1. Start
2. Read or Get:
get a
get b
get c
a, b, and c are variables, which are placeholders for numbers.
3. Sum:
sum = n1 + n2 + n3
The computer adds the values inside n1, n2, and n3 and stores the result
in sum.

Illustration with Envelopes


Imagine giving a three-year-old child (the computer) three envelopes labeled n1, n2,
and n3. The child doesn't know what's inside.

Page 7
Created by Turbolearn AI

1. Give the child another envelope labeled sum.


2. Instruct the child to take whatever is inside n1, add it to n2, then add it to n3.
3. Tell the child to write the result on a piece of paper and store it inside the sum
envelope.

The child (computer) only needs to know the instructions and the variables.

Continuing the Solution


4. Average:
ave = sum / 3
5. Answer:
answer = average * sum
6. Print:
print answer

Summary of the Algorithm


Read three numbers.
Find the sum.
Divide the sum by three to find the average.
Multiply the average by the sum.
Print the result.

In the end, we used six variables to perform the calculations based on our
instructions.

Key Concepts
Pseudocode: A type of code that a computer can understand based on
given instructions.

The instructions must align with the data types (integers, floating points, characters)
that the computer understands.

Programming Fundamentals

Page 8
Created by Turbolearn AI

Core Concepts
A variable is a fundamental concept in programming.

Five key elements can enable a computer to perform any task: These elements
are not defined in this segment.

Computers require instructions in their own language, which is a type of code


called pseudocode.

Pseudocode: A notation resembling a simplified programming


language, used in program design.

Next Steps
Next week's topic: Control structures.
Sequential execution
Selection
Loops

Page 9

You might also like