introduction to computers, programs, and c++staff.columbiacollege.bc.ca/msamiei/apsc...

40
Introduction to Computers, Programs, and C++ APSC 160 – Chapter 1 1

Upload: vankhanh

Post on 26-Mar-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Introduction to Computers, Programs,

and C++ APSC 160 – Chapter 1

1

Page 2: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Objectives • To understand computer basics, programs, and operating

systems (1.2-1.4)

• To describe the history of C++ (1.5)

• To write a simple C++ program for console output (1.6)

• To understand the C++ program-development cycle (1.7)

• To know programming style and documentation (1.8)

• To explain the differences between syntax errors, runtime

errors, and logic errors (1.9)

2

Page 3: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Computers

3

• Today, billions of general-purpose computers, and embedded computers in cell phones, smartphones, tablet computers, home appliances, automobiles and more are used

• Computers can perform computations, store and process data, and make logical decisions much faster than human beings

• Computers are general purpose tools to solve problems o Computation – Military, Industrial, Science, …

o Data Storage / Retrieval – Science, Business, …

o Office applications – Word, Excel, PowerPoint, …

o Processing – Applying standardized routines to data, …

o Communications – Email, Internet, Web, Smart phones, Cell phones, …

o Entertainment – Games, Music, Cameras, Video, …

o Social Networking – Myspace, Facebook, …

o Industrial Automation – Robotic, Factories …

Page 4: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

What is a computer? • A computer includes hardware and software

o hardware comprises the visible, physical elements of a computer

o software provides the invisible instructions that control the hardware

and make it perform specific tasks

• A computer hardware consists of a CPU, memory, storage

devices, I/O devices, and communication devices

4

CPU

e.g. Disk, CD, and Tape

Input Devices

e.g. Keyboard, Mouse

e.g. Monitor, Printer

Communication Devices

e.g. Modem, and NIC

Storage Devices

Memory Output Devices

Bus

Page 5: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

A Simple Computer Model

5

Main Memory

Stored Instructions Stored Data

CPU (Central

Processing Unit)

Fetches then Executes

Instructions

Output Device Input Device

Page 6: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Bus

6

• A computer's components are interconnected by a

subsystem called a bus

o a system of roads running among the computer's

components

• Data and power travel along the bus from one part of

the computer to another

• In personal computers, the bus is built into the

computer's motherboard

o a circuit case that connects parts of a computer together

Page 7: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

CPU

7

• The central processing unit (CPU) is the computer’s brain

o It retrieves instructions from memory and executes them

• The CPU usually has two components

o a control unit: controls and coordinates the actions of the other components

o an arithmetic/logic unit (ALU): performs numeric and logical operations

• CPUs are built on small silicon semiconductor chips with millions of tiny

electric switches, called transistors, for processing information

• CPUs were originally developed with one core

o core is the part of processor that performs reading and executing of instructions

• To increase CPU processing power, CPUs contain multiple cores

o two, three, or even four separate cores

Page 8: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Clock

8

• Every computer has an internal clock, which emits electronic pulses at a constant rate

• These pulses are used to control and synchronize the pace of operations

• A higher clock speed enables more instructions to be executed in a given period

• The unit of measurement of clock speed is the hertz (Hz), with 1 hertz equaling 1 pulse per second

• In the 1990s, computers measured clocked speed in megahertz (MHz)

• The clock speed of a computer is now usually stated in gigahertz, 1000 Hz (GHz)

Page 9: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Bits and Bytes

9

• How information (data and programs) are stored in a computer?

• A computer is a series of switches in two states: on or off

• Storing information in a computer is a matter of setting a sequence of switches on or off o If the switch is on, its value is 1. If the switch is off, its value is 0

o These 0s and 1s are interpreted as digits in the binary number system and are called bits (binary digits)

• The minimum storage unit in a computer is a byte which contains eight bits

• Data of various kinds, such as numbers and characters, are encoded as a series of bytes

Page 10: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Encoding

10

• An encoding scheme is a set of rules that translates characters, numbers,

and symbols into data the computer can work with

• To represent English language, we need to encode

o 26 uppercase and 26 lowercase letters

o 10 digits

