You are on page 1of 13

ALGORITHMS INTRODUCTION

Definition
An algorithm is a finite sequence of step by step

instructions for solving a particular problem


problem

algorithm

input

computer

output

Definition
Problems can be in any form
Business
Get a part from Vancouver to Ottawa by morning Allocate manpower to maximize profit

Life
I am hungry. How do I order pizza? Explain how to tie shoelaces to a five year old child

Algorithms
An algorithm must specify every step completely,

so a computer can implement it without any further understanding An algorithm must work for all possible inputs of the problem. Algorithms must be:
Correct: For each input produce an appropriate output Efficient: run as quickly as possible, and use as little memory as possible more about this later

Algorithmic Representation of Computer Functions


Input
Get information Get (input command) Given/Result Intermediates/Set Let (assignment command) Loop If Give (output command)

Storage
Store information

Process
Arithmetic Repeat instructions Branch conditionals

Output
Give information

Representation of Algorithms

Flowcharts

Pseudocodes
Programs

Flowchart

A flowchart is a common type of diagram, that

represents an algorithm, showing the steps as boxes of various kinds, and connecting these with arrows

Algorithm 1.1
NAME: SumTimes GIVENS:Num1, Num2 RESULTS: Total, Product DEFINITION: Total & Product := SumTimes(Num1, Num2)
Start SUMTIMES

Get Num1 Get Num2

Let Total = Num1 + Num2 Let Product = Num1 * Num2

Give Total Give Product

Finish SUMTIMES

Pseudocode
high-level description of a computer programming algorithm. Pseudo-instructions are phrases written in ordinary natural language that a computer cannot understand. If student's grade is greater than or equal to 60 Print "passed" else Print "failed"

Program
A computer program (also a software program, or just a program) is a sequence of instructions written to perform a specified task for a computer. Computer programming is the iterative process of writing or editing source code. Editing source code involves testing, analyzing, and refining
#include <stdio.h> int main() { printf("Hello world!\n"); return 0; }

Data Types
BOOLEAN (2 bytes) Either True or False
INTEGER (2 bytes) Whole numbers (no decimals)
-32,768 32,767

LONG (4 bytes) Whole numbers (integers)


-2 trillion 2 trillion

Whole numbers require less memory and run

faster than decimal numbers (or variant type)

Data Types
SINGLE (4 bytes) Real numbers
10^30 with 7 significant digits of accuracy

DOUBLE (8 bytes) Real numbers


10^300 with 15 significant digits of accuracy

CURRENCY (8 bytes) Real numbers


4 digits to the right of the decimal point 15 digits to the left of the decimal point

Data Types
DATE (8 bytes) Storage of specific dates
Jan 01, 100 to Dec 31, 9999

STRING (1 byte per character) Up to 2 billion characters per string

Use the * symbol to denote size Some numbers should be defined as strings

Student number, SIN, telephone number We do not perform arithmetic calculations on them

VARIANT (sized by Visual Basic as required) Default type, if type not defined Can be any type and type can vary as code is executing Not to be used

You might also like