introduction to computer & c computers computers are programmable machines capable of performing...

Post on 29-Dec-2015

227 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction toComputer & C

ComputersComputers

Computers are programmable machines capable of performing calculations

Examples of special-purpose computers are calculators and game-playing machines

Examples of general-purpose computers are personal computers and notebooks

Hardware/SoftwareHardware/Software

A computer consists of hardware and softwareThe hardware consists of various physical devices that performs wired and basic operationsThe software consists of various programs that coordinates basic operations to accomplish flexible and complex tasks

ProgramsPrograms

A Program is a sequence of instructions (basic operations) to control the operation of the computer

Programming is the task of designing programs

Programming languages are notations used to represent the instructions of computers

HardwareHardware

storage unit

input unit

primary storage (memory) unit

output unit

secondary storage unit

control unit

arithmetic and logic unit (ALU)

central processin

g unit (CPU)

I/O unit

control bus

data bus

Control UnitControl Unit

Control unit repeatedly fetches instructions from memory unit to control unit via data bus

Control unit then interprets instructions, and coordinates the operations of other units via control bus

Arithmetic and Logic UnitArithmetic and Logic Unit

ALU is responsible for performing calculations based on arithmetic operations such as addition, subtraction, multiplication, and division

ALU is also responsible for making decisions based on logical operations such as equality (=, ≠) and relation (<, ≦, >, ≧)

Arithmetic and Logic UnitArithmetic and Logic Unit

Arithmetic or logical operations performed by ALU are controlled by control unit via control bus

Data on which operations are performed are transferred from memory unit via data bus

Data resulted from operations are transferred to memory unit via data bus

Primary Storage UnitPrimary Storage Unit

Memory unit stores both instructions and data

It retains information that is actively being used by the computer

It is the short-term, rapid-access, low-capacity warehouse of a computer

Secondary Storage UnitSecondary Storage Unit

Secondary storage unit retains information that is not actively being used by the computer

It is the long-term, slow-access, high-capacity warehouse of the computer

Common secondary storage devices are disks and tapes

Input UnitInput Unit

Input unit transfers information from various input devices to memory unit

It also transforms information in human-readable form to information in machine-readable form

Common input devices are keyboards and mouse devices

Output UnitOutput Unit

Output unit transfers information from memory unit to various output devices

It also transforms information in machine-readable form to information in human-readable form

Common output devices are screens and printers

Users

SoftwareSoftware

Application Programs

Operating System

Hardware

Operating SystemOperating System

The operating system provides efficient management of the hardware so that application programmers can easily use the computer without knowing the detailed operations of the hardware

Common operating systems are DOS, Windows 2000, Windows NT, Unix, Linux

Application ProgramsApplication Programs

An application program allows users to use a computer to solve problems in a specific domain without knowing the details of the computer

Common application programs are MS Word, MS Excel, MS Access, MS Internet Explorer, Netscape Communicator

Biological Applications:

BLAST, FastA, DOCK, DSSP, …

High-level languages

Machine language: computer’s native language.

Add two numbers: 00100111 1010 0101

Assembly language: uses mnemonics.

Add two numbers: ADD R1 R2

An assembly code needs to be translated to machine code.

High-level languages

High-level programming languages: bridging the gap between machine language and natural languages.

Examples: COBOL, Fortran, Algol, C

Add two numbers (in C): Z = A + B;

Compilation: translation to machine code.

CompilersCompilers

A compiler translates high-level language

programs into assembly language programs

or machine language programs

High-level language

C, C++, Pascal

Interpreter

Basic, PHP, …

Each high-level instruction is usually

translated into several assembly instructions

AlgorithmsAlgorithms

An algorithm is an abstract strategy for

solving a problem

Solving a problem by computer consists of

designing an algorithm and expressing the

algorithm as a program

The Programming Language CThe Programming Language C

C is a general-purpose programming language

C is developed by Dennis Ritchie at Bell Laboratories

C has become one of the most widely used languages in the world

The C Programming SystemThe C Programming System

C programming language

A set of notations for representing

programs

C standard library

A set of well-developed programs

C programming environment

A set of tools to aid program development

ProgramsPrograms

A C program consists of functions and variables

A function contains instructions that specify the operations to be done during the computation

Variables denote memory locations that store data used during the computation

FunctionsFunctions

Functions can be either user-defined functions or library functions

A C program begins the execution at the beginning of the function named main

A function may call other functions to help it perform one of its subtasks

The First C ProgramThe First C Program

#include <stdio.h>/* include library

information */main( ) /* name of starting function */{ /* beginning of instructions */ printf(“hello, world\n”);

/* call library function */} /* end of instructions */

C Programming EnvironmentC Programming Environment

Edit

Compile

Link

Execute

prog1.c, prog1.h, prog2.c, prog2.h

prog1.obj, prog2.obj

prog.exe

lib.h

lib.obj

input outputDebug

Compilation in C

Use the cc or gcc compiler:cc prog.c

Produces executable file a.out if no errors

To specify name of executable file, use -o option:

cc -o prog prog.c

Name of executable file becomes ‘prog’.

