computer science 1620 other data types. quick review: checklist for performing user input: 1) be...

56
Computer Science 1620 Other Data Types

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Computer Science 1620

Other Data Types

Quick Review:checklist for performing user input:

1) Be sure variable is declared2) Prompt the user for input3) Use cin to obtain data

Three types so farWords – stringwhole numbers – integer floating point - double

Text Types to store a sequence of characters, use a

string type

string name = "Kevin";

to store a single character, you can use a char type

char first = 'K';

Text Typeswhat other differences are there between

string and charchars are denoted with single quotes

string name = "Kevin"; char x = 'K';

chars require 1 byte of store strings require x + 1 bytes of storage

chars are a simple data type compatible with C

Write a program that reads in your name, age, and sex, and subsequently displays this information

#include <iostream>#include <string> using namespace std;

int main() {

string name; int age; char sex;

cout << "Name: "; cin >> name;

cout << "Age: "; cin >> age;

cout << "Sex: "; cin >> sex;

cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl;

return 0;}

Write a program that reads in your name, age, and sex, and subsequently displays this information

#include <iostream>#include <string> using namespace std;

int main() {

string name; int age; char sex;

cout << "Name: "; cin >> name;

cout << "Age: "; cin >> age;

cout << "Sex: "; cin >> sex;

cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl;

return 0;}

Write a program that reads in your name, age, and sex, and subsequently displays this information

#include <iostream>#include <string> using namespace std;

int main() {

string name; int age; char sex;

cout << "Name: "; cin >> name;

cout << "Age: "; cin >> age;

cout << "Sex: "; cin >> sex;

cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl;

return 0;}

Write a program that reads in your name, age, and sex, and subsequently displays this information

#include <iostream>#include <string> using namespace std;

int main() {

string name; int age; char sex;

cout << "Name: "; cin >> name;

cout << "Age: "; cin >> age;

cout << "Sex: "; cin >> sex;

cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl;

return 0;}

Write a program that reads in your name, age, and sex, and subsequently displays this information

#include <iostream>#include <string> using namespace std;

int main() {

string name; int age; char sex;

cout << "Name: "; cin >> name;

cout << "Age: "; cin >> age;

cout << "Sex: "; cin >> sex;

cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl;

return 0;}

Write a program that reads in your name, age, and sex, and subsequently displays this information

#include <iostream>#include <string> using namespace std;

int main() {

string name; int age; char sex;

cout << "Name: "; cin >> name;

cout << "Age: "; cin >> age;

cout << "Sex: "; cin >> sex;

cout << "Information on file:" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl;

return 0;}

Integer types there are actually several flavours of "integer"

in C++

by default, all integer literals are ints

* compiler dependent

Type Range Size

char -128 .. 127 1 byte

short -32768 .. 32767* 2 bytes

int -2147483648 … 2147483647* 4 bytes

long -2147483648 … 2147483647* 4 bytes

Integer typesExample:

#include <iostream> using namespace std;

int main() {

int i = 2500; cout << i << endl;

short s = 2500; cout << s << endl;

return 0; }

Same as above, butuses half the memory.

Integer typesExample 2:

#include <iostream> using namespace std;

int main() {

int i = 40000; cout << i << endl;

short s = 40000; cout << s << endl;

return 0; }

Error: Literal is too bigto be stored in a short.(runtime error: the compiler does not complain)

Is using a short common?not really

Why?1) Memory is cheap

embedded programmers will typically make use of small data types

2) Arithmetic operations between any two integer types results in an int

Integer typesExample 3:

#include <iostream> using namespace std;

int main() {

short a = 1600; short b = 2200;

cout << a + b << endl;

return 0; } Value of addition expression

is of type int.

Why is long the same size as int? compiler dependent the rules state that:

a short is guaranteed to be both: at least 16 bits (2 bytes) at least as big as a char

an int is guaranteed to be both: at least 16 bits (2 bytes) at least as big as a short

