Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

PROGRAMMING TECHNICS

ALGORITHMS
RECOMMENDATIONS FOR EFFECTIVE LEARNING

• Come to class
• Punctuality
• Proactive participation in class
• Do not be a perfectionist,allow mistakes to happen and
learn from them, try and experiment, ask questions.
• Study and work at home.
PROGRAMMING TECHNICS

• Introduction:Programming • ControlStructures:
languages - IDE – Thonny – Sequence, Conditional,
VSC (Visual Studio Code) Iteration
• Algorithms:Data types, • Arrays
variables and constants • Matrices (Image Bitmaps)
• Flow Diagrams • Functions
• Input/Output
INTRODUCTION

• The computer is more than a machine that can perform data


processing that gives us results, without us knowing which specific
operations it carries out to arrive at them.
• A computer allows us to design tailor-made solutions to any
specific problem we confront. Even more if such problems involve
complex mathematical operations, whether these must be repeated
many times or manage a high volume of data.
• The design of tailor-made solutions to our problems requires, as in
any other discipline, a specific methodology that teaches us in a
gradual manner a path to arrive at such solutions.
INTRODUCTION

• Computer:
• electronic device to process information and extract results. Data or
information can be entered into the computer device as INPUT where it
is subsequently processed to produce an OUTPUT result.
INPUT PROCESS OUTPUT

• Program:
• A set of instructions written in a given programming language that when
executed in the order as written (sequence), solves a specific problem.
INTRODUCTION
• Programming Language:
• Language adapted to communicate orders to a computer, it consists
of a set of symbols, characters, syntax and semantic rules with
which users communicate with the computer device to obtain in
return comprehensible answers.
• Programming languages have a set of instructions that allow the
user to perform certain operations:
• INPUT/OUTPUT
• Calculation (arithmetic) / String manipulation (text strings)
• Logic / Comparisons
• Storage / Recall (Recovery)
INTRODUCTION

• Classification of programming languages:


• Machine Code: Instructions that are comprehensible by the computer CPU unit, they do
not need to be translated. This language is expressed in terms of the lowest portion
of memory register, the bit (binary digit 0 or 1).
• Low-level programming language (Assembly): This language is written with mnemonic
alphabetic codes that stand for specific operations. These codes consist of a rather
small set of acronyms or abbreviations of human language words. Example: SUB (for
subtraction).
• High-level programming language: The set of instructions of these languages are
similar to human language statements (generally in English), making comprehension and
coding easier. Some examples of high-level languages are: BASIC, Pascal, COBOL,
Javascript, Typescript and Python.
INTRODUCTION

• In general terms the programs described as high-level have a


resemblance to human natural language (any language) and
those described as low-level have a resemblance to Machine
Code: Binary.
• Some programming languages are described as mid-level because
they can speak directly to specific parts of the computer
hardware but at the same time these instructions can be
expressed or written with high-level syntax, some examples of
these mid-level programming languages include C#, Java, etc.
INTRODUCTION
IDE: INTERACTIVE DEVELOPMENT ENVIRONMENT

IDE: YOU WILL USE THONNNY IDE:


THONNY VISUAL STUDIO CODE
ALGORITHMS

• ¿What are algorithms?


• An algorithm is a series of steps (instructions) that have the
following characteristics:
• Finite
• Determined
• Order
• These steps must describe the process that should be followed to
solve a specific problem, not necessarily of technological nature.
INTRODUCTION
DETERMINED AND
FINITE DEFINED ORDERED SEQUENCE
• Algorithms are a • Implies that if we • The set of
finite set of repeat the instructions that
instructions algorithm with the constitute the
meaning they have a same input we algorithm have a
Beginning (starting should get the same defined order or
point) and an End. result. (Similar to sequence.
a function f(x)=y )
ALGORITHMS

• The union of these three characteristics establish a precise


association between Machine Code and real world language,
with which we lay and plot any given problem.
• Being able to establish this association is the MOST COMPLEX
TASK of programming, this is the SKILL YOU MUST DEVELOP
before coding.
• This skill is also usually called COMPUTATIONAL THINKING.
ALGORITHMS

• There are ways to ease up the design of algorithms and the


subsequent translation to any programming language.
• Existence of a series of symbols and rules to explicitly describe a
process, pretty much like a recipe.
• Flow Diagrams: describe the steps or instructions of an algorithm
using graphic diagrams allowing a detailed follow up of the process
the inputs go through to generate an output.
• Pseudocode: way of writing instructions using specific words from
natural language, brief English language statements usually called:
STRUCTURED ENGLISH.
Define the problem
ALGORITHMS
Define input data

• Phases for designing


an algorithm Define output data

1. Analyzing and breakdown of


the problem Write steps Modify
2. Algorithm design No
Flow Diagram
3. Coding (writing program
Pseudo Code
script)
4. Verification (testing) Testing
Works
Fine?
5. Maintenance

Yes
End
ALGORITHMS

• Example: Steps to serve a glass of water from the fridge.


• # Beginning
• Go to the kitchen ¿Which are
• Open the fridge Variables?
• Take out the pitcher from the fridge
• Pour water into a glass
¿Which are
• Put back the pitcher in the fridge
Operations?
• Close the fridge
• # End
ALGORITHMS
LET’S ANALYZE THE PREVIOUS CHART: NON-TECHNOLOGICAL ALGORITHM

