/
Data Types, Variables   & Arithmetic Data Types, Variables   & Arithmetic

Data Types, Variables & Arithmetic - PowerPoint Presentation

ellena-manuel
ellena-manuel . @ellena-manuel
Follow
368 views
Uploaded On 2018-03-13

Data Types, Variables & Arithmetic - PPT Presentation

C Integer Types Data Size in s igned unsigned Type bytes Range Range short 2 32767 0 to 65553 int 4 ID: 649249

variable type tax float type variable float tax rate int integer double radius types statement constants identifier change program

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Data Types, Variables & Arithmetic" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Data Types, Variables

& ArithmeticSlide2

C++ Integer Types

Data

Size

in

s

igned

unsigned

Type

bytes

Range

Range

short

2

±

32,767

0 to

65,553

int

4

~

±2 billion

0 to ~4

billion

long

4 (same as

int

,

currently

)

long

long

8 ~ ±

9x10

18

0 to ~1.8x10

19

On a 64-bit CPU. Size/Range vary by CPU model and Word size.Slide3

C++ Integer Types

s

igned

vs.

unsignedunsigned short x; //range 0 to 65553signed short x; //range ± 32767short x; //assumed signedSlide4

C++ Float Types

Type

Size # significant digitsfloat 4 bytes 7double 8 bytes 15 There are (usually) no unsigned floats or doubles.Using unsigned float

in a program will result in:

- a compiler error (must be corrected)

or

- a compiler warning (compiler ignores

unsigned

)Slide5

C++ Float Types

Floats are stored in

scientific notation

as two integer values

Mantissa: digits with an assumed radix point after the first digit. Exponent: (actually an exponent of 2 in binary) Example: (in decimal)1234.567 = 1.234567 x 103Mantissa = 1234567 Exponent = 3Slide6

C++ Float Types

Addition in Scientific Notation

1.234567 x 10

4

Exponents must match!+ 7.654321 x 102 (4 - 2 = 2)123.456700 x 102

Move radix point, subtract from exponent

+

7.654321

x 10

2

131.110321 x

10

2

Add the mantissas, keep the

common exponentSlide7

C++ Float Types

Addition in Scientific

Notation

131.110321

x 102 1.31110321 x 104 Convert the result to Scientific Notation1.31110321 x 104 Round off to 7 significant digits1.311103

x 10

4

Done!

A CPU must go through a very similar

process to add two Floats!Slide8

Integer vs. Float

Consideration

Integer

Float

Increasing Sizeincreases Rangeincreases number of significant digits*Arithmeticfast and easyslow and complicatedPrecisionalways preciseimprecise due to max number of significant digitsRangeLimited (small)Unlimited (practically)* - in floats, increasing size may also increase range somewhatSlide9

Integer vs. Float

General Design Principles

- Use Integers unless Floats are needed

- Use larger Integer types when increased Range (larger numbers) is needed- Use larger Float types when increased Precision (number of significant digits) is needed.Slide10

Float

Current Practical Considerations

In current 64-bit versions of C++, literals with

decimal points are type

double float taxRate = 0.06; Gets a "possible loss of data" warning message(because 0.06000000 is rounded to 0.06000)Slide11

Float

Current Practical Considerations

To avoid this warning message, always use

double

instead of float for this course // float taxRate = 0.06; double taxRate = 0.06;Slide12

Character Types

char

exactly one character

string

a sequence of 0 or more charactersString variables are not native to C++; they are an Extension. To use this type, you must:#include <string>Slide13

Variable

A variable is a

container

for data that has a

NAME: an identifier (created by the programmer)(DATA) TYPE: describes data values and operationsCONTENTS: can change during execution of the programVariables must be Declared before used!Slide14

Declaration Statement

Syntax:

type identifier

;

type identifier = literal;type id1, id2, id3;type id1 = lit1, id2 =

lit2

,

id3

=

lit3

;Slide15

Declaration Statement

Semantics:

type identifier

=

literal;- Creates a new variable named identifier of type type - Sets the initial value of the variable to literalSlide16

Declaration Statement

Semantics:

Example:

int

a = 3;1. Memory is allocated (“set aside”), the size of an int. 2. The location is “named” a.3. The value 3 is placed in the location.Slide17

Declaration Statement

Style:

Create

meaningful

variable names.double length, width, area;Notdouble l, w, a;Slide18

Declaration Statement

Examples:

i

nt

length;long long width = 0;float radius = 3.25; // may get a warningdouble length = 2.5, width = 3.75, height = 0;char

letterGrade

= ’A’;

s

tring

our_cheer

= ”Go Big Blue!”;Slide19

Assignment Statement

Purpose:

change the value of a variable

Syntax:

variable = expression;- The variable must already be declared- The expression must evaluate to a value that is compatible with the type of the variable.Slide20

Assignment Statement

Syntax

:

“compatible” usually means “same type”, as for char and string.char letterGrade = ’A’;string our_cheer = ”Go Big Blue!”;Notchar letterGrade

=

”A”;

string

our_cheer

=

’Go

Big Blue

!’;

letterGrade

=

our_cheer

;Slide21

Assignment Statement

Syntax:

