chapter 1: introduction to computers and programming cseb113 principles of programming cseb134...

31
CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1 BS (May 2012)

Upload: vanessa-rogers

Post on 18-Jan-2016

227 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

CHAPTER 1: Introduction to Computers and Programming

CSEB113 PRINCIPLES of PROGRAMMING CSEB134

PROGRAMMING I

byBadariah Solemon

1BS (May 2012)

Page 2: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Topics1. Overview of PC components 2. Programming languages 3. C programming language4. C programming environment5. C program at a glance

2BS (May 2012)

Page 3: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

OVERVIEW OF PC COMPONENTS

Topic 1

3BS (May 2012)

Page 4: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Elements of a Computer

HardwareHardware SoftwareSoftware

Program?

2

1

3

4 5

2

1

4BS (May 2012)

Page 5: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Main Memory• A semiconductor device which stores the

information necessary for a program to run.• Types:

1. ROM (Read Only Memory)• Contains information that is necessary for the computer

to boot up• The information stays there permanently even when the

computer is turned off.

2. RAM (Random Access Memory)• Contains instruction or data needed for a program to run• Got erased when the computer is turned off.

5BS (May 2012)

Page 6: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Anatomy of Main Memory• Memory may be conceptually viewed as an ordered sequence

of storage location called memory cells.• Information is stored in memory in bits• A memory cell is a group of bits called a byte• Each memory cell has a unique address

1000110010001100 A9A91011001010110010 A8A80111011101110111 A7A71101000011010000 A6A61001011110010111 A5A50001011000010110 A4A41101011011010110 A3A31001010110010101 A2A21001011010010110 A1A1

0111111001111110 B9B91011011110110111 B8B81101110011011100 B7B71001000010010000 B6B60000101000001010 B5B51001000110010001 B4B41010111010101110 B3B30111011001110110 B2B21000011010000110 B1B1

6BS (May 2012)

Ref: Tan and D’Orazio, C Programming for Engineering & Computer Science, New York: McGraw-Hill

Page 7: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

CPU• Does most of the work in executing a program• The CPU inside a PC is usually the

microprocessor• Three main parts:

1. Control Unit• Fetch instructions from main memory and put them in

the instruction register2. ALU (Arithmetic Logic Unit)

• Execute arithmetic operations3. Registers

• Temporarily store instructions or data fetched from memory

7BS (May 2012)

Page 8: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Secondary Storage Devices• Magnetic devices used to store a large amount

of information.• Store the software components or data

needed for the computer to execute its tasks.• Could be “read only” or “writable”.• Example:

8BS (May 2012)

Page 9: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Input/Output Devices• Input Devices

– Accepts information from the user and transforms it to digital codes that the computer can process

– Example:

• Output Devices– An interface by which the computer

conveys the output to the user– Example:

9BS (May 2012)

Page 10: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Computer Networks• Computers are now linked together in

networks – communicate• Types:

1. LAN ( )• Computers and other devices in a building are

connected by cables or wireless network

2. WAN ( )• Links many individual computers and LANs over a

large geographic area• The most well-known WAN is _____________

10BS (May 2012)

Page 11: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Software• Consists of programs that enable us to solve

problems with a computer

Operating Systems

Application SoftwareApplication Software

Utility Programs

Language Translation

System SoftwareSystem Software

11BS (May 2012)*Complete this list

Page 12: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

PROGRAMMING LANGUAGES

Topic 2

12BS (May 2012)

Page 13: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

What is Programming?• Programming is instructing a computer to do

something for you with the help of a programming language

• A programming language contains instructions for the computer to perform a specific action or a specific task:– 'Calculate the sum of the numbers from 1 to 10‘– 'Print “I like programming”‘– 'Output the current time'

13BS (May 2012)

Page 14: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

What is Programming?• Programming Language is different compared

to our everyday-language (natural language); spoken and written– Does not have to be 100% but still understandable– Depends on circumstances; the context – one

word can have many meaning depending on the situation

• For example:–OPERATE

14BS (May 2012)

Page 15: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

What is Programming?• The two roles of a programming language:

– Technical: It instructs the computer to perform tasks.

– Conceptual: It is a framework within which we organize our ideas about things and processes.

• In programming, we deal with two kind of things:– Data - representing 'objects' we want to

manipulate– Procedures -'descriptions' or 'rules' that define

how to manipulate data

15BS (May 2012)

Page 16: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Programming Language• Programming Language is a Formal Language

used to communicate to a computer.– Very specific (one word means one thing – context free)

