Top Banner
Introduction Computers, algorithms
32

Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

May 16, 2018

Download

Documents

phungkhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Introduction

Computers, algorithms

Page 2: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Computer• mathematical engine that processes programs

and data• it uses some physical principle, for example:

– mechanical computers – Pascal – electronic computers – after the 2nd World War

• computer processes:– programs

• system programs and apliccations

– data• originally only numbers

– mathematical problems – numerical solving of differential equations

• texts, pictures, sound, …

Page 3: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• computer transforms input data to input data

• data are represented in digital computers by binary digits 0,1

Computer

Computerinput data output

Page 4: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Basic attributes of the architecture:

1. Architecture is independent of the processed task, the behavior of the computer is controlled by the programm, that is stored in the memory(program is sequence (flow) of instructions).

2. Memory is common for program and data.3. Memory is divided into cells with linear

organization; cell is identified by its address (address reference number)

• architecture of todays computers was designed in 1947 by American mathematician of Hungarian origin John von Neumann

Page 5: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

4. Instructions, addresses, data, control signals are represented by binary signals (binary system)

5. Instructions are processed in the sequential order, how they are stored in the memory; the order can be change with the special instruction JUMP.

6. No operand (processed data) is usually stored in the instruction, but reference (address) to the data

– consequence: the program is universal, it works not only with constants stored in instruction but with data stored in memory (and read from keyboard, disk, …)

Page 6: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Software equipment of PC• BIOS (Basic Input Output System)

– stored in Flash EEPROM– basic routines (programs) to control hardware

(input and output operations), for example:• reading/writing block of data from/to harddisk• control videocard

• operating system (OS)– “basic” software(administrator of sources –

memory, disk)– it provides an environment for users to control

computer (GUI - graphical user interface)and for application program to run

Page 7: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

– OS makes access to SW and HW resources• access to files on disk, • access to networks

• application software– text editors, …

Page 8: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Programming

• once more– the behavior of digital computer is controlled

by the program– program is a sequence of elementar

command (instructions) coded binary, it is stored in memory

– program (instructions) is executed by processor

• the set of instructions of some processor is called instruction set; the set and binary coding of instructions are determined by the designer of processor

Page 9: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Instruction consist of:1. command

– binary coded operation (add, multiply, movdata, …)

2. operand/operands– data to operate

• direct – constant is stored directly in instruction• indirect - adresa of memory cell, where operand

is stored

Page 10: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Examples of instructions• arithmetic

– add two numbers stored in registers, multiply two numbers

– compare two numbers in registers

• jumps– unconditional – jump to address x (i.e next

executed instruction is at address x)– conditional – jump to address x, if result of

previous operation is equal to 0

Page 11: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• moving data– move data from memory cell of address x to

the internal register• inside processor there are special memory cell

called registers to store result or intermediate results due to fast access: operations are processed between registred inside processor

– přesuň data from internal register to the peripheral device

• …

Page 12: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• program is a sequence of binary numbers(= coded instructions)– such program is written in machine code

• in the era of beginning (50th) programmers wrote programs directly in machine code: first level of the programming– „superprogramers“ knew binary codes of

instructions from their memory and they were able to write programs in binary digits without list of instruction

– it is said Mr. S. Cray (designer of the first parallel computer) be able to dictate machinery code of his operation system kernel using telephone

Page 13: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• such way of programming is slowly and blind and

• second level of the programming: assembler– text mnemonic code of instruction

(abreviations)– ADD – addition, CMP - compare, JZ - jump

zero – conditional jump – jump to address, if result of previous operation is zero

• program = text in assembler

Page 14: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• program in assembler is an input of another program called. compiler, which transform assembler into machine code (runableprogram)

• example of assembler:

ADD AX, BX // add to the register AX value in BXCMP AX,5 // compare value in AX with number 5JZ 1234 // jump to address 1234, if result of