floats and integers are generally cross-“compatible” syntactically, but semantically there are caveats ("dangers").

double y = 3; // no problemshort x = 2.5;//OK, BUT see belowSlide22

Assignment Statement

Semantics:

variable

=

expression;- Evaluate the expression on the right.- When the types differ but are compatible, convert the expression’s type to the variable’s type.- Change the current value of the variable to the evaluated result.Slide23

Assignment Statement

Semantics: Warning on Float-to-Integer!

Float values are

truncated

(not rounded).int x = 2.9; // sets x to 2double y = 3.6;int z = y; // sets z to 3, not 4 or 3.6cout << x << " " << y;Prints: 2 3Slide24

Arithmetic

Operators

+

,

-, *, / work as expected except:- integer / integer results in an integer- Mixed operands (one int, one float) always results in a

float

.

double x = 2/3; //

int

/

int

->

int

c

out

<< x; // prints

0

x

= 2/3.0; //

int

/ float -> float

c

out

<< x; // prints

0.6666Slide25

Arithmetic

Modulus Operator for integers:

%

int

dividend, remainder;dividend = 7 / 5; // dividend = 1remainder = 7 % 5; // remainder = 2Slide26

Arithmetic

Order of Precedence:

First:

*

, / , % left to rightSecond: + , - left to rightint x;x = 5 + 2 * 4 – 6 / 3; // x = 11

// x = 5 + (2*4) – (6/3)

// x = 5 + 8 – 2

// x = 11Slide27

Arithmetic

Parentheses may be used

double x;

x

= (3 + 2) * 5; // x = 25X = 3 + 2 * 5; // x = 13Slide28

Arithmetic

Raising to an Exponent, Square Root, Round

double x =

sqrt

(16); // x = 4double y = pow(3,2); // y = 9double z = round(2.8); // z = 3.0double w = round(2.2); // w = 2double d = round(3.12345 * 100) / 100; // d = 3.12Slide29

Abbreviated Assignment

Increment and Decrement

i

nt

y=0, x=0;x++; // x = x + 1;++x; // x = x + 1;y = x++; // y = x; x = x + 1;y = ++x; // x = x + 1; y = x;x--; // x = x – 1;--x; // x = x – 1;Slide30

Abbreviated Assignment

+= -= *= /=

i

nt

X=1;X += 2; // x = x + 2;X -= 2; // x = x – 2;X *= 2; // x = x * 2;X /= 2; // x = x / 2;Slide31

Constants

Definition:

An identifier with a value that can never change.

Syntax and Semantics:Same as a variable declaration with an initial value, but with the reserved word const in front:const type identifier = literal;Slide32

Constants

Example:

const

double

tax_rate = 0.06;…// syntax error: value cannot be changed tax_rate = 0.07; Slide33

Constants

Why

use constants?

1.

Clarity 2. Consistency3. MaintainabilitySlide34

Constants

Clarity

how well a programmer can understand a program due to use of good style.

extra = subtotal * 0.06;What is 0.06? A tax rate? Interest rate? Fee rate?const float tax_rate = 0.06;…tax = subtotal * tax_rate;Now we know!Slide35

Constants

Consistency

when a value that

should be exactly the same throughout a program actually is.area = 3.14 * radius * radius;circumference = 2 * 3.1415 * radius;sphere_surface = 4 * 3.1415926 * radius * radius;The value of π is not consistent…this is better:c

onst

double pi = 3.1415926;

area

=

pi

* radius * radius;

circumference = 2 *

pi

* radius;

sphere_surface

= 4 *

pi

* radius * radius;Slide36

Constants

Maintenance

A

ny change to a program after it is “finished”.

Maintainability The ease/speed of doing Maintenance.Slide37

Constants

Maintainability Example

In

2013, by coincidence, the state and

local rates were the samestate_tax = subtotal * 0.06;local_tax = subtotal * 0.06;In 2016, the local tax rate changed to 0.07 (but the state rate remained at 0.06)Now we have to go through all programs, find all 0.06’s and decide if each needs to change to 0.07 or not. Slide38

Constants

Maintainability Example

This

would have been better:

const double state_tax_rate = 0.06;const double local_tax_rate = 0.06;…state_tax = subtotal * state_tax_rate

;

local_tax

= subtotal *

local_tax_rate

;

Now, all we have to do is change the constant’s value in

one place

, and re-compile all programs that use it.Slide39

Vocabulary

Term

Definition

Variable

A container for data: has a name, type and current contents.DeclarationA statement that defines a variable (name, type, initial value) and allocates the variable during execution.AllocateDuring execution, the reservation of memory location(s) represented by a variable.AssignmentA statement that changes the value (current contents) of a variable.MantissaThe significant digits of a number in Scientific Notation, with an assumed radix point after the first digit.ModulusOperator that calculates the remainder of an integer division. % in C++ConstantAn identifier with a declared type and value that cannot change.Slide40

Vocabulary

Term

Definition

Clarity

How well a programmer can understand a program due to use of good style.ConsistencyWhen a value that should be the same throughout a program actually is.MaintenanceAny changes to a program after it is “finished”.MaintainabilityThe ease/speed of changing a program after it is “finished”.