Top Banner
Introduction to Computer & C
45

Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Dec 29, 2015

Download

Documents

Michael Warner
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 to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Introduction toComputer & C

Page 2: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 3: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 4: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 5: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 6: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 7: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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 (<, ≦, >, ≧)

Page 8: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 9: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 10: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 11: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 12: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 13: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Users

SoftwareSoftware

Application Programs

Operating System

Hardware

Page 14: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 15: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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, …

Page 16: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 17: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 18: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 19: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 20: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 21: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 22: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 23: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 24: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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 */

Page 25: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 26: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 27: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 28: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Program development

EDIT

Understanding the problem

Writing an algorithm

Converting to code

COMPILE

RUN

errors

Result

Verifying the algorithm

Program = Algorithm + Data Structures

Page 29: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 30: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Algorithm

Input OutputAlgorithm

Page 31: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 32: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 33: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 34: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 35: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 36: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 37: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 38: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 39: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Characteristics of an algorithm

Each step must be exact.

Must terminate.

Must be effective.

Must be general.

Page 40: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 41: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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.

Page 42: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 43: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Flowchart

Diagrammatic form:

terminator

connector

process box

decision box

Page 44: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

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

Page 45: Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Exercise

Dedign a flowchart for gcd function

Write a program for gcd function