+ All Categories
Home > Documents > Lecture 6 - Simple C++ Programs pt 2.pdf

Lecture 6 - Simple C++ Programs pt 2.pdf

Date post: 04-Jun-2018
Category:
Upload: sunnyopg
View: 220 times
Download: 0 times
Share this document with a friend

of 16

Transcript
  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    1/16

    1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann

    ENGR 1200U Introduction to Programming

    Lecture 6

    Simple C++ Programs (Chapter 2) (contd)

    Dr. Eyhab Al-Masri

    ENGR 1200U

    Winter 2013 - UOIT

    A symbolic constant is defined in a declaration statementusing the modifier const.

    A symbolic constant allocates memory for an object thatcan not be modified during execution of the program. Any attempt to modify a constant will be flagged as a syntax

    error by the compiler.

    A symbolic constant must be initialized in the declarationstatement.

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    2/16

    1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann

    C++ Operators

    ENGR 1200U

    Winter 2013 - UOIT

    The assignment operator (=) is used in C++ toassign a value to a memory location.

    The assignment statement:x1 = 1. 0;

    assigns the value 1. 0 to the variable x1.

    Thus, the value 1. 0 is stored in the memory location

    associated with the identifier x1.(Note: x1 must have been previously declared.)

    0 0 0 0 0 1 0 0

    8-bit representation

    Assignment Operators

    integer part fractional part

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    3/16

    ENGR 1200U

    Winter 2013 - UOIT

    ENGR 1200U

    Winter 2013 - UOIT

    Because different types are different representations,frequently we need to convert between types.

    Sometimes these conversions may loose information.

    Conversion from lower types to higher types results in noloss of information.

    Conversion from higher types to lower types may looseinformation.

    High:

    Low:

    long doubledoublefloatlong integerintegershort integer

    Assignment Operators contd)

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    4/16

    ENGR 1200U

    Winter 2013 - UOIT

    Assignment operators must not be confused

    with equality (==)

    stores a new value (in this case the

    value of y1) in x1 replacing any

    previously stored value

    Assignment Operators contd)

    ENGR 1200U

    Winter 2013 - UOIT

    Increment Operator ++ post increment x++; pre increment ++x;

    Decrement Operator - - post decrement x- -; pre decrement - -x;

    For examples assume k=5 prior to executing thestatement.

    m= ++k; // both m and k become 6

    n = k- -; // n becomes 5 and k becomes 4

    Increment and Decrement Operatorscontd)

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    5/16

    ENGR 1200U

    Winter 2013 - UOIT

    ENGR 1200U

    Winter 2013 - UOIT

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    6/16

    ENGR 1200U

    Winter 2013 - UOIT

    If c = 5, then

    cout

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    7/16

    ENGR 1200U

    Winter 2013 - UOIT

    Most programs perform arithmetic calculations

    ENGR 1200U

    Winter 2013 - UOIT

    The five operators (* / % + -) arebinary operators - operators that require twoarguments (i.e. operands).

    C++ also include operators that require only asingle argument unary operators. For example, a plus or a minus sign preceding an

    expression can be unary operators: (e.g. - x2).

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    8/16

    ENGR 1200U

    Winter 2013 - UOIT

    Division between two integers results in aninteger.

    The result is truncated, not rounded

    Example:The expression 5/3 evaluates to 1The expression 3/6 evaluates to 0

    Integer Division and Remainder

    ENGR 1200U

    Winter 2013 - UOIT

    The % operator computes the remainder of aninteger division.

    It is called the modulus oper tor(also modulo and mod)

    It has nothing to do with the % key on a calculator

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    9/16

    ENGR 1200U

    Winter 2013 - UOIT

    Hi, Im Penny.

    Integer Division and Remainder

    ENGR 1200U

    Winter 2013 - UOIT

    You wanna break

    me open for what?

    Whats a dollar?

    Integer Division and Remainder

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    10/161

    ENGR 1200U

    Winter 2013 - UOIT

    Time to break open the piggy bank.

    You want to determine the value in dollars and

    cents stored in the piggy bank.

    You obtain the dollars through an integer division

    by 100.

    The integer division discards the remainder.

    To obtain the remainder, use the % operator:

    int pennies = 1729;

    int dollars = pennies / 100; // Sets dollars to 17int cents = pennies % 100; // Sets cents to 29

    Integer Division and Remainder

    ENGR 1200U

    Winter 2013 - UOIT

    dollars = / 100;

    cents = % 100;

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    11/161

    ENGR 1200U

    Winter 2013 - UOIT

    When a floating-point value is assigned to an integer variable,the fractional part is discarded:

    double price = 2.55;

    int dollars = price;

    // Sets dollars to 2

    You probably want to round to the nearest integer.To round a positive floating-point value to the nearest integer,add 0.5 and then convert to an integer:

    int dollars = price + 0.5;

    // Rounds to the nearest integer

    ENGR 1200U

    Winter 2013 - UOIT

    What about this?

    Inside the parentheses is easy:

    1 + (r / 100)But that raised to the n?

    1001

    r n

    b

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    12/161

    ENGR 1200U

    Winter 2013 - UOIT

    In C++, there are no symbols for powers and roots.To compute them, you must call functions.

    The C++ library defines many mathematical functionssuch as sqrt (square root) andpow (raising to apower).

    To use the functions in this library, called the cmathlibrary, you must place the line:

    #include at the top of your program file

    It is also necessary to includeusing namespace std;at the top of your program file.

    ENGR 1200U

    Winter 2013 - UOIT

    The power function has the base followed by a comma

    followed by the power to raise the base to:

    pow(base, exponent)

    Using thepow function:

    b + pow(1 + r / 100, n) 1001

    r n

    b

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    13/161

    ENGR 1200U

    Winter 2013 - UOIT

    ENGR 1200U

    Winter 2013 - UOIT

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    14/161

    ENGR 1200U

    Winter 2013 - UOIT

    Binary operations on two values of same typeyield a value of that type (e.g. dividing twointegers results in an integer).

    Binary operations between values of differenttypes is a mixed operation. Value of the lower type must be converted to the higher

    type before performing operation.

    Result is of the higher type.

    Mixed Operations

    ENGR 1200U

    Winter 2013 - UOIT

    The cast operator The cast operator is a unary operator that requests that thevalue of the operand be cast, or changed, to a new type for

    the next computation. The type of the operand is notaffected. Example:

    int count(10), sum(55);

    double average;

    average = (double)sum/count;

    Memory snapshot:

    int count

    int sum

    double average

    10

    55

    5.5

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    15/161

    ENGR 1200U

    Winter 2013 - UOIT

    Overflow answer too large to storeExample: using 16 bits for integersr esul t = 32000 + 532;

    Exponent overflow answers exponent is too largeExample: using float, with exponent range 38 to 38r esul t = 3. 25e28 * 1. 0e15;

    Exponent underflow answers exponent too small

    Example: using float, with exponent range 38 to 38r esul t = 3. 25e- 28 * 1. 0e- 15;

    ENGR 1200U

    Winter 2013 - UOIT

    operator example equivalent statement+= x+=2; x=x+2;

    - = x-=2; x=x- 2;

    *= x*=y; x=x*y;

    / = x/=y; x=x/ y;

    %= x%=y; x=x%y;

  • 8/14/2019 Lecture 6 - Simple C++ Programs pt 2.pdf

    16/16

    ENGR 1200U

    Winter 2013 - UOIT

    C++ follows rules of operator precedence, which are generally the sameas those followed in algebra

    ENGR 1200U

    Winter 2013 - UOIT


Recommended