Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

Slides:



Advertisements
Similar presentations
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
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.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
How Create a C++ Program. #include using namespace std; void main() { cout
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
1 Simple Data Types: Built-in and Use-Defined. 2 Chapter 10 Topics  Built-In Simple Types  Integral and Floating Point Data Types  Using Combined Assignment.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Types, Operators and Expressions CSE 2031 Fall /5/2015 3:59 PM.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
1 Chapter 10 Simple Data Types: Built- In and User- Defined Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Simple Data Types: Built-In and User-Defined
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Simple Data Types Built-In and User Defined Chapter 10.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Objects Types, Variables, and Constants Chapter 3.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter Topics The Basics of a C++ Program Data Types
Chap. 2. Types, Operators, and Expressions
Basic Elements of C++.
C++ Simple Data Types Simple types Integral Floating
Basic Elements of C++ Chapter 2.
Operators and Expressions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2: Introduction to C++.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University

178110: Computer Programming (II/2546) 2 Major Concepts Data types Numeric types Boolean type Enumeration type Character type Arithmetic operations Type conversions Run-time errors: Numeric overflow Round-off error

178110: Computer Programming (II/2546) 3 Numeric Data Types There are two kinds of numbers Whole numbers, Integers ( จำนวนเต็ม ) Including negative whole numbers, 0, and positive whole numbers Examples: 0, 1, 2, -1, -2 Decimal numbers Fraction where the denominator is a power of ten and is therefore expressed using a decimal point. Examples: 0.37 Rational numbers: จำนวนจริงเป็นตัวเลขที่ สามารถเขียนอยู่ในรูปทศนิยม, such as 2/10 Irrational numbers: cannot be expressed as a fraction, such as

178110: Computer Programming (II/2546) 4 Fundamental Types Standard C++ has 14 different fundamental types: 11 integral types and 3 floating-point types Integral Types Boolean Type bool Enumeration Type enum

178110: Computer Programming (II/2546) 5 Fundamental Types (Cont.) Integral Types Character Types char unsigned char wchar_t Integer Types short int long

178110: Computer Programming (II/2546) 6 Fundamental Types (Cont.) Integral Types Integer Types unsigned short unsigned int unsigned long Floating-point Types float double long double

178110: Computer Programming (II/2546) 7 The Boolean Type A boolean type is an integral type whose variables can have only two values: False and true These values are stored as the integers 0 and 1 The boolean type in Standard C++ is named bool

178110: Computer Programming (II/2546) 8 Boolean Variables #include using namespace std; int main() {// print the value of a boolean variable bool flag = false; cout << “flag = “ << flag << endl; flag = true; cout << “flag = “ << flag << endl; }

178110: Computer Programming (II/2546) 9 Enumeration Types An enumeration type is an integral type that is defined by the user with the syntax enum typename { enumerator-list }; enum is a C++ keyword typename is an identifier that names the type being defined enumerator-list is a list of acceptable values of the variable that has that typename

178110: Computer Programming (II/2546) 10 Enumeration Types (Cont.) Example: enum Semester {FALL, WINTER, SPRING, SUMMER}; We can then declare variables of this type Semester s1, s2; We can use those variables and those type values as we would with predefined types s1 = FALL; s2 = WINTER;

178110: Computer Programming (II/2546) 11 Enumeration Types The actual values defined in the enumerator-list are called enumerators In fact, they are ordinary integer constants Example: the enumerators FALL, WINTER, SPRING, and SUMMER that are defined for the semester type could have been defined like this: const int FALL = 0; const int WINTER = 1; const int SPRING = 2; const int SUMMER = 3;

178110: Computer Programming (II/2546) 12 Enumeration Types (Cont.) The values 0, 1, … are assigned automatically when the type is defined These default values can be overridden in the enumerator-list: enum Coin {PENNY = 1, NICKEL = 5, DIME = 10, QUARTER = 25};

