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

During the course of work of the programming, we learn the core of the programming. The main points disucss in these lecture slides are:Simple Data Types, Built-In and User-Defined, Internal Representations, Floating Point Data Types, Combined Assignment Operators, Postfix Forms of Increment, Ternary Operator, Type Cast Operator, Enumeration Type

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banamala
banamala 🇮🇳

4.4

(19)

114 documents

1 / 54

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 10
Simple Data
Types: Built-In
and User-
Defined
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

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