Workstation

Errors

Compilation errors: occur during compilation.

Reason: syntax errors.

Easy to rectify.

Run-time errors: occur during execution.

Reasons: logic errors, data errors, computation errors.

Harder to rectify.

Program development

EDIT

Understanding the problem

Writing an algorithm

Converting to code

COMPILE

RUN

errors

Result

Verifying the algorithm

Program = Algorithm + Data Structures

Algorithm

Algorithm: a well defined computational procedure consisting of a set of instructions, that takes some value(s) as input, and produces some value(s), as output.

Al-Khowarizmi Algorismus Algorithm

Algorithm

Input OutputAlgorithm

Algorithm

Understanding the problem

Writing an algorithm

Converting to code

Verifying the algorithm

Algorithm embeds logic of solution.

Algorithm is converted to program.

Algorithmic problem solving:

writing an algorithm -- tough

translate algorithm to code -- easy

Euclidean algorithm

First documented algorithm by Euclid (300 B.C.) to compute greatest common divisor (gcd). Examples: gcd(3,21) = 3; gcd(15,40) = 5.

1. Let A and B be integers with A > B 0.

2. If B = 0, then the gcd is A and the algorithm ends.

3. Otherwise, find q and r such thatA = qB + r where 0 r < B

Note that we have 0 r < B < A and gcd(A,B) = gcd(B,r).Replace A by B, B by r. Go to step 2.

Euclidean algorithm

Walk through the algorithm with examples: A = 40, B =15.

A = 2B + 10 A = 15 ; B = 10

A = 1B + 5 A = 10 ; B = 5

A = 2B + 0 A = 5 ; B = 0

gcd is 5

1. Let A and B be integers with A > B 0.

2. If B = 0, then the gcd is A and the algorithm ends.

3. Otherwise, find q and r such thatA = qB + r where 0 r < B

Note that we have 0 r < B < A and gcd(A,B) = gcd(B,r).Replace A by B, B by r. Go to step 2.

Data types and structures

Data types: integer, real number, character, Boolean, pointer, etc. Examples: 32, 0.0264, 'a', true.

Data structures: arrays, records, files, etc.

Program = Algorithm + Data Structures

Data are stored in variables in a program. Variables take up memory space in the computer.

Euclid’s algorithm in C

int gcd(int m, int n){ int r; while ( (r = m % n) != 0) { m = n; n = r; } return n;}

“New Style” function declaration lists number and type of arguments

Originally only listed return type. Generated code did not care how many arguments were actually passed.

Arguments are call-by-value

m n

1 15 4

2 4 3

3 3 1

m n

1 30 15

m n

1 48 15

2 15 3

3 3

Euclid’s algorithm in C

int gcd(int m, int n){ int r; while ( (r = m % n) != 0) { m = n; n = r; } return n;}

Automatic variable

Storage allocated on stack when function entered, released when it returns.

All parameters, automatic variables accessed w.r.t. frame pointer.

Extra storage needed while evaluating large expressions also placed on the stack

nm

ret. addr.r

Frame pointer Stack

pointer

Excess arguments simply ignored

Euclid’s algorithm in C

int gcd(int m, int n){ int r; while ( (r = m % n) != 0) { m = n; n = r; } return n;}

Expression: C’s basic type of statement.

Arithmetic and logical

Assignment (=) returns a value, so can be used in expressions

% is remainder

!= is not equal

Euclid’s algorithm in C

int gcd(int m, int n){ int r; while ( (r = m % n) != 0) { m = n; n = r; } return n;}

High-level control-flow statement. Ultimately becomes a conditional branch.

Supports “structured programming”

Each function returns a single value, usually an integer. Returned through a specific register by convention.

Characteristics of an algorithm

Each step must be exact.

Must terminate.

Must be effective.

Must be general.

Pseudo-code

How to represent an algorithm?

Pseudo-code

Flowchart

Pseudo-code: a combination of English text, mathematical notations, and keywords from the programming language.

Pseudo-code

To find average, min and max among a list.First, you initialise sum to zero, min to a very big number, and max to a

very small number.

Then, you enter the numbers, one by one.

For each number that you have entered, assign it to num and add it to the sum.

At the same time, you compare num with min, if num is smaller than min, let min be num instead.

Similarly, you compare num with max, if num is larger than max, let max be num instead.

After all the numbers have been entered, you divide sum by the numbers of items entered, and let ave be this result.

End of algorithm.

Pseudo-code

To find average, min and max among a list.sum count 0 { sum = sum of numbers;

count = how many numbers are entered? }min ? { min to hold the smallest value eventually }max ? { max to hold the largest value eventually }

for each num entered,

increment count

sum sum + num

if num < min then min num

if num > max then max numave sum/count

Flowchart

Diagrammatic form:

terminator

connector

process box

decision box

Flowchart

To find minimum and maximum among a list.

start

sum count 0min ?max ?

end of input?

increment countsum sum + num

num<min?

num>max?

min num

Yes

No

ave num/count

A

A

end

Yes

max numYes

No

No

Exercise

Dedign a flowchart for gcd function

Write a program for gcd function

top related