o 10 arithmetic characters

o 20 punctuation characters

o 3 white space characters (new line, tab, backspace)

• Arranging 2 bit values into n-length sequences creates 2n symbols

o With 8 bits 2^8=256 values from 0 (00000000) to 255 (11111111) are created

• How many characters are needed to represent 95 symbols?

o 26 = 64 27 = 128

Page 11: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Encoding

11

• The popular ASCII (American Standard Code for Information Interchange)

encoding scheme uses 8 bits to encode characters

o An early standard used 7 bits

• Unicode system with 2 bytes encoding represents more than 65,000

characters

• A computer's storage capacity is measured in multiples of bytes

o A kilobyte (KB) is about 1,000 bytes

o A megabyte (MB) is about 1 million bytes

o A gigabyte (GB) is about 1 billion bytes

o A terabyte (TB) is about 1 trillion bytes

Page 12: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

ASCII Table

12

Page 13: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Numeral Systems - Binary

13

• Binary numeral system, or base-2 system

o represents numeric values using two symbols; typically 0

and 1

o numbers represented in this system are commonly

called binary numbers

• Computers use binary system

o because of its straightforward implementation in digital

electronic circuitry using logic gates

Page 14: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Numeral Systems - Conversion

14

• converting binary to decimal o 1011 in binary (1*2^3)+(0*2^2)+(1*2^1)+(1*2^0)=8+0+2+1=11

• converting decimal to binary o 11 / 2 = 5 r 1

5 / 2 = 2 r 1

2 / 2 = 1 r 0

1 / 2 = 0 r 1 11 1011

o 256 / 2 = 128 r 0

128 / 2 = 64 r 0

64 / 2 = 32 r 0

32 / 2 = 16 r 0

16 / 2 = 8 r 0

8 / 2 = 4 r 0

4 / 2 = 2 r 0

2 / 2 = 1 r 0

1 / 2 = 0 r 1 256 100,000,000

decimal number

binary number

0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101

14 1110

15 1111

Page 15: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Numeral Systems - Hexadecimal

15

• Writing the sequence of 0 and 1 bits is long, tedious, and error-prone

• Hexadecimal (base-16 or hex)

o human-friendly representation of binary-coded values

o uses 16 symbols

• 0 to 9 to represent values 0 to 9

• A to F to represent values 10 to 15

• A 4-bit sequence creates 16 (2^4) values

• Conversion is easy

o group bits in 4

• 1101 1000 is D8 (Hex)

• 10 1101 (0010 1011) is 2B (Hex)

• 16F (Hex) is 0001 0110 1111

Page 16: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Numeral Systems - Conversion

16

decimal binary hexadecimal

0 0000 0

1 0001 1

2 0010 2

3 0011 3

4 0100 4

5 0101 5

6 0110 6

7 0111 7

8 1000 8

9 1001 9

10 1010 A

11 1011 B

12 1100 C

13 1101 D

14 1110 E

15 1111 F

Page 17: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Memory

17

• Memory stores programs as well as data

• Every byte in the memory has a unique address used

to locate the byte for storing and retrieving the data

• Since the bytes in the memory can be accessed in any order, the memory

is also referred to as random-access memory (RAM)

• Computers have at least 1 GB of RAM, more commonly 2 to 4 GB

• The more RAM a computer has, the faster it can operate

• The current content of a memory byte is lost whenever new information is

placed in it

Page 18: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Storage Devices

18

• A computer's memory is a volatile form of data storage o Information is lost when the system’s power is turned off

• Programs and data are permanently stored on storage devices o A computer usually has at least one hard disk drive

• A program and its data must be moved into computer’s

memory before they can be executed by the CPU

• There are three main types of storage devices o Magnetic disk drives

o Optical disc drives (CD and DVD)

o USB flash drives

Page 19: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Input and Output Devices

19

• I/O devices let the user communicate with the computer

• A keyboard is a device for entering input

• A mouse is a pointing device to move a cursor on screen or

click on objects to perform an action

• A monitor displays information (text and graphics)

o The screen resolution specifies the number of pixels in horizontal and

vertical dimensions

• The higher the resolution, the sharper and clearer the image

