Lecture1 Algorithms

42
Lecture 1: Computer Structure, Classification of Computer Programming Languages Prof. Carolina Peña-Ortega Universidad de la Salle

Transcript of Lecture1 Algorithms

Lecture 1: Computer Structure,

Classification of Computer

Programming Languages

Prof. Carolina Peña-Ortega

Universidad de la Salle

Outline

2

Computer Structure

Software: Operating System and Application Software

Programming Languages

Program Development Life Cycle

Computer structure

3

What is a Computer?

4

A computer is an

electronic device that

manipulates information, or

“data”. It can store,

retrieve, and process data.

Computer Hardware

5

The physical components of a computer.

Most computers have six basic components:

Main memory

Central processing unit (CPU)

The arithmetic/logic unit (ALU)

The control unit

Input/output devices

Auxiliary storage devices

Basic Components of a Computer

6

Arithmetic Logic Unit

Control Unit

Auxiliary

Storage

Device Memory Unit ( RAM & Registers )

Central Processing Unit ( CPU )

Input Device

Output Device

Peripherals

Central Processing Unit (CPU)

7

The part of the computer that

executes the instructions

(program) stored in memory;

made up of the arithmetic/logic

unit and the control unit.

Central Processing Unit (CPU)

8

The arithmetic/logic unit (ALU) performs

arithmetic operations (addition, subtraction,

multiplication, and division) and logical operations

(comparing two values).

The control unit controls the actions of the other

components so that program instructions are

executed in the correct order.

Main Memory

9

Commonly known as random access memory (RAM)

Holds instructions and data needed for programs that

are currently running

RAM is usually a volatile type of memory

Used as temporary storage

Secondary Storage

10

A nonvolatile storage medium

Hard disk drives are most common

Records data magnetically on a circular disk

Provides fast access to large amounts of data

Optical devices store data on CD’s as pits

USB flash memory devices

High capacity device plugs into USB port

Portable, reliable, and fits easily in a pocket

Input/output (I/O) Devices

11

The parts of the computer that accept data to be

processed (input) and present the results of that

processing (output).

Input devices: any type of device that provides data

to a computer from the outside world. Example:

keyboard, mouse and scanner.

Output devices: any type of device that provides

data from a computer to the outside world. Example:

Monitor (display screen) and printer.

Software

12

The programs that run on a computer

Two major categories:

Operating systems

Controls the processes within the computer

Manages the computer's hardware devices

Application Software

Solve problems or perform tasks needed by users

Examples include word processing, spreadsheets, games, Internet browsers, playing music, etc)

Each program is referred to as an application

Concepts

13

Computer programming: The process of planning

a sequence of steps for a computer to follow.

Algorithm: A step-by-step procedure for solving a

problem in a finite amount of time.

Computer program: A sequence of instructions to

be performed by a computer.

Programming language: A set of rules, symbols,

and special words used to construct a computer

program.

Classification of computer

programming languages

14

Programming Languages

15

The steps in our algorithm must be stated in a form

the computer understands

The CPU processes instructions as a series of 1’s and

0’s called machine language

Machine language: The language, made up of

binary-coded instructions (strings of 0s and 1s), that

is used directly by the computer. It is no portable, and

it runs only on specific type of computer.

Programming Languages

16

Assembly language: It is based on mnemonics that

symbolize processing steps (instructions), processor

registers, memory locations, and other language

features.

Use symbols instead of binary digits to describe fields

of instructions.

10101100100000100000000000010101 ADDI R4 R2 21

ADDI R4,R2,21

Assembler Program

17

A utility program called an assembler is used to

translate assembly language statements into the

target computer's machine code.

Source program: A program written in assembly

language.

Object program: The machine language version of

a source program.

Source

Program

(Assembly)

ASSEMBLER Object

Program

Levels of Programming Languages

18

The levels of programming languages are:

Low-level language

High-level language

Levels of Programming Languages

19

Low-Level Language: A computer language that

deals with hardware registers by name; also known as

assembly language.

A program written in a low-level language can be

used only on a computer system that uses one type

of main processor (or possibly a member of a family

of processors).

Low-level programming wastes effort in coding a

solution rather than solving a problem.

Difficult to build and maintain large programs.

Levels of Programming Languages

20

High-Level Language: High-level programming

languages allow the specification of a problem

solution in terms closer to those used by human

beings.

These languages were designed to make

programming far easier, less error-prone and to

remove the programmer from having to know the

details of the internal structure of a particular

computer.

High-Level languages are portable, and they require a

compiler.

Compiler and Interpreter Languages

21

In addition to the distinction between high-level and

low-level languages, there is a further distinction

between compiler languages and interpreter

languages:

Compiler Languages: are the high-level equivalent

of assembly language. Each instruction in the compiler

language can correspond to many machine

instructions.

Compiler Languages

22

Once the program has been

written, it is translated to the

equivalent machine code by a

program called a compiler. Once the

program has been compiled, the

resulting machine code is saved

separately, and can be run on its

own at any time.

Source

Program

COMPILER

Object

Program

Interpreter Languages

23

An interpreter language, like a

compiler language, is considered

to be high level. However, it

operates in a totally different

manner from a compiler language.

Rather, the interpreter program

