Docsity

Simple Data Types - Problem Solving and Structured Programming - Lecture Slides, Slides of Advanced Computer Programming

Partial preview of the text

Download Simple Data Types - Problem Solving and Structured Programming - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 10

Simple Data

Types: Built-In

and User-

Defined

Chapter 10 Topics

  • External and Internal Representations of Data
  • Integral and Floating Point Data Types
  • Using Combined Assignment Operators
  • Prefix and Postfix Forms of Increment and

Decrement Operators

  • Using Ternary Operator
  • Using Type Cast Operator
  • Using an Enumeration Type
  • Creating and Including User-Written Header

Files

By definition,

The size of a C++ char value is always 1 byte

exactly one byte of memory space

Sizes of other data type values in C++ are machine-

dependent

4

„A‟

Using one byte (= 8 bits)

How many different numbers can be

represented using 0’s and 1’s?

Each bit can hold either a 0 or a 1. So there are just two choices for each bit, and there are 8 bits.

2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 2^8 = 256

5

Some Integral Types

7

Type Size in Bytes Minimum Value Maximum Value

char 1 -128 127

short 2 -32,768 32,

int 2 -32,768 32,

long 4 -2,147,483,648 2,147,483,

NOTE: Values given for one machine; actual sizes are machine-dependent

Data Type bool

  • Domain contains only 2 values, true and false
  • Allowable operation are the logical (!, &&, ||) and relational operations

The only guarantees made by C++

are...

1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

1 <= sizeof (bool) <= sizeof (long)

sizeof (float) <= sizeof (double) <= sizeof (long double)

char is at least 8 bits

short is at least 16 bits

long is at least 32 bits

Exponential (Scientific) Notation

2.7E4 means 2.7 x 10

2.7E-4 means 2.7 x 10

11

More about Floating Point Types

  • Floating point constants in C++ like 94.6 without a suffix are of type double by default
  • To obtain another floating point type constant a suffix must be used - The suffix F or f denotes float type, as in 94.6F - The suffix L or l denotes long double, as in 94.6L

13

Header Files

climits and cfloat

  • Contain constants whose values are the maximum and minimum for your machine
  • Such constants are FLT_MAX, FLT_MIN, LONG_MAX, LONG_MIN

14

#include using namespace std;

.. . cout << “Maximum long is “ << LONG_MAX << endl; cout << “Minimum long is “ << LONG_MIN << endl;

A statement to subtract 10 from weight

int weight;

cin >> weight;

weight = weight - 10;

OR

weight -= 10;

A statement to divide money by 5.

float money;

cin >> money;

money = money / 5.0;

OR

money /= 5.0;

A statement to raise cost 15%

float cost;

cin >> cost;

cost = cost + cost * 0.15;

OR

cost = 1.15 * cost;

OR

cost *= 1.15;

Which form to use?

  • When the increment (or decrement) operator is

used in a “ stand alone ” statement to add one (or

subtract one) from a variable’s value, it can be used

in either prefix or postfix form

20

dogs -- ; -- dogs;

USE EITHER