o The dot pitch is the amount of space between pixels, measured in

millimeters

• The smaller the dot pitch, the sharper the display

Page 20: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Communication Devices

20

• Computers can be networked through communication devices

• A dial-up modem uses a phone line and can transfer data at speeds up to

56,000 bps (bits per second)

• A digital subscriber line (DSL) connection uses a standard phone line, but it

can transfer data 20 times faster than a dial-up modem

• A cable modem uses the cable TV line maintained by the cable company

and is generally faster than DSL

• A network interface card (NIC) connects a computer to a local area

network (LAN) which can transfer data at 1,000 million bps

• Wireless networking is now extremely popular to connect to a local area

network and the Internet

Page 21: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Programming

21

• Computer programming is the process of designing, writing,

testing, debugging, delivering, and maintaining the source

code of computer programs

• The purpose of programming is to create a program that

performs specific operations or exhibits a certain desired

behavior

• A programming language is an artificial language designed to

communicate instructions to a computer

Page 22: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Programming Languages

22

1. Machine Language

o generally consist of numbers (ultimately reduced to 1s and 0s)

o defined by computer’s hardware design

o Any computer can directly understand only its own machine

language

o Programming in machine language is slow and tedious

2. Assembly Language

o English-like abbreviations to represent elementary operations

o Translator programs called assemblers convert assembly-language

programs to machine language

Page 23: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Compiler Source File Object File Linker Executable File

Programming Languages

23

3. High-Level Languages o A program written in a high-level language is called a source program

• single statements, almost like everyday English, to accomplish substantial tasks

o Translator programs called compilers convert source programs into machine language called an object program

• it is often linked with other supporting library code before the object can be executed on the machine

o Interpreter programs execute high-level language programs directly, but slower than compiled programs

Page 24: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Programming Languages

24

Page 25: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Operating Systems

• Major tasks of an operating system

o Controlling and monitoring system activities

o Allocating and assigning system resources

o Scheduling operations

25

• The operating system (OS) manages and controls a computer's activities

• Popular operating systems for general-purpose computers are Microsoft Windows, Mac OS, and Linux

• Application programs, such as a Web browser or a word processor, cannot run without an operating system

Page 26: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

History of C++

26

• C++ is a general-purpose, object-oriented programming (OOP) language evolved from C o C developed by Dennis Ritchie of ATT/Bell Labs 1970s

• First used for writing the Unix operating system and replaced Assembly for this purpose

• Linux was also written in C, and moved to C++

• In the 1980s Object Oriented capabilities were added, making C a subset of C++

• Open source compilers for C and C++ (gnu C and others)

• OS X and iOS by Apple were written in Objective C o a quite different object-oriented extension of the earlier C language

Page 27: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

A simple program in C++

27

// -----------------------------------------

// A simple program in C++ to display

// a message on the screen.

// These are line comments (for programmers)

// -----------------------------------------

#include <iostream>

using namespace std;

int main() {

// Display Welcome to C++ to the console

cout << "Welcome to C++!" << endl;

return 0; //Let the O/S now all is well

} // End main function

Page 28: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

A simple program in C++ • The first line in the program

#include <iostream>

is a compiler preprocessor directive that tells the compiler to include the iostream library in this program to support console input and output

• C++ library contains pre-defined code for developing C++ programs

• The statement using namespace std;

tells the compiler to use the standard namespace

• Namespace is a mechanism to avoid naming conflicts in a large program

28

Page 29: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

A simple program in C++ • Every C++ program is executed from a main function

o A function is a construct that contains statements enclosed within braces { }

• Every statement in C++ must end with a semicolon (;) statement terminator

• cout (pronounced see-out) stands for console output and displays a message to the

console

o The << operator, the stream insertion operator, sends a string to the console

o A string must be enclosed in quotation marks

• endl stands for end line

o It ends a line and ensures that the output is displayed immediately

• The statement

return 0;

is placed at the end of every main function to exit the program

o The value 0 indicates that the program has terminated with a successful exit

29

Page 30: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

A simple program in C++ • Comments help programmers communicate and understand the program

• Comments are not programming statements thus ignored by the compiler