resides in memory, and directly

executes the high-level program

without preliminary translation to

machine code.

Source

Program

INTERPRETER

translation and

execution line

by line

Interpreter Languages

24

This use of an interpreter program to directly

execute the user's program has both advantages and

disadvantages. The primary advantage is that you can

run the program to test its operation, make a few

changes, and run it again directly.

In addition, because the interpreter has to scan the

user's program one line at a time and execute

internal portions of itself in response, execution of

an interpreted program is much slower than

for a compiled program.

Common Programming Languages

25

Python – Interpreter language

Matlab – Interpreter language

Java – Compiler language

C – Compiler language

C++ - Compiler language

Visual Basic – Compiler and Interpreter language

Code Examples

26

C

#include <stdio.h>

void main() {

printf("Hello, World!\n");

}

C++

#include <iostream>

void main(){

cout << "Hello, World!\n”;

}

26

Machine Code c7 3c 2a 3c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c

28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c

2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b

2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c

3c 28 5c 2a 2b 2a 5c 3c 28 5c 2a 2b 2a 5c 3c 28

Python

print "Hello, World!"

Program Development Life Cycle

27

Program Development Life Cycle

28

The basic set of procedures that are followed by

various organizations in their program development

methods are as follows:

1. Problem Definition

2. Algorithm Design

3. Coding

4. Debugging and Testing

5. Documentation

6. Maintenance

Program Development Life Cycle

29

Maintenance

Problem Definition

and Design

Documentation

Time

Stages of

Program

Development

Life Cycle 1

2

3

Phase 1: Problem Definition and Design

30

Problem

Definition

Coding

Time

Program Design

Debugging

and Testing

Steps of

Problem

Definition

and Design

Phase 1: Problem Definition and Design

31

Step 1: Problem Definition

This stage is the formal definition of the task. It

includes the specification of inputs and outputs

processing requirements, system constraints, and

error handling methods.

This step is very critical for the completion of

a satisfactory program. It is impossible to solve a

problem by using a computer, without a clear

understanding and identification of the problem.

Inadequate identification of problem leads to poor

performance of the system.

Phase 1: Problem Definition and Design

32

Step 2: Algorithm Design

Once the problem has been identified, the next stage

is the program design. The programmer must

decide, prior to writing his program, exactly

which steps the computer should take to solve

an identified problem.

Such a functional description of the task is either

called an algorithm or results in a diagram called

flowchart.

Phase 1: Problem Definition and Design

33

The techniques that are useful in program design include:

Modular Programming: A method in which long

programs are divided into smaller programs or

modulates that can be designed, coded and debugged

separately with a minimum amount of interaction.

Main

Program

Module 1 Module 2

Module 11 Module 21 Module 22

Phase 1: Problem Definition and Design

34

Top-Down Design: A method in which the

overall task is first defined in terms of generalized

sub-tasks that, in turn, are subsequently further

defined. The process continues downward until

the sub-tasks are defined in a form suitable for

execution by the computer.

Phase 1: Problem Definition and Design

35

Step 3: Coding

The third step is the process of transforming the

program logic design documents into a computer

language format.

This stage translates the program design into

computer instructions. These instructions are the

actual program or the software product.

During this step the programmer eliminates all the

syntax and formal errors from the program and

all logic errors are detected and resolved during this

process.

Phase 1: Problem Definition and Design

36

Step 4: Debugging and Testing

Debugging: This stage is the discovery and

correction of programming errors. Few programs

run correctly the first time, so debugging is an

important and time consuming stage of software

development.

Phase 1: Problem Definition and Design

37

Testing: This stage is the validation of the program.

Testing ensures that the program performs

correctly the required tasks. Program testing and

program debugging are closely related. Testing is

essentially a later stage of debugging in which the

program is validated by trying it on a suitable set of

cases.

Testing may reveal errors, but does not guarantee

the absence of errors.

Phase 1: Problem Definition and Design

38

Programming theorists often refer to program

debugging and testing as verification and validation,

respectively. Verification ensures that the

program does what the programmer intends

to do. Validation ensures that the program

gives the correct results for a set of test data.

Phase 2: Documentation

39

This stage is the documentation of the program so that

those who use and maintain it, can understand it, so that

the program can be extended to further applications. Yet

proper documentation is not only useful in the testing

and debugging stages, it is also essential in the

maintenance and redesign stages.

A properly documented program can be easily reused

when needed; an undocumented program usually requires

so much extra work that the programmer might as well

start from scratch.

Phase 2: Documentation

40

Among the techniques commonly used in

documentation are flowcharts, comments, memory

maps, parameter and definition lists, and program

library forms.

Documentation is a time consuming task that the

programmer performs simultaneously with the design,

coding, debugging and testing stages of software

development.

Phase 3: Maintenance

41

This stage is the updating and correcting of the

program to account for changing conditions or field

experience.

In order to reduce the costs and the time spent on

maintenance, we can use the following steps:

Clarity and readability of code.

Structured code.

Modularity.

Debugging and testing.

Documentation.

References

42

Programming in C++, Nell Dale and Chip Weems.

Chapter 1.

C++ for Engineers and Scientists, Gary J. Bronson.

Chapter 1.