Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

Embed Size (px)

Citation preview

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    1/13

    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 4

    Data Representation & Storage (contd)

    Simple C++ Programs (Chapter 2)

    Dr. Eyhab Al-Masri

    ENGR 1200U

    Winter 2013 - UOIT

    Convert the following:

    2410 = ?2

    24 23 22 21 20

    16 8 4 2 1

    1 1 0 0 0

    16 8

    110002=2410

    24 / 2 = 12 rem 0

    12 / 2 = 6 rem 0

    6 / 2 = 3 rem 0

    3 / 2 = 1 rem 1

    1 / 2 = X rem 1 MSB

    LSB

    110002=2410

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    2/13

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    3/13

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    4/13

    ENGR 1200U

    Winter 2013 - UOIT

    Computers use electricity for data processingand storage

    Electricity can flow through switches If switch is closed, electricity flows If switch is open, electricity does not flow

    Computers have switches to represent data Switches have only two states: ON and OFF Binary has two digits: 0 and 1

    1 for the ON state, 0 for the OFF state

    ENGR 1200U

    Winter 2013 - UOIT

    Computer manufacturers express capacity ofmemory in terms of bytes

    One byte can represent many different kinds ofdata This depends on how the computer creates the byte

    0 1 0 0 0 0 0 1

    0 64 0 0 0 0 0 165

    ASCII A

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    5/13

    ENGR 1200U

    Winter 2013 - UOIT

    0 1 0 0 0 0 1 0

    0 64 0 0 0 0 2 066

    0 1 0 0 0 0 0 1

    0 64 0 0 0 0 1 167

    ASCII C

    ASCII B

    ENGR 1200U

    Winter 2013 - UOIT

    Digital photographs are composed of a grid of coloreddots A grid of pixels represents graphic data

    Examples: pictures, frames of a movie, animations

    Pixel is short for Picture Element In simple graphics, a byte can represent a single pixel

    1 byte can hold 256 different integers (0 255)

    10000000

    01111100

    11111010

    11001100

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    6/13

    ENGR 1200U

    Winter 2013 - UOIT

    Several graphic standard formats

    Bitmap (.bmp) store every pixel of image

    JPEG & GIF compressed formats

    Instead of storing every pixel, store a pattern of pixels

    Size of image is important particularly when sending picturesvia email, downloading them over the Web, etc

    Primary goal of using these compressed formats is to shrinksize of image files without altering the quality of the image

    A compression ratio is commonly adjusted to make graphic file

    size smaller

    ENGR 1200U

    Winter 2013 - UOIT

    Example: uncompressed, 84 kilobytes (JPEG)

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    7/13

    ENGR 1200U

    Winter 2013 - UOIT

    Example: 20 compression, 37 kilobytes (JPEG)

    ENGR 1200U

    Winter 2013 - UOIT

    Example: 40 compression, 20 kilobytes (JPEG)

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    8/13

    ENGR 1200U

    Winter 2013 - UOIT

    Example: 60 compression, 11 kilobytes (JPEG)

    ENGR 1200U

    Winter 2013 - UOIT

    Example: 80 compression, 8 kilobytes (JPEG)

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    9/13

    ENGR 1200U

    Winter 2013 - UOIT

    Example: 100 compression, 5 kilobytes (JPEG)

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

    Simple C++ Programs (Chapter 2)

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    10/131

    ENGR 1200U

    Winter 2013 - UOIT

    /*--------------------------------------------------------

    * Program chapter1_1

    * This program computes the distance between two points.

    */

    #include // Required for cout, endl.

    #include // Required for sqrt()

    using namespace std;

    int main()

    {

    // Declare and initialize objects.

    double x1(1), y1(5), x2(4), y2(7),

    side1, side2, distance;

    // Compute sides of a right triangle.

    side1 = x2 - x1;

    side2 = y2 - y1;

    distance = sqrt(side1*side1 + side2*side2);

    // Print distance.

    cout

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    11/131

    ENGR 1200U

    Winter 2013 - UOIT

    /*--------------------------------------------------------

    * Program chapter1_1

    * This program computes the distance between two points.

    */

    #include // Required for cout, endl.

    #include // Required for sqrt()

    using namespace std;

    int main()

    {

    // Declare and initialize objects.

    double x1(1), y1(5), x2(4), y2(7),

    side1, side2, distance;

    // Compute sides of a right triangle.

    side1 = x2 - x1;

    side2 = y2 - y1;

    distance = sqrt(side1*side1 + side2*side2);

    // Print distance.

    cout

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    12/131

    ENGR 1200U

    Winter 2013 - UOIT

    /*--------------------------------------------------------

    * Program chapter1_1

    * This program computes the distance between two points.

    */

    #include // Required for cout, endl.

    #include // Required for sqrt()

    using namespace std;

    int main()

    {

    // Declare and initialize objects.

    double x1(1), y1(5), x2(4), y2(7),

    side1, side2, distance;

    // Compute sides of a right triangle.

    side1 = x2 - x1;

    side2 = y2 - y1;

    distance = sqrt(side1*side1 + side2*side2);

    // Print distance.

    cout

  • 8/14/2019 Lecture 4 - Data Rep. and Storage cont. - Simple C++ Prog.pdf

    13/13

    ENGR 1200U

    Winter 2013 - UOIT

    /*--------------------------------------------------------

    * Program chapter1_1

    * This program computes the distance between two points.

    */

    #include // Required for cout, endl.

    #include // Required for sqrt()

    using namespace std;

    int main()

    {

    // Declare and initialize objects.

    double x1(1), y1(5), x2(4), y2(7),

    side1, side2, distance;

    // Compute sides of a right triangle.

    side1 = x2 - x1;

    side2 = y2 - y1;

    distance = sqrt(side1*side1 + side2*side2);

    // Print distance.

    cout