• We have two variables that will change: the pitcher and the glass
• The glass has a fixed volume and is empty, then it will be filled, it does not matter how
much as long as it does not exceed the volume of the glass.
• The pitcher is a bit different, it holds an initial quantity of water and it has a second
‘variable’ a given placement (inside the fridge, could be at the door, at the back or up
front of an inner shelf).
• We can identify an operation that involves two variables: serving the pitcher content into
the empty glass, changing the values of both variables. The glass value will increase
while the pitcher value will decrease in equal amount (linear relationship).
• There is one final operation that has two stages, meaning that one implies the other.
Opening the fridge to access the pitcher and once the pitcher is placed back in the
initial place closing it.
ALGORITHMS

Data
• Any data value comes associated to a data type. Data can be a simple character
such as the letter “b” or an integer number such as 35.

Simple Data Types


• Numeric Data: Used to represent scalar values in numeric form, they include
whole integer values or real (decimal) values. This data type allows arithmetic
operations.
• Logic Data: Type that can only have two possible values: TRUE or FALSE. They
represent the result of a comparison between the values of two different
variables (numeric or alphanumeric), they are called Boolean types.
ALGORITHMS

• AlphanumericData (strings): Set of characters (numbers,


letters and symbols) that allow to represent descriptive
values such as: names, addresses, pseudo-numbers, etc.
It´s possible to represent numbers with alphanumeric
characters but: these lose their arithmetic properties
and it is not possible to perform math operations with
them.
ALGORITHMS
CONSTANTS & VARIABLES

• Constant: A memory stored variable whose numeric or alphanumeric


value does not change (a fixed value) throughout a given process. A
perfect example is the approximation value of pi = 3.14159.
• Variable: A space in a register (inside the computer memory) where
a data value is stored during a process. To be able to store it we
need to name it so it can be invoked or recalled later. In many
cases we will need to define the type of data and also specify
forehand the amount of memory space needed to store it.
• Example: Area = (pi*(radius**2))
• The variables are: the radius, Area and the constant pi.
ALGORITHMS
VARIABLES
• Assigning a value to a constant or a variable to a memory register,
remember the size of the register can vary:

= 3.14159 = ‘John’
Pi name
ALGORITHM
SEQUENTIAL STRUCTURES
• Start
• A sequential process is the • Action 1
shape a process adopts if
each action or step
• Action 2
(instruction) follows a • ……………………
defined order.
• The output of a step is the
• Action N
input of the following step • End
until the end is reached.
• A sequence may be
represented as follows:
ALGORITHMS

• There are 3 general types of processes:


• Reading Inputs: Entered data by the user and saved to be
processed. Ex: INPUT height and base
• Assignments: They allow saving data in the memory or for
calculations. Ex: Area_Tri = (height*base)/2
• Showing Outputs: There are different ways of showing data. On
screen, printed text or graphics, sound, video, etc…
Ex: OUTPUT Area_Tri
ALGORITHMS: FLOW DIAGRAMS

Process
Decision

Input/Output
Data
Start/End
ALGORITHMS:

• We want to calculate the surface area of a circle, we know


the mathematical formula:

AREA = (Pi*(radius**2))

• In order to calculate the area the formula requires that we


need an approximate value of Pi and the value of the radius.

• Circle Radius INPUT DATA


ALGORITHMS
Pseudocode: First Attempt

• Store the constant approximate value of Pi = 3.14159


• Ask the user to enter the value of the radius. Is the proper data
type of the radius variable int() or float()? What do you think?
• Save the value in the variable named “radius”.
• Calculate the square of radius and store in a new variable called
“Aux”
• Calculate the product of Pi and Aux and store it in the variable
named “Area”.
• Show the user on screen the result stored in the variable “Area”.
ALGORITHMS
Final Pseudocode: Written as IGCSE requests.

START START
• Pi  3.14159 • Pi  3.14159
• INPUT radius • INPUT radius
• Aux  (radius * radius) • Area  (Pi * (radius^2))
• Area  (Pi * Aux) • OUTPUT Area
• OUTPUT Area END
END
Both versions are correct.
START
ALGORITHMS
Pi  3.14159
Final Flow Diagram:

START
INPUT radius
• Pi  3.14159
• INPUT radius
Area  (Pi*(radius^2))
• Area  (Pi * (radius^2))
• OUTPUT Area
OUTPUT Area
END

END
ALGORITHMS
Exercises:

1. “Supposean individual has a capital invested in a bank


and wants to know how much money he will earn after a
month if the bank pays at a rate of 2% per month.”
2. “Supposean individual has a capital invested in a bank
and wants to know how much money he will earn after a
month if the bank pays at a rate of 2% per month, only
if the capital is greater than $1000.”
ALGORITHMS
Exercises:

3. Designa program that asks to enter two numbers and


tells the user which of those two numbers is the
greatest.
4. Designanother program that asks to enter three numbers
and tells the user which is the greatest of the three.
Can you add any useful information about the other
numbers for the user?
REMEMBER TO STUDY AT HOME TOO!!!
Thank you for Reading this

You might also like