178110: Computer Programming (II/2546) 13 Enumeration Types (Cont.) If integer values are assigned to only some of the enumerators, then the ones that follow are given consecutive values enum Month {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Since enumerators are simply integer constants, it is legal to have several different enumerators with the same value: enum Answer {NO = 0, FALSE = 0, YES = 1, TRUE = 1, OK = 1}

178110: Computer Programming (II/2546) 14 Enumeration Types (Cont.) Enumeration types are usually defined to make code more self-documenting, i.e., easier for humans to understand enum Sex {FEMALE, MALE}; enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; enum Radix {BIN=2, OCT=8, DEC=10, HEX=16};

178110: Computer Programming (II/2546) 15 Enumeration Types (Cont.) Enumerators must be valid identifiers This following definition would not be valid enum Grade {F, D, C-, C, C+, B-, B, A-, A}; Why is it invalid? ‘+’ and ‘-’ cannot be used in identifiers Same values cannot be used in the same scope (same place) enum Month {JAN, …, OCT, …} enum Radix {BIN, OCT, …}

178110: Computer Programming (II/2546) 16 Character Types A character type is an integral type whose variables represent characters like the letter ‘A’ or the digit ‘s’ Characters are delimited by the apostrophe (‘) Like all integral type values, character values are stored as integers In C++, the type name for a character is char

178110: Computer Programming (II/2546) 17 Character Types (Cont.) Using the ASCII encoding standard, the examples of the values that represent these characters are as follow: ‘ ’represented by 32 ‘+’ represented by 43 ‘A’represented by 65 ‘a’represented by 97

178110: Computer Programming (II/2546) 18 Escape Sequences Escape sequences are used to print out special characters \‘Single quote \“Double quote \\ Backslash \nNewline \tHorizontal tab \vVertical tab

178110: Computer Programming (II/2546) 19 Integer Types There are 6 integer types in Standard C++ short: 2 bytes int: 4 bytes long: 4 bytes unsigned short: 2 bytes unsigned int: 4 bytes unsigned long:4 bytes The least value of variables with unsigned types are 0

178110: Computer Programming (II/2546) 20 Integer Type Ranges This program prints the numeric ranges of the 6 integer types in C++ #include #include // defines the constants SHRT_MIN, etc. using namespace std; int main() { // prints some of the constants stored in the header: cout << "minimum short = " << SHRT_MIN << endl; cout << "maximum short = " << SHRT_MAX << endl; cout << "minimum unsigned short = 0" << endl; cout << "maximum unsigned short = " << USHRT_MAX << endl; cout << "minimum int = " << INT_MIN << endl; cout << "maximum int = " << INT_MAX << endl; cout << "minimum unsigned int = 0" << endl; cout << "maximum unsigned int = " << UINT_MAX << endl; cout << "minimum long= " << LONG_MIN << endl; cout << "maximum long= " << LONG_MAX << endl; cout << "minimum unsigned long = 0" << endl; cout << "maximum unsigned long = " << ULONG_MAX << endl; }

178110: Computer Programming (II/2546) 21 Integer Type Ranges (Cont.) The output of the program minimum short = maximum short = minimum unsigned short = 0 maximum unsigned short = minimum int = maximum int = minimum unsigned int = 0 maximum unsigned int = minimum long= maximum long= minimum unsigned long = 0 maximum unsigned long =

178110: Computer Programming (II/2546) 22 Arithmetic Operations C++ performs its numerical calculations by means of the five arithmetic operators: +, -, *, /, and % Operations are performed in this order 1. ( ) Perform the operation inside ( ) first 2. +,- Then, perform unary plus and unary minus, such as +32, *,/,% 4. +, - If there is more than one operation with the same order, perform from left to right

178110: Computer Programming (II/2546) 23 Arithmetic Operations (Cont.) Math: a = bc/de C++: a = (b*c)/(d*e) Is b*c/d*e == (b*c)/(d*e)? Is b*c*d*e == (b*c)*(d*e)? Math: m = C++: m = (y-b)/(x-a) Is y-b/x-a == (y-b)/(x-a)? Is y-b-x-a == (y-b)-(x-a)?

178110: Computer Programming (II/2546) 24 Arithmetic Operations (Cont.) -34*32 = ? Choice a: (-34)*32 Choice b: -(34*32) 7 – 2 % 3 = ? Choice a: (7-2)% 3 Choice b: 7 – (2 %3) / 4 = ? Choice a: 3 + (2/4) Choice b: (3 + 2)/ 4

178110: Computer Programming (II/2546) 25 Arithmetic Operations (Cont.) #include using namespace std; int main() { // tests operators +, -, *, /, and %: int m=54; int n=20; cout << "m = " << m << " and n = " << n << endl; cout << "m+n = " << m+n << endl; // = 74 cout << "m-n = " << m-n << endl; // = 34 cout << "m*n = " << m*n << endl; // 54*20 = 1080 cout << "m/n = " << m/n << endl; // 54/20 = 2 cout << "m%n = " << m%n << endl; // 54%20 = 14 return 0; }

178110: Computer Programming (II/2546) 26 Increment & Decrement Operators The values of integral objects can be incremented by one with the ++ operator and decremented by one with the – operator Each of “++” and “--” operator has two versions: A “pre” version A “post” version

178110: Computer Programming (II/2546) & -- Operators The “pre” version performs the operation on the object before the resulting value is used in its context Example: n = ++m  m= m + 1, n = m The “post” version performs the operation on the operation after the object’s current value ahs been used Example: n = m++  n = m, m = m + 1

178110: Computer Programming (II/2546) & -- Operators (Cont.) int main() { // shows the difference between m++ and ++m m = 44; n = ++m; cout << “m = “ << m << “, n = “ << n << endl; m = 44; n = m++; cout << “m = “ << m << “, n = “ << n << endl; }

178110: Computer Programming (II/2546) 29 Composite Assignment Operators The standard assignment operator in C++ is the equal sign ‘=‘ C++ also includes the composite assignment operators: ‘+=‘, ‘-=‘, ‘*=‘, ‘/=‘, and ‘%=‘ Examples: A += B;  A = A + B; A *= B;  A = A * B;

178110: Computer Programming (II/2546) 30 Composite Assignment Operators #include using namespace std; int main() { // tests arithmetic assignment operators: int n=22; cout << "n = " << n << endl; n += 9; // adds 9 to n cout << "After n += 9, n = " << n << endl; n %= 7; // mod n with 7 cout << "After n %= 7, n = " << n << endl; }

178110: Computer Programming (II/2546) 31 Floating-Point Types C++ has three types for storing values with floating-point float: use 4 bytes double: use 8 bytes long double: use 8, 10, 12, or 16 bytes On most systems, double uses twice as many bytes as float

178110: Computer Programming (II/2546) 32 Floating-Point Arithmetic int main() { double x = 54.0; double y = 20.0; cout << “x*y = “ << x*y << endl; // 1080 cout << “x/y = “ << x/y << endl; // 2.7 } If x and y have type ‘int’, what is the value of x/y? 2

178110: Computer Programming (II/2546) 33 Using the sizeof Operator The programs use the sizeof operator which returns the size in bytes of the type specified Examples: cout << “size of integer is “ << sizeof(int) << endl; cout << “size of float is “ << sizeof(float) << endl; Output: size of integer is 4 size of float is 4

178110: Computer Programming (II/2546) 34 Type Conversions int n = 22; float x = ; n += x; x += n; cout << “n is “ << n << endl; cout << “x is “ << x << endl; What are the values of n and x? n is 25 x is

178110: Computer Programming (II/2546) 35 Type Conversions (Cont.) If T is one type and v is a v value of another type, then the expression T(v) converts v to type T This process is called type casting Example: double v = ; int n = (int) v; cout << “n is “ << n endl; What is the value of n? n is 1234

178110: Computer Programming (II/2546) 36 Numeric Overflow Computers are finite, so the range of any type must also be finite But in mathematics, there are infinitely many integers Consequently, computers are manifestly prone to error when their numeric values become too large This kind of error is called numeric overflow

178110: Computer Programming (II/2546) 37 Numeric Overflow (Cont.) int main() { // print n until it overflows int n = 10000; cout << “sizeof(int) is “ << sizeof(int) << endl; cout << “n is “ << n << endl; n *= 10000; cout << “n is “ << n << endl; n *= 10000; cout << “n is “ << n << endl; return 0; } What is the maximum n can be?

178110: Computer Programming (II/2546) 38 Integer Overflow (Cont.) What is the size of an integer in bits? 32 bits 2 32 = 4,294,967,296 What is the maximum value of a variable with type ‘int’? 2,147,483,647 ((2 32 )/2) - 1 What is the minimum value of a variable with type ‘int’? -(2 32 )/2

178110: Computer Programming (II/2546) 39 Floating-Point Overflow #include using namespace std; int main() { // prints x until it overflows: float x=1000.0; cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; }

178110: Computer Programming (II/2546) 40 Floating-Point Overflow (Cont.) Output x = x = 1e+08 x = 1e+16 x = 1e+32 x = inf Integer overflow “wrap around” to negative integers Floating-point overflow “sinks” into the abstract notion of infinity

178110: Computer Programming (II/2546) 41 Round-off Error Round-off error is another kind of error that often occurs when computers do arithmetic on rational numbers Example: The number 1/3 might be stored as , which is not exactly equal to 1/3 The difference is called round-off error In some cases, these errors can cause serious problems

178110: Computer Programming (II/2546) 42 Round-Off Error (Cont.) // Example 2.14 on page 28 // Round-off Error #include using namespace std; int main() { // illustrates round-off error:: double x = 1000/3.0; cout << "x = " << x << endl; // x = 1000/3 double y = x ; cout << "y = " << y << endl; // y = 1/3 double z = 3*y - 1.0; cout << "z = " << z << endl; // z = 3(1/3) - 1 if (z == 0) cout << "z == 0.\n"; else cout << "z does not equal 0.\n"; // z != 0 }

178110: Computer Programming (II/2546) 43 Round-Off Error (Cont.) Program output x = 1000/3 = // x = 1000/3 y = x – 333 = // y = 1/3 z = 3*y – 1.0 = e-14// z = 3(1/3) - 1 z does not equal 0. // z != 0 It is better to avoid tests for equality with floating-point types Overflow and round-off errors are run-time errrors

178110: Computer Programming (II/2546) 44 E-Format for Floating-Point Values When input or output, floating-point values may be specified in either of two formats: Fixed-point format Example: , 0.1 Scientific format Example: x = e-14

178110: Computer Programming (II/2546) 45 E-Format (Cont.) Letter e stands for “exponent on 10” Thus, e-14 means The scientific format is thus usually used to express very small or very large numbers You can use either e or E in the scientific format

178110: Computer Programming (II/2546) 46 Scope The scope of an identifier is that part of the program where it can be used Variables cannot be used before they are declared The scope of a variable extends from the point where it is declared to the end of the internal block within which it is declared { int a = 2; // a can be used from this point } a cannot be used here

178110: Computer Programming (II/2546) 47 Scope (Cont.) #include using namespace std; int main() { // illustrates the scope of variables: x = 11; // Is this line OK? int x; { x = 22; // Is this line OK? y = 33; // Is this line OK? int y; y = 11; // Is this line OK? } x = 66; // Is this line OK? y = 77; // Is this line OK? }

178110: Computer Programming (II/2546) 48 Scope (Cont.) int x = 11; int main() { int x = 22; { int x = 33; cout << “In block inside main(): x = “ << x << endl; } cout << “In main(): x = “ << x << endl; cout << “In main(): ::x = “ << ::x << endl; } The scope resolution operator :: is to access the global x that is otherwise hidden in main()