CHAPTER 1: INTRODUCTION TO COMPUTERS AND PROGRAMMING 1 Muhalim Mohamed Amin Faculty of Computing @2015/2016-1
Objectives In this chapter, you will learn: Basic computer concepts. The different types of programming languages in general. The elements of a typical C program development environment. 2
Contents 1.1 Introduction to a Program 1.2 Computer Systems 1.3 Programming Languages 1.4 Programming Paradigms 1.5 Summary 3
1.1 Introduction to a Program What is a program? Computer: programmable machine designed to follow instructions. Program: set of instructions in computer memory to make it do something. Programmer: person who writes instructions (programs) to make computer perform a task as solution to a problem. So, without programmers, no programs Without programs, a computer cannot do anything 4
1.1 Introduction to a Program 5 Figure: Programming used to solve problem
1.1 Introduction to a Program 6 Figure: The general process to solve a problem using computer
Contents 1.1 Introduction to a Program 1.2 Computer Systems Introduction Hardware Software Data Hierarchy 1.3 Programming Languages 1.4 Programming Paradigms 1.5 Summary 7
1.2 Computer Systems Computers can perform computations and make logical decisions faster than human beings can. Billions of calculations in one second Supercomputers perform thousands of trillions (quadrillions) of instruction per second! Computers process data under the control of sequences of instructions called computer program. Introduction A computer consists of various devices referred to as hardware. The programs that run on a computer are referred to as software. 8
1.2 Computer Systems A basic computer consists of 4 basic components: Hardware Input devices (Central Processing Unit) Output devices Memory 9 Figure: Computer hardware components
1.2 Computer Systems Inputs will send the data to CPU CPU Arithmetic Control & Logic Unit (Central Unit Processing (ALU) Unit) Main memory (RAM) Secondary storages 10 Figure: Computer hardware components
1.2 Computer Systems Regardless of differences in physical appearance, virtually each computer can be divided into few logical units or sections 1,2 : CPU will executes the computer instructions Memory holds the data and programs temporarily Secondary storage store the data and program Inputs will send the data to the memory Outputs make processed information available 11 1 Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
1.2 Computer Systems Central Processing Unit (CPU): 1 The administrative section of the computer. It is the computer s coordinator. It is responsible for supervising the operation of the other sections. 2 12 Control Unit Retrieves and decodes program instructions. Coordinates activities of all other parts of computer. Arithmetic and Logic Unit (ALU) Hardware optimized for performing high-speed numeric calculation. Hardware designed for true/false, yes/no decisions. 1 http://imgsrv.worldstart.com/promos/cpu.png 2 https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/intel_cpu_pentium_4_640_prescott_bottom.jpg/200px-intel_cpu_pentium_4_640_prescott_bottom.jpg
1.2 Computer Systems Main Memory: Stores instructions and data that are to be processed by the computer. It is volatile. Main memory is erased when program terminates or computer is turned off. Also called Random Access Memory (RAM). Each byte in memory is identified by a unique number known as an address. 14
1.2 Computer Systems Memory organized as follows: o Information is stored in bits or binary digits. o Bit: Smallest piece of memory with values 0 (off, false) or 1 (on, true) o Byte: 8 consecutive bits. Bytes have addresses. o The number 149 is stored in the byte (10010101) with the address 16, and the number 72 (01001000) is stored at address 23. 15
1.2 Computer Systems Secondary Storage: It is non-volatile. Data retained when program is not running or computer is turned off. Comes in a variety of media: o Magnetic disc: floppy disk, zip disk, hard drive o Optical disc: CD-ROM, DVD-ROM o Flash drives, connected to the USB port: pen drive 16
1.2 Computer Systems Input Devices: Devices that send information to the computer from outside. Many devices can provide input: o Keyboard o Mouse o Scanner o Digital camera o Microphone 17
1.2 Computer Systems Output Devices: Output is information sent from a computer program to the outside world. The output is sent to an output device Many devices can be used for output: o Computer monitor o Printer o Speaker o Plotter 18
1.2 Computer Systems 19 Figure: Some examples of a computer system
Programs that run on a computer. 1.2 Computer Systems Software Software Categories Operating System Programs that manage the computer hardware and the programs that run on them. examples Application Software Programs that provide services to the user. Examples: word processing, games, programs to solve specific problems. 20
1.2 Computer Systems Data Hierarchy Data items processed by computers form a data hierarchy It become larger and more complex in structure from bits to higher level Figure: Example of data hierarchy 1 21
1.2 Computer Systems Table: Level of the data hierarchy 1 Level Description Bits The smallest data item in a computer (0 or 1) Characters Composed of bits Consists of decimal digits (0-9), letters (A-Z and a-z) and special symbols (e.g. $, @, %, &, *, +) The computer s character set is all characters used to write programs and present data items Fields Records Files Database A group of characters or bytes that conveys meaning Several related fields can be used to compose a record A group of related records An electronic collection of data that organized for easy access and manipulation 22
Contents 1.1 Introduction to a Program 1.2 Computer Systems 1.3 Programming Languages Introduction Type of Programming Languages 1.4 Programming Paradigms 1.5 Summary 23
1.3 Programming Languages Introduction A programmer write instructions in various programming languages, some directly understandable by computer and others requiring intermediate translation steps. We start with an algorithm, which is a set of well-defined steps. Example 1.1: Algorithm for calculating the average of 3 numbers. Step 1: Determine the first number; call it A Step 2: Determine the second number; call it B Step 3: Determine the third number; call it C Step 4: Find the sum of A,B and C; call it X Step 5: Divide X by 3; call it the RESULT Step 6: Present the RESULT 24
1.3 Programming Languages Example 1.2: Algorithm for calculating gross pay Step 1: Display a message on the screen asking "How many hours did you work? Step 2: Wait for the user to enter the number of hours worked. Once the user enters a number, store it in memory. Step 3: Display a message on the screen asking "How much do you get paid per hour? 25
1.3 Programming Languages Step 4: Wait for the user to enter an hourly pay rate. Once the user enters a number, store it in memory. Step 5: Multiply the number of hours by the amount paid per hour, and store the result in memory. Step 6: Display a message on the screen that tells the amount of money earned. The message must include the result of the calculation performed in Step 5. 26
1.3 Programming Languages Type of Programming Language Type of Programming Languages 27 Machine Language The only language the computer can understand. In binary machine code (0 s/1 s) directly Assembly Language One level above machine language. A low level language that is processor dependent. Each CPU has its own assembly language High-Level Language Closer to human language that people can read, write, and understand. High level programming languages are not processor dependent. A program written with this language must be translated into a language that can be understood by a computer before it can be run.
1.3 Programming Languages Machine language Although the previous algorithm defines the steps for calculating the gross pay (Example 1.2 - pg 25), it is not ready to be executed on the computer. The computer only executes machine language instructions. Machine language instructions are binary numbers, such as 10110100 00000101 00101111 11111111 Rather than writing programs in machine language, programmers use programming languages. 28
1.3 Programming Languages printf( Enter number1 ); scanf( %d,&data1); printf( Enter number2 ); scanf( %d,&data2); Data3=Data1*Data2; printf( Result is %d,c); MOV MUL MOV AL, DATA1 DATA2 DATA3, AX Figure: Hierarchy of programming languages 1 29
1.3 Programming Languages Table: Some well-known programming languages 30
1.3 Programming Languages Program Development Environment Generally, C systems consist of several parts: o A program development environment o The language o The C standard library C programs typically go through six phases to be executed (Figure 1.5a and 1.5b): edit, preprocess, compile, link, load and execute. 31
1.3 Programming Languages Figure: (a) Typical C development environment 1 32
1.3 Programming Languages Figure: (b) Typical C development environment 1 33
1.3 Programming Languages Phases (2) to (4) are often performed by a single command or button click. Errors detected at any step will prevent execution of following steps. Source Code Object Code Preprocessor Compiler Linker Phase 2: Phase 3: Phase 4: Modified Source Code Executable Code 34 Figure: Steps in producing an executable code
1.3 Programming Languages Integrated Development Environments (IDEs) An IDE, combine all the tools needed to write, compile, and debug a program into a single software application. Examples: Microsoft Visual C++, Borland C++ Builder, CodeWarrior, etc. Figure: Integrated Development Environments (IDE) 35
1.3 Programming Languages Figure: Example of IDE using BorlandC++ 36
1.3 Programming Languages What a Program Made of? Common elements in programming languages: o Syntax o Keywords o Programmer-Defined Identifiers o Operators o Punctuation o Variables 37
1.3 Programming Languages Syntax: specify rules for sequence of characters to be written in C language that will be grouped together to form tokens. Tokens are either keyword, identifier, constant, variable or any symbol which has some meaning in C language. A C program can also be called as collection of various tokens. Figure: Example of basic syntax in C programming language 38
1.3 Programming Languages Keywords: Reserved word These reserved words that may not be used as constant or variable or any other identifier names. Figure: Example of keyword in C programming language 39
1.3 Programming Languages Programmer-defined identifier: A name used to identify a variable, function, or any other userdefined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9) Ahmad one_day _123 abc mynumber99 a1b2c3 W8848 P A_B_C Figure: Example of acceptable programmer-defined identifier in C programming language 40
1.3 Programming Languages Operator: An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Table Example of operator in C programming language Operator Examples Arithmetic + - / * % ++ -- Relational!= > < Logical &&! 41
1.3 Programming Languages Punctuation: The punctuation and special characters have various uses, from organizing program text to defining the tasks that the compiler or the compiled program carries out. They do not specify an operation to be performed. Some punctuation symbols are also operators. The compiler determined their use from context. [ ] ( ) { } *, : = ; # 42 Figure: Example of acceptable programmer-defined identifier in C programming language
1.3 Programming Languages Variable: A variable is nothing but a name given to a storage area that programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. Upper and lowercase letters are distinct because C is casesensitive. 43
1.3 Programming Languages Table: Variable types 1 44
Contents 1.1 Introduction to a Program 1.2 Computer Systems 1.3 Programming Languages 1.4 Programming Paradigms Introduction Procedural Programming Object-Oriented Programming (OOP) 1.5 Summary 45
1.4 Programming Paradigms Introduction Programming is a creative process carried out by programmers to instruct a computer on how to do a task to solve a particular problem. There are a number of alternative approaches to the programming process, referred to as programming paradigms. Fundamentally, different paradigms represent different approaches to building solutions to specific types of problems using programming. Most programming languages fall under one paradigm, but some languages have elements of multiple paradigms. 46
1.4 Programming Paradigms Two of the most important approaches of programming process: Programming Paradigms Procedural Programming Object-Oriented Programming (OOP) 47
1.4 Programming Paradigms Procedural Programming Focus is on the process or action oriented 1. Uses a list of instructions to tell the computer what to do step-by-step. Unit of programming: function 1 or procedures. Functions / procedures are written to process data. Examples: C, COBOL, FORTRAN, Pascal, BASIC 48 Figure: The main program coordinates calls to procedures and hands over appropriate data as parameters 2 I Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall
1.4 Programming Paradigms Object-Oriented Programming (OOP) Also known as modular programming 2 Focus is on objects, which contain data and know how to manipulate the data with grouped functionality. Unit of programming: class or module (from which objects created) 1 Messages sent to objects to perform operations. Examples: C++, Java, C#, Delphi, Ada Figure: The main program coordinates calls to procedures in separate modules and hands over appropriate data as parameters 2 49 _ I Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall
Contents 1.1 Introduction to a Program 1.2 Computer Systems 1.3 Programming Languages 1.4 Programming Paradigms 1.5 Summary 51
1.5 Summary In this chapter, you have learnt: Basic computer concepts, its hardware and software components that support the program development. A programmers use programming languages to write a program rather than coding in machine language. The common elements of a typical C program development environment. 52
53 http://i.imgur.com/souer.png
Self-Review 54
Exercise 1.1: Fill in the blanks in each of the following. Self-Review a) Computers process data under the control of sequences of instructions called computer. program b) languages are most convenient to programmer for writing a program quickly and easily. High-level c) The only language a computer can directly understand is that computer s. Machine language d) C programs are normally typed into a computer using a(n) program. editor 55
Self-Review Exercise 1.1: Fill in the blanks in each of the following. e) The translate a(n) language program into f) A(n) provides access to system program for editing, compiling, and so on. g) Before linking, a machine language program is saved on disk as a(n) file. h) After linking, a machine language program is saved on disk as a(n) file. e) compiler, high-level, machine language f) Operating system, g) object, h)executable 56
Exercise 1.2: Self-Review Categorize each of the following items as either a form of input or output. a) Scanning images b) Printing images c) Scanning a barcode. d) Receiving a video from a webcam e) Displaying a whatsapp message in a screen f) Receiving a voice command 57
Self-Review Exercise 1.3: 58 1. Specify the correct order: execution, linking, translation, loading. Ans: translation, linking, loading, execution 2. Determine whether each the following characteristics applies memory or secondary storage: a. Faster to access b. Volatile c. May be extended virtually without limit d. Less espensive e. Used to store files f. Central processor accesses it to obtain the next machine language instruction for execution. g. Provides semipermanent data storage. Ans: main(a, b, f), secondary(c,d,e,g)
Self-Review Answer 1.1: a) Program b) high-level. c) machine language. d) editor Answer 1.2: a) Input b) Output c) Input d) Input e) Output f) Input 59