a long is guaranteed to be both: at least 32 bits (4 bytes) at least as big as an int

Unsigned Integerseach of the integer types can be prepended

with the word unsignedAn unsigned variable can only store positive

numbersThis increases the size of the number that

can be stored

Unsigned integer types

* compiler dependent

Type Range Size

unsigned char 0 .. 255* 1 byte

unsigned short 0 .. 65535* 2 bytes

unsigned int 0 .. 4294967295* 4 bytes

unsigned long 0 .. 4294967295* 4 bytes

in C++, unsigned only applies to integers!!

Unsigned Numberswhy can we store a larger number with an

unsigned type?suppose we have a 32 bit signed numberone of the bits (usually the leftmost) indicates

the sign of the number 0 means positive 1 means negative

the remainder of the bits (31) are used to store the digits

Unsigned Numberswhy can we store a larger number with an

unsigned type?suppose we have a 32 bit unsigned numberall 32 bits can be used to store the digitsstoring one extra digit doubles the size of the

number

Example: which of these statements are ok, and which are in error

(assuming stated compiler limits)?

#include <iostream> using namespace std;

int main() {

int i = 3000000000; // 3 billion

unsigned int j = 3000000000; // 3 billion int k = -2000000000; // negative 2 billion

unsigned int m = -2000000000; // negative 2 billion

return 0; }

Floating Point types there are actually several flavours of fps in C++

by default, all floating point literals are doubles

* compiler dependent

Type Range Size

float 1.17549e-38 ... 3.40282e+38 4 bytes

double 2.22507e-308 ... 1.79769e+308* 8 bytes

long double 3.3621e-4932 ... 1.18973e+4932* 10 bytes

Floating Point Literalsby default, a floating point literal is of type

double

#include <iostream> using namespace std;

int main() {

cout << 12.84 << endl; }

stored as a double

Floating Point Literalsyou can store the number as a float by

suffixing the number with f or F

#include <iostream> using namespace std;

int main() {

cout << 12.84F << endl; }

stored as a float

Floating Point Literalsyou can store the number as a long double

by suffixing the number with l or L

#include <iostream> using namespace std;

int main() {

cout << 12.84L << endl; }

stored as a long double

Approximate guidelines for choosing fp type

Number of significant digits required

Data Type

7 or less float

15 or less double

19 or less long double

Examplesbank accounts with less than $150000

doublebank accounts with less than $5000

floatpi to 17 decimal places

long double

Types and Arithmeticour previous rules need an update:

1. if the operator has the same types, then the value of the expression has the same type

2. if one of the operands is a floating point number and the other an integer, then the integer is promoted to a floating point number. The value of the expression is a floating point number (double)*

3. the precedence rules from before still apply

Conversion Rules: * 1. If both operands are an integer type

If both operands are character, short, or int data types, the result of the expression is an int value.

When one of the operands is a long integer, the result is a long integer.

2. If any one operand is a floating-point value then: when one or both operands are floats, the result is a float when one or both operands are doubles, the result is a

double when one or both operands are long doubles, the result is

a long double.

* from C++ for Engineers and Scientists (Bronson) pp. 79

Examples:

Operand 1 Operand 2 Result

int short int

short short int

long int int long int

long int double double

float double double

float float float

long int float float

Promotion and Demotion refers to the conversion of one type to another promotion

conversion of integer to floating point number ie. an int to a double

conversion of smaller integer type to a larger integer type ie. a short to a long

conversion of a smaller floating point type to a larger floating point type

ie. a float to a long double

Promotion and Demotion refers to the conversion of one type to another demotion

conversion of floating point to integer ie. a double to an int

conversion of larger integer type to a smaller integer type ie. a long to a short

conversion of a larger floating point type to a smaller floating point type

ie. a long double to a float

Promotion and Demotionpromotion typically occurs automatically

no compiler warning generated

#include <iostream> using namespace std;

int main() {

double x = 10;

cout << x << endl; }

