Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.

Similar presentations


Presentation on theme: "Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input."— Presentation transcript:

1 Computer Science 1620 Other Data Types

2 Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input 3) Use cin to obtain data

3 Three types so far Words – string whole numbers – integer floating point - double

4 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';

5 Text Types what other differences are there between string and char chars 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

6 Write a program that reads in your name, age, and sex, and subsequently displays this information #include 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; }

7 Write a program that reads in your name, age, and sex, and subsequently displays this information #include 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; }

8 Write a program that reads in your name, age, and sex, and subsequently displays this information #include 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; }

9 Write a program that reads in your name, age, and sex, and subsequently displays this information #include 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; }

10 Write a program that reads in your name, age, and sex, and subsequently displays this information #include 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; }

11 Write a program that reads in your name, age, and sex, and subsequently displays this information #include 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; }

12

13 Integer types there are actually several flavours of "integer" in C++ by default, all integer literals are ints * compiler dependent TypeRangeSize char-128.. 1271 byte short-32768.. 32767*2 bytes int-2147483648 … 2147483647*4 bytes long-2147483648 … 2147483647*4 bytes

14 Integer types Example: #include using namespace std; int main() { int i = 2500; cout << i << endl; short s = 2500; cout << s << endl; return 0; } Same as above, but uses half the memory.

15 Integer types Example 2: #include using namespace std; int main() { int i = 40000; cout << i << endl; short s = 40000; cout << s << endl; return 0; } Error: Literal is too big to be stored in a short. (runtime error: the compiler does not complain)

16 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

17 Integer types Example 3: #include 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.

18 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

19 Unsigned Integers each of the integer types can be prepended with the word unsigned An unsigned variable can only store positive numbers This increases the size of the number that can be stored

20 Unsigned integer types * compiler dependent TypeRangeSize unsigned char0.. 255*1 byte unsigned short0.. 65535*2 bytes unsigned int0.. 4294967295*4 bytes unsigned long0.. 4294967295*4 bytes in C++, unsigned only applies to integers!!

21 Unsigned Numbers why can we store a larger number with an unsigned type? suppose we have a 32 bit signed number one 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

22 Unsigned Numbers why can we store a larger number with an unsigned type? suppose we have a 32 bit unsigned number all 32 bits can be used to store the digits storing one extra digit doubles the size of the number

23 Example: which of these statements are ok, and which are in error (assuming stated compiler limits)? #include 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; }

24 Floating Point types there are actually several flavours of fps in C++ by default, all floating point literals are doubles * compiler dependent TypeRangeSize float1.17549e-38... 3.40282e+384 bytes double2.22507e-308... 1.79769e+308*8 bytes long double3.3621e-4932... 1.18973e+4932*10 bytes

25 Floating Point Literals by default, a floating point literal is of type double #include using namespace std; int main() { cout << 12.84 << endl; } stored as a double

26 Floating Point Literals you can store the number as a float by suffixing the number with f or F #include using namespace std; int main() { cout << 12.84F << endl; } stored as a float

27 Floating Point Literals you can store the number as a long double by suffixing the number with l or L #include using namespace std; int main() { cout << 12.84L << endl; } stored as a long double

28 Approximate guidelines for choosing fp type Number of significant digits required Data Type 7 or lessfloat 15 or lessdouble 19 or lesslong double

29 Examples bank accounts with less than $150000 double bank accounts with less than $5000 float pi to 17 decimal places long double

30 Types and Arithmetic our 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

31 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

32 Examples: Operand 1Operand 2Result intshortint short int long intintlong int double floatdouble float long intfloat

33 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

34 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

35 Promotion and Demotion promotion typically occurs automatically no compiler warning generated #include using namespace std; int main() { double x = 10; cout << x << endl; }

36 Promotion and Demotion demotion typically occurs automatically a compiler warning is generated in only some cases typically, when a fp # is demoted to an integer #include using namespace std; int main() { int x = 10.0; cout << x << endl; }

37 Why does C++ warn us when demoting a floating-point # to an integer? #include 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.

38 What happens if I want to demote a floating-point # to an int do I have to live with a compiler warning? no use a cast operator Syntax: static_cast ( ); data type expression Data type refers to the type of value to convert to. The expression is any expression with a 'convertible' value.

39 #include using namespace std; int main() { int x = 10.6; cout << x << endl; } Example:

40 #include using namespace std; int main() { int x = static_cast ( expression ); cout << x << endl; } Example: We are casting to an int.

41 #include using namespace std; int main() { int x = static_cast ( expression ); cout << x << endl; } Example: We are casting to an int.

42 #include using namespace std; int main() { int x = static_cast ( expression ); cout << x << endl; } Example: The expression we wish to convert is 10.6

43 #include using namespace std; int main() { int x = static_cast ( 10.6 ); cout << x << endl; } Example: The expression we wish to convert is 10.6

44 #include using namespace std; int main() { int x = static_cast ( 10.6 ); cout << x << endl; } Example:

45 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.

46 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

47 Example: compute (15 % 4) / (10 % 6) as a decimal result The answer is 3 / 4 = 0.75 #include using namespace std; int main() { cout << (15 % 4) / (10 % 6) << endl; return 0; }

48 Example: compute (15 % 4) / (10 % 6) as a decimal result The answer is 3 / 4 = 0.75 #include using namespace std; int main() { double result = (15 % 4) / (10 % 6); cout << result << endl; return 0; }

49 Example: compute (15 % 4) / (10 % 6) as a decimal result The answer is 3 / 4 = 0.75 #include using namespace std; int main() { cout (15 % 4) / (10 % 6) << endl; return 0; }

50 Casting could we not have simply added a term to the formula? #include using namespace std; int main() { cout << (15 % 4) * 1.0 / (10 % 6) << endl; return 0; }

51 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 using namespace std; int main() { cout << (15 % 4) * 1.0 / (10 % 6) << endl; return 0; }

52 Casting this makes it very obvious what is going on #include using namespace std; int main() { cout (15 % 4) / (10 % 6) << endl; return 0; }

53 C-Style Casting the casting that you've seen so far is C++ casting casting was allowed in C as well predecessor to C++ C-style cast syntax: ( ) data typeexpression or ( ) data typeexpression

54 Casting C-style casting #include using namespace std; int main() { cout << double(15 % 4) / (10 % 6) << endl; return 0; }

55 Casting C-style casting #include using namespace std; int main() { cout << (double)(15 % 4) / (10 % 6) << endl; return 0; }

56 C vs. C-style Casting C casting is more compact however, 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


Download ppt "Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input."

Similar presentations


Ads by Google