CMP is zero (comparison in computer is realized by subtraction, i.e. result of previous operation is zero, if value in AX is 5

Page 15: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• advantage of assembler:– programs are created easily

• disadvantage of assembler– programmers must know architecture and

instructions of given processor; program is not portable to other platform

– notation of the program is far from mathematic and natural language

– assembler is a low level language

Page 16: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• third level: from 60th: – high level programming languages (Fortran,

Cobol, Pascal, Basic, C, Java, Javascript)– text of program written in some high level

programming language is similar to the natural language (structured English) and to the mathematical notation

– program is compiled to the machinery code (runnable program) using compiler

– program written in high level programming language is readable, understandable, portable

• to compile the code under others OS, for another platform (processor) small changes are sufficient, we use only another compiler

Page 17: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Algorithms

Page 18: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Algorithms

• algorithm– technique, how to solve some problem written

in formal (structured) way– it is a sequence of some elemental steps

(instructions, commands)– you know basic algorithm:

• process how to compute roots of quadratic equation • cookery book: recipe

– an object who executes algorithm is a processor

• processor determines elementar steps

Page 19: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• example:– recipe how to prepare cake

• cook(man) is a processor, elemental steps are: take 2 eggs, …

– algorithm to compute roots of quadratic equation for computer

• processor is a computer processor, elemental steps are binary coded instruction of given processor

• to write a program means: think out algorithm (how to solve a problem) and write down algorithm in some high level programming language

Page 20: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Example: Algorithm to solve quadratic equation

1. if a=0 go to the step 82. D = b2-4ac3. if D < 0 go to the step 64. Equation has two real roots …5. End6. Equation has two comlex roots …7. End8. etc.

Page 21: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Features of algorithms• elemental

– consist of finite numbers of elemental steps

• deterministic– it is possible to determine after the execution of each

step if the process finishes (i.e. which is the next step to be executed)

• finite– total count of the steps is finite

• massed (universal)– it is useful to solve set of similar tasks

Page 22: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Basic components of the algorithms:• 3 components:1. simple sequence of commands2. branching (conditions)

– selection of the next step (instruction) depends if a condition is satisfied or not

3. cycles (loops)– steps are executed repeatedly

• loops with given count of repeating• loops with condition (at the end of the loop/at the beginnig of

the loop)

Page 23: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Algorithm notation

• flowcharts (graphical notation)– steps of the algorithm are expressed by

special graphical symbols depending on the type of the operation

– the sequence of steps execution is determined by arrows

– the flowchart is oriented graph

Page 24: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• START / END of the algorithm

• action (command)

• condition (branching)

START

s = a * b

a < byes, +, 1

no, -, 0

Page 25: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

There exist special symbols for cycles, input/output operations (printing, reading from keyboard), work with files

• input/output operation

Print: The circumference of the circle is r

Page 26: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• cycle (loop) with given count of repeating– called for loop in programming languages

i = 1 to 10

Print: 3*i

NEXT i

loop body

Page 27: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• loop with condition at the beginning– while loop in programming languages

are dataready?

yes

nowait 1s

Page 28: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Example: algorithm to compute circumference of the circle

r < 0

0

1

o = 2 * 3.14 * r

START

END

Print: Enter circumreference

Read: r

Print: Circumference is o

Print: Error,

Radius is negative

Page 29: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• structured natural language

1. Print: Enter radius2. Read: r3. if r < 0, go to the step 74. o = 2*3,14*r5. Print: Circumference is o6. End7. Print: Error: Radius is negative8. End

Page 30: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

• pseudo-language– we use constructs of some programming

language but commands are written by free style

– example: pseudo-language based on Pascal

Print: Enter radiusRead: rif r < 0 then Print: Error: Radius is negativeelsebegino = 2*3,14*rPrint: Circumference is o

end

Page 31: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Program written in C programming language

printf("Enter circumference: ");

scanf("%f",&r);

if (r<0)

printf(“Error: Radius is negative");

else

{

o = 2*3.14*r;

printf(“Circumference = %f",o);

}

Page 32: Introduction - cvut.cz · Introduction Computers, algorithms. Computer • mathematical engine that processes programs ... programming language is similar to the natural language

Tasks

• Write an algorithm how to prepare a cup of tea

• Write an algorithm to compute a root of the linear equation ax + b = c

• Write complete algorithm to compute roots of the quadratic equation ax2 + bx + c = 0