DOC PREVIEW
CSUN COMP 106 - Data Types

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Data TypesFebruary 7, 20061Data Types Data Types ––Declarations Declarations and Initializations and Initializations Larry CarettoComputer Science 106Computing in Engineering and ScienceFebruary 7, 20052Outline• Review last week• Meaning of data types• Integer data types have no decimal point and integer division truncates• Floating point data types: approximate representation of decimal numbers• Character, string and boolean (logical) data types3Review of Last Week• Basic elements of C++ programs– Template #include, using namespace std; int main(), return EXIT_SUCCESS;– C++ is case sensitive– White space does not matter except in string constants– End statements with a semicolon (;)– Braces { and } frame logical code blocks– Will use in lab this week4Review of Last Week II• Screen output using cout and <<• Keyboard input using cin and >>• Variables refer to memory locations– Use letters, numbers and _– Start with letter or _– 31 characters maximum– Use meaningful names– Variables are case sensitive (x2 is not X2)5Review Variables and Memory• Program variables refer to computer memory (RAM) locations• When we use a variable (say x) we get the value in the memory location the compiler associated with this variable– x = 3; // assigns the value 3 to x– cout << x; // writes value of x to the screen– y = x; // assigns value of x to y– x = x + 3; // replaces x by x + 36Computer Memory• Cells show memory address, associated variable name and value stored (if any)• What is effect of y2 = PI * rad * rad?– What happens to cell 106? to cells 102 and 104?– Cell 106 gets the new value of π(3.5)2– Cells 102 and 104 are not changed107 _data 13106 y2105 nam“CSUN”104 rad3.5103 i0102 PI 3.1415926101 x1 15100 Var12.2 AddressVariable nameValueData TypesFebruary 7, 200627Computer Memory• What is effect of _data = x1?– The value of 15 in cell 101 is stored in cell 107– What happens to the value of 13 in cell 107?– It is lost forever– What happens to the value of 15 in cell 101?– Nothing 107 _data 13106 y2105 nam“CSUN”104 rad3.5103 i0102 PI 3.1415926101 x1 15100 Var12.2 8Computer Memory• What is effect of var = var + 3.5?– What happens to cell 100?– The value of 12.2 is replaced by 12.2 + 3.5 = 15.7– Are any other cells affected?–No107 _data 13106 y2105 nam“CSUN”104 rad3.5103 i0102 PI 3.1415926101 x1 15100 Var12.2 9Computer Memory• What is effect of var = var + rad on cell 104? on cell 100?– The value of 3.5 in cell 104 does not change– The value of 12.2 in cell 100 is replaced by the result 12.2 + 3.5 = 15.7107 _data 13106 y2105 nam“CSUN”104 rad3.5103 i0102 PI 3.1415926101 x1 15100 Var12.2 10Computer Memory• Can you do y2 = nam?– This is a trick question. You have no basis for answering it yet. It depends on the type for y2.– If y2 can hold strings the operation is okay– If y2 cannot hold strings this is a syntax error107 _data 13106 y2105 nam“CSUN”104 rad3.5103 i0102 PI 3.1415926101 x1 15100 Var12.2 11Data Types• Computer memory stores binary information – a string of ones and zeros• How are these ones and zeros interpreted? Depends on data type• Analogy – How do you interpret the following string of characters?chair• Depends on language12Integer Data Types• There are several of these including int, the only one we will use in this course• Integer data types have no decimal part• When we divide two integers the decimal part is truncated (lost)– Not rounded, but truncated– What is 12/7?– What is 4/5?10Data TypesFebruary 7, 2006313Integer Data Types II• Range of values for different types– Based on number of bits in computer member allocated for type– short -32,768 to 32,767– int -2,147,483,648 to 2,147,483,647– long -2,147,483,648 to 2,147,483,647– unsigned short 0 to 65,535– unsigned int 0 to 4,294,967,295– unsigned long 0 to 4,294,967,29514Integer Data Types III• Range depends on compiler vendor– Ranges shown on previous page are common and are used in Visual C++– Only requirement for standard is that digits for long ≥ digits for int ≥ digits for short– Range comes from binary storage– 16 bits stores a number from 0 to 216–1– This is actual range for unsigned short– Short range is –215to 215–115Integer Data Types IV• What is result of following code?short result, maxShort = 32767;result = maxShort + 1;cout << “Result = “ << result;• Result is -32768, the smallest short integer• What is result of code where you subtract 1 from minShort = -32768?• Result, 34767, maximum short integer16Floating Point Data Types• Numbers with decimal points• Stored as characteristic plus mantissa• Numbers not stored exactly• Smallest type, float, occupies four bytes• Range for float is -3.402823466x1038to -1.175494351x10-38, 0, and 1.175494351x10-38to 3.402823466x103817Approximate Representation• How do you represent 1/3 as decimal?– .3, .33, .333, .3333333333333 or ?– Representation should be accurate enough for calculations but will never be exact– Similar problem on computers– Fractional numbers like 1/10 = 0.1 are represented as binary fractions• 1/10 is .000110011001100110011001100…2• 10 * 0.1 will not be exactly one18Floating Point Data Types II• Type float has about seven significant figures of accuracy• When does 1 + ε = 1?– When ε is so small that adding it to 1 does not change the significant figures available– For float, ε = 1.19x10-7• Other floating point types are doubleand long double– Both same in Visual C++Data TypesFebruary 7, 2006419Floating Point Data Types III• Type double has about 15–16 significant figures of accuracy– Range is -1.7976931348623158x10308to -2.2250738585072014x10-308, 0, and 2.2250738585072014x10-308to 1.7976931348623158x10308– For double, ε = 2.2x10-16• Generally use type double for modern engineering/science/mathematics codes20Other Data Types• char data type represents single characters (constant example: ‘a’)• string data type represents a string of characters (constant example: “string”)– Requires #include<string> declaration• bool data type used in logical operations– Only two possible values for type


View Full Document

CSUN COMP 106 - Data Types

Download Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?