since to 'talk' to a computer; to instruct a computer; our commands must be 100% clear and correct.

– Either Functional - tell what to do but not how (sum [1...10])

– or Imperative - describing the step by step to accomplish the task (Take the first number and add the next number to it; then add the next number to the sum; and …………; until you have reached 10 as number to be added. Then print the sum of all numbers)

16BS (May 2012)

Page 17: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Programming Language• Can be classified into as a special-purpose and

general-purpose programming languages.• Special-purpose : is design for a particular type of

application– Structured Query Language (SQL)

• General-purpose : can be used to obtain solutions for many types of problems1. Machine Languages2. Assembly Languages3. High-Level Languages

17BS (May 2012)

Page 18: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

1. Machine Language• The only language that the processor actually understand.• Consists of binary codes: 0 and 1• Example: 1101 0001 1000 0101 1000 0110 1111 0001 0100 1100 0101 1110 0101 1010 0001 1100

• Each of the lines above corresponds to a specific task to be done by the processor.

• Programming in machine code is difficult and slow since it is difficult to memorize all the instructions. Mistakes can happen very easily.

• Processor dependent (different machine language for different CPU) - not portable

18BS (May 2012)

Page 19: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

2. Assembly Language• Enable machine code to be represented in words and

numbers.• Example of a program in assembly language:

LOAD A, 9999LOAD B, 8282

• Cannot be processed directly by a computer, must be converted to machine language using assemblers

• Easier to understand compared to machine code but still is quite difficult to use.

• Each instruction corresponds to a specific machine code: lengthy programs.

• Processor dependent and not portable.

19BS (May 2012)

LOAD A, 9999LOAD B, 8282SUB BMOV C, A

LOAD A, 9999LOAD B, 8282SUB BMOV C, A

0111001100100011001110011110011100110010

Assembler

Page 20: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

3. High-level Language

20BS (May 2012)

Page 21: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

C PROGRAMMING LANGUAGE

Topic 3

21BS (May 2012)

Page 22: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

What is ‘C’?• Why ‘C’? - Because based on 'B'; developed at Bell

Laboratories• C was developed by Dennis Ritchie at Bell

Laboratories in 1972.• C combines the power of high-level languages with

the power of assembly languages.• Starts as a system implementation language, but has

evolved into general-purpose language and available for a wide variety of computers.

• C coupled with good program design techniques - generate well structured, easy to read and easy to maintain program

22BS (May 2012)

Page 23: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

What is ‘C’?• In 1983, the American National Standards

Institute (ANSI) set up X3J11, a Technical Committee to draft a proposal for the ANSI standard, which was approved in 1989 and referred to as the ANSI/ISO 9899 : 1990 or simply the ANSI C, which is now the global standard for C.

23BS (May 2012)

Page 24: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

C – An Imperative Language• C is a highly imperative language

– We must tell it exactly how to do what; – the means and functions to use;– which libraries to use;– when to add a new line;– when an instruction is finished;– in short: everything and anything…

• Hint: Observe the syntax in the next slide– Semantics – the meaning of the language within a given

context– Syntax - Syntax are the rules to join words

together in forming a correct expression or phrase.

24BS (May 2012)

Page 25: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Example of a C program

function for printing a character/string

25BS (May 2012) *Complete the labeling

Page 26: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

C PROGRAMMING ENVIRONMENT

Topic 4

26BS (May 2012)

Page 27: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

IDE• The system software necessary to develop C application

program are bundled into an integrated development environment (IDE)

• A typical IDE contains text editor, C compiler, preprocessor, libraries, other tools

• Components of C programming environment:– Language – features to carry out basic operations (Ex. Compare

data values, arithmetic operation)– Libraries – routines to carry out operations not part of the

language (Standard libraries – ex. #include <stdio.h> & programmer-defined libraries)

27BS (May 2012)

Page 28: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Ms Visual C++

28BS (May 2012)

Page 29: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

C PROGRAM AT A GLANCE

Topic 5

29BS (May 2012)

Page 30: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

A Simple C Program

Output:Output:

30BS (May 2012)

CONSOLE VIEW of the C program’s output

Page 31: CHAPTER 1: Introduction to Computers and Programming CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)

Summary• PC components

– Hardware : main memory, CPU, storage devices, I/O devices

– Software : system software & application software

• Programming languages– Machine language, assembly language, high-level

languages

• C as a highly imperative language• A typical C programming environment contains text editor, C compiler, preprocessor, libraries, other tools

31BS (May 2012)