• A line comment is preceded by two slashes (//) on a line

• A block comment enclosed between /* and */ on one or several lines

• Examples:

// This application program prints Welcome to C++!

/* This application program prints Welcome to C++! */

/* This application program

prints Welcome to C++! */

• Keywords, or reserved words, have a specific meaning to the compiler and cannot be used in the program for other purposes

o There are four keywords in this program: using, namespace, int, and return

30

Page 31: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Extending the simple program

31

// -----------------------------------

// A simple program in C++ to display

// three messages on the screen.

// These are line comments.

// -----------------------------------

#include <iostream>

using namespace std;

int main() {

// Display three messages to the console

cout << "Hello World!" << endl;

cout << "Welcome to C++!" << endl;

cout << "Programming is fun!" << endl;

return 0; //Let the O/S now all is well

} // End main function

Page 32: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

A Simple Computation in C++

32

//--------------------------------------------------------

// Compute: A simple computation C++ demo

// It calculates the an expression and prints the result.

//--------------------------------------------------------

#include <iostream>

using namespace std;

int main() {

/* Allocate a floating point variable */

float result;

cout << "Compute: \n"; // Announce program

result = (10.5 + 2.0 * 3.0)/(45.0 – 3.5);// Compute the expression

cout << "Result = " << result << "\n"; // Print the result

return 0; // Let O/S know all went well

} // End main function

Page 33: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

C++ Development Cycle

33

• The C++ development process consists of creating/modifying source code,

compiling, linking and executing programs

Page 34: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Phase 1: Creating a C++ Program

34

• Phase 1 consists of editing a file

• This is accomplished with an editor program

• Software packages for the C/C++ integrated program development

environments such as Microsoft Visual Studio have editors that are

integrated into the programming environment

• You type a C++ program with the editor, make corrections if

necessary, then store the program on a secondary storage device

such as a hard disk

• C++ program file names should end with the .cpp extension

• The output of editing phase is a source code

Page 35: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Phases 2&3: Preprocessing & Compiling a C++ Program

35

• After creating a program you give the command to compile

the program

• In a C++ system, a preprocessor program executes

automatically before the compiler’s translation phase begins

(Phase 2)

o certain manipulations are performed on the program before

compilation

o usually includes other files in the file to be compiled

• In Phase 3, the compiler translates the C++ program into

machine-language code (or object code)

Page 36: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Phase 4: Linking

36

• C++ programs typically contain references to functions defined

elsewhere, such as in the standard libraries

• The object code produced by the C++ compiler typically

contains "holes" due to these missing parts

• A linker links the object code with the code for the missing

functions to produce an executable image (with no missing

pieces)

• If the program compiles and links correctly, an executable file

will be produced at this phase

Page 37: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Phase 5: Loading

37

• Before a program can be executed, the program must be

placed in memory

o the loader takes the executable image from disk and transfers it to

memory

• Additional components from shared libraries that support the

program are also loaded

Page 38: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Phase 6: Execution

38

• Finally, the computer, under the control of its CPU, executes

the program one instruction at a time

Page 39: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Programming Style and Documentation

39

• Good programming style and documentation make a program

easy to read and help preventing errors

• Appropriate Comments

o a summary at the beginning of the program to explain what the

program does, its key features, and any unique technique

o introduce each major step

o concise comments

• Proper Indentation and Spacing Lines

o a consistent indentation style makes programs clear and easy to read,

debug, and maintain

Page 40: Introduction to Computers, Programs, and C++staff.columbiacollege.bc.ca/msamiei/APSC 160/lectures/… ·  · 2016-05-12o a system of roads running among the computer's ... controls

Programming Errors

40

• Programs do not always work on the first try

o Each of the phases can fail because of various errors

o You would return to the edit phase, make the corrections and proceed through the remaining phases again to determine that the corrections work properly

• A syntax error or compile error occurs when the compiler cannot recognize a statement because it violates the rules of the language, e.g. missing a semicolon

• Runtime or execution-time errors occur as a program runs, e.g. division-by-zero or enter a string instead of a number as input

• Logic errors occur when a program does not perform the way it was intended

o Division of integers

o cout << (9 / 5) * 35 + 32 << endl;