Promotion and Demotiondemotion typically occurs automatically

a compiler warning is generated in only some cases

typically, when a fp # is demoted to an integer

#include <iostream> using namespace std;

int main() {

int x = 10.0;

cout << x << endl; }

Why does C++ warn us when demoting a floating-point # to an integer?

#include <iostream> using namespace std;

int main() {

int x = 10.6;

cout << x << endl; }

When a floating point number is converted to an integer, the digits after the decimal are lost.

What happens if I want to demote a floating-point # to an intdo I have to live with a compiler warning?

nouse a cast operatorSyntax:

static_cast< >( );data type expression

Data type refers to the typeof value to convert to.

The expression is anyexpression with a 'convertible'value.

#include <iostream> using namespace std;

int main() {

int x = 10.6;

cout << x << endl; }

Example:

#include <iostream> using namespace std;

int main() {

int x = static_cast< data type > ( expression );

cout << x << endl; }

Example:We are casting to an int.

#include <iostream> using namespace std;

int main() {

int x = static_cast< int > ( expression );

cout << x << endl; }

Example:We are casting to an int.

#include <iostream> using namespace std;

int main() {

int x = static_cast< int > ( expression );

cout << x << endl; }

Example:The expression we wish toconvert is 10.6

#include <iostream> using namespace std;

int main() {

int x = static_cast< int > ( 10.6 );

cout << x << endl; }

Example:The expression we wish toconvert is 10.6

#include <iostream> using namespace std;

int main() {

int x = static_cast< int > ( 10.6 );

cout << x << endl; }

Example:

Why no compiler warning that time?by using the cast operator, you are

acknowledging to the compiler that you wish to make the conversion

You "assume the risks" involved with casting

loss of precision, etc.

When else is casting useful?2) Conversion of an expression

while it is easy to convert integer literals to floating point literals (and vice versa), it is not so easy to do the same to variables, or arithmetic expressions

Example:compute (15 % 4) / (10 % 6) as a decimal

resultThe answer is 3 / 4 = 0.75

#include <iostream> using namespace std;

int main() {

cout << (15 % 4) / (10 % 6) << endl;

return 0;}

Example:compute (15 % 4) / (10 % 6) as a decimal

resultThe answer is 3 / 4 = 0.75

#include <iostream> using namespace std;

int main() {

double result = (15 % 4) / (10 % 6);

cout << result << endl;

return 0;}

Example:compute (15 % 4) / (10 % 6) as a decimal

resultThe answer is 3 / 4 = 0.75

#include <iostream> using namespace std;

int main() {

cout << static_cast<double>(15 % 4) / (10 % 6) << endl;

return 0;}

Castingcould we not have simply added a term to

the formula?

#include <iostream> using namespace std;

int main() {

cout << (15 % 4) * 1.0 / (10 % 6) << endl;

return 0;}

Casting the problem with this approach is that it is not immediate

whether the 1.0 is part of the original equation, or is being used strictly for conversion purposes

#include <iostream> using namespace std;

int main() {

cout << (15 % 4) * 1.0 / (10 % 6) << endl;

return 0;}

Casting this makes it very obvious what is going on

#include <iostream> using namespace std;

int main() {

cout << static_cast<double>(15 % 4) / (10 % 6) << endl;

return 0;}

C-Style Casting the casting that you've seen so far is C++

castingcasting was allowed in C as well

predecessor to C++C-style cast syntax:

( )data type expression

or

( )data type expression

Casting C-style casting

#include <iostream> using namespace std;

int main() {

cout << double(15 % 4) / (10 % 6) << endl;

return 0;}

Casting C-style casting

#include <iostream> using namespace std;

int main() {

cout << (double)(15 % 4) / (10 % 6) << endl;

return 0;}

C vs. C-style CastingC casting is more compacthowever, not part of new standards

support for this type of cast could disappear to make sure your code complies with future

versions of C++ compilers, use C++ casting