chapter 01 c++

Upload: aljawad14

Post on 02-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Chapter 01 C++

    1/45

    INTRODUCTION TO

    COMPUTERS ANDPROGRAMMING

    Chapter 01

    1

  • 8/10/2019 Chapter 01 C++

    2/45

    1.1 Why Program?2

  • 8/10/2019 Chapter 01 C++

    3/45

    1-3

    Why Program?

    Computerprogrammable machine designed to

    follow instructions

    Programinstructions in computer memory to

    make it do somethingProgrammerperson who writes instructions

    (programs) to make computer perform a task

    SO, without programmers, no programs; without

    programs, a computer cannot do anything

  • 8/10/2019 Chapter 01 C++

    4/45

    1.2 Computer Systems: Hardware

    and Software4

  • 8/10/2019 Chapter 01 C++

    5/45

    1-5

    Main Hardware Component

    Categories:

    1. Central Processing Unit (CPU)

    2. Main Memory

    3. Secondary Memory / Storage

    4. Input Devices

    5. Output Devices

  • 8/10/2019 Chapter 01 C++

    6/45

    1-6

    Main Hardware Component

    Categories

    Figure 1-1

  • 8/10/2019 Chapter 01 C++

    7/45

    1-7

    Central Processing Unit (CPU)

    Comprised of:

    Control UnitRetrieves and decodes program instructions

    Coordinates activities of all other parts of computerArithmetic & Logic Unit

    Hardware optimized for high-speed numeric calculation

    Hardware designed for true/false, yes/no decisions

  • 8/10/2019 Chapter 01 C++

    8/45

  • 8/10/2019 Chapter 01 C++

    9/45

  • 8/10/2019 Chapter 01 C++

    10/45

    1-10

    Main Memory

    AddressesEach byte in memory is identified by

    a unique number known as an address.

    Above figure, the number 149 is stored in the byte with the address 16, and thenumber 72 is stored at address 23.

  • 8/10/2019 Chapter 01 C++

    11/45

    1-11

    Secondary Storage

    Non-volatile: data retained when program is

    not running or computer is turned off

    Comes in a variety of media:

    magnetic: floppy disk, hard drive

    optical: CD-ROM, DVD

    Flash drives, connected to the USB port

  • 8/10/2019 Chapter 01 C++

    12/45

    1-12

    Input Devices

    Devices that send information to the computer

    from outside

    Many devices can provide input:

    Keyboard, mouse, scanner, digital camera,microphone

    Disk drives, CD drives, and DVD drives

  • 8/10/2019 Chapter 01 C++

    13/45

    1-13

    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:

    Computer monitor and printer

    Disk drives

    Writable CD and DVD drives

  • 8/10/2019 Chapter 01 C++

    14/45

    1-14

    SoftwarePrograms That Run

    on a Computer

    Categories of software:

    Operating system: programs that manage thecomputer hardware and the programs that run onthem. Examples: Windows, UNIX, Linux

    Application software: programs that provideservices to the user. Examples: word processing,games, programs to solve specific problems

  • 8/10/2019 Chapter 01 C++

    15/45

    1.3 Programs and Programming

    Languages15

  • 8/10/2019 Chapter 01 C++

    16/45

    1-16

    Programs and

    Programming Languages

    A program is a set of instructions that the

    computer follows to perform a task

    We start with an algorithm, which is a set of

    well-defined steps.

  • 8/10/2019 Chapter 01 C++

    17/45

    1-17

    Example Algorithm for

    Calculating Gross Pay

  • 8/10/2019 Chapter 01 C++

    18/45

    1-18

    Machine Language

    Although the previous algorithm defines the

    steps for calculating the gross pay, it is not

    ready to be executed on the computer.

    The computer only executes machinelanguageinstructions.

  • 8/10/2019 Chapter 01 C++

    19/45

    1-19

    Machine Language

    Machine language instructions are binary

    numbers, such as

    1011010000000101

    Rather than writing programs in machine

    language, programmers useprogramming

    languages.

  • 8/10/2019 Chapter 01 C++

    20/45

    1-20

    Programs and

    Programming Languages

    Types of languages:

    Low-level: used forcommunication with computerhardware directly. Often writtenin binary machine code (0s/1s)directly.

    High-level: closer to humanlanguage

  • 8/10/2019 Chapter 01 C++

    21/45

    1-21

    Some Well-Known Programming

    Languages (Table 1-1 on Page 9)

    BASIC

    FORTRAN

    COBOL

    C

    C++

    C#

    Java

    JavaScript

    Python

    Ruby

    Visual Basic

  • 8/10/2019 Chapter 01 C++

    22/45

    1-22

    From a High-level Program to an

    Executable File

    a) Create file containing the program with a text editor.

    b) Run preprocessor to convert source file directives tosource code program statements.

    c) Run compiler to convert source program into machineinstructions.

    d) Run linker to connect hardware-specific code tomachine instructions, producing an executable file.

    Steps bd are often performed by a single commandor button click.

    Errors detected at any step will prevent execution offollowing steps.

  • 8/10/2019 Chapter 01 C++

    23/45

    1-23

    From a High-level Program to an

    Executable File

    Source Code

    Preprocessor

    ModifiedSource Code

    Compiler

    Object Code

    Linker

    Executable Code

  • 8/10/2019 Chapter 01 C++

    24/45

    1-24

    Integrated Development

    Environments (IDEs)

    An integrated development environment, or

    IDE, combine all the tools needed to write,

    compile, and debug a program into a single

    software application. Examples are Microsoft Visual C++, Turbo

    C++ Explorer, CodeWarrior, etc.

  • 8/10/2019 Chapter 01 C++

    25/45

    1-25

    Integrated Development

    Environments (IDEs)

  • 8/10/2019 Chapter 01 C++

    26/45

    1.4 What Is a Program MadeOf?

    26

  • 8/10/2019 Chapter 01 C++

    27/45

    1-27

    What Is a Program Made Of?

    Common elements in programminglanguages:

    Key Words

    Programmer-Defined IdentifiersOperators

    Punctuation

    Syntax

  • 8/10/2019 Chapter 01 C++

    28/45

    1-28

    Program 1-1

    1 // This program calculates the user's pay.2 #include 3 using namespace std;45 int main()6 {7 double hours, rate, pay;8

    9 // Get the number of hours worked.10 cout > hours;1213 // Get the hourly pay rate.14 cout > rate;

    1617 // Calculate the pay.18 pay = hours * rate;1920 // Display the pay.21 cout

  • 8/10/2019 Chapter 01 C++

    29/45

    1-29

    Key Words

    Also known as reserved words

    Have a special meaning in C++

    Can not be used for any other purpose

    Key words in the Program 1-1: using,

    namespace, int, double, and return.

  • 8/10/2019 Chapter 01 C++

    30/45

    1-30

    1 // This program calculates the user's pay.2 #include 3 using namespace std;45 int main()6 {7 double hours, rate, pay;

    89 // Get the number of hours worked.10 cout > hours;1213 // Get the hourly pay rate.14 cout > rate;

    1617 // Calculate the pay.18 pay = hours * rate;1920 // Display the pay.21 cout

  • 8/10/2019 Chapter 01 C++

    31/45

    P D fi d

  • 8/10/2019 Chapter 01 C++

    32/45

    1-32

    1 // This program calculates the user's pay.2 #include 3 using namespace std;45 int main()6 {7 double hours, rate, pay;

    89 // Get the number of hours worked.10 cout > hours;1213 // Get the hourly pay rate.14 cout > rate;

    1617 // Calculate the pay.18 pay = hours * rate;1920 // Display the pay.21 cout

  • 8/10/2019 Chapter 01 C++

    33/45

    1-33

    Operators

    Used to perform operations on data

    Many types of operators:

    Arithmetic - ex: +,-,*,/

    Assignmentex: =

    Some operators in Program1-1:

    > = *

  • 8/10/2019 Chapter 01 C++

    34/45

    1-34

    Operators

    1 // This program calculates the user's pay.2 #include 3 using namespace std;45 int main()6 {7 double hours, rate, pay;

    89 // Get the number of hours worked.10 cout > hours;1213 // Get the hourly pay rate.14 cout > rate;

    1617 // Calculate the pay.18 pay = hours * rate;1920 // Display the pay.21 cout

  • 8/10/2019 Chapter 01 C++

    35/45

    1-35

    Punctuation

    Characters that mark the end of a statement,

    or that separate items in a list

    In Program 1-1: ,and ;

  • 8/10/2019 Chapter 01 C++

    36/45

  • 8/10/2019 Chapter 01 C++

    37/45

    1-37

    Syntax

    The rules of grammar that must be followed

    when writing a program

    Controls the use of key words, operators,

    programmer-defined symbols, and punctuation

  • 8/10/2019 Chapter 01 C++

    38/45

    1-38

    Variables

    A variable is a named storage location in thecomputers memory for holding a piece ofdata.

    In Program 1-1 we used three variables: The hoursvariable was used to hold the

    hours worked

    The ratevariable was used to hold the pay

    rate Thepayvariable was used to hold the gross

    pay

  • 8/10/2019 Chapter 01 C++

    39/45

    1-39

    Variable Definitions

    To create a variable in a program you must write

    a variable definition (also called a variable

    declaration)

    Here is the statement from Program 1-1 that

    defines the variables:

    double hours, rate, pay;

  • 8/10/2019 Chapter 01 C++

    40/45

    1-40

    Variable Definitions

    There are many different types of data, whichyou will learn about in this course.

    A variable holds a specific type of data.

    The variable definition specifies the type ofdata a variable can hold, and the variable

    name.

  • 8/10/2019 Chapter 01 C++

    41/45

    1-41

    Variable Definitions

    Once again, line 7 from Program 1-1:

    double hours, rate, pay;

    The word doublespecifies that the variables

    can hold double-precision floating point

    numbers. (You will learn more about that in

    Chapter 2)

  • 8/10/2019 Chapter 01 C++

    42/45

    1.5 Input, Processing, andOutput

    42

  • 8/10/2019 Chapter 01 C++

    43/45

    1-43

    Input, Processing, and Output

    Three steps that a program typically performs:

    1) Gather input data: from keyboard

    from files on disk drives

    2) Process the input data

    3) Display the results as output: send it to the screen

    write to a file

  • 8/10/2019 Chapter 01 C++

    44/45

  • 8/10/2019 Chapter 01 C++

    45/45

    The Programming Process