Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are."— Presentation transcript:

1

2 Introduction to Computer & C

3 Computers 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

4 Hardware/Software A computer consists of hardware and software The hardware consists of various physical devices that performs wired and basic operations The software consists of various programs that coordinates basic operations to accomplish flexible and complex tasks

5 Programs 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

6 Hardware storage unit input unit primary storage (memory) unit output unit secondary storage unit control unit arithmetic and logic unit (ALU) central processing unit (CPU) I/O unit control bus data bus

7 Control 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

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

9 Arithmetic 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

10 Primary 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

11 Secondary 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

12 Input 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

13 Output 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

14 Users Software Application Programs Operating System Hardware

15 Operating 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

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

17 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.

18 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.

19 Compilers 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

20 Algorithms 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

21 The 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

22 The 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

23 Programs 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

24 Functions 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

25 The First C Program #include /* include library information */ main( )/* name of starting function */ {/* beginning of instructions */ printf(“hello, world\n”); /* call library function */ }/* end of instructions */

26 C 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 output Debug

27 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

28 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.

29 Program development EDIT Understanding the problem Writing an algorithm Converting to code COMPILE RUN errors Result Verifying the algorithm Program = Algorithm + Data Structures

30 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

31 Algorithm InputOutputAlgorithm

32 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

33 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 that A = 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.

34 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 that A = 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.

35 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.

36 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 mn 1154 243 331 mn 13015 mn 14815 21533

37 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 n m ret. addr. r Frame pointer Stack pointer Excess arguments simply ignored

38 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

39 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.

40 Characteristics of an algorithm Each step must be exact. Must terminate. Must be effective. Must be general.

41 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.

42 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.

43 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  num ave  sum/count

44 Flowchart Diagrammatic form: terminator connector process box decision box

45 Flowchart To find minimum and maximum among a list. start sum  count  0 min  ? max  ? end of input? increment count sum  sum + num num<min? num>max? min  num Yes No ave  num/count A A end Yes max  num Yes No

46 Exercise Dedign a flowchart for gcd function Write a program for gcd function


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

Similar presentations


Ads by Google