CSUN COMP 106 - Data Types (5 pages)

Previewing pages 1, 2 of 5 page document View the full content.
View Full Document

Data Types



Previewing pages 1, 2 of actual document.

View the full content.
View Full Document
View Full Document

Data Types

112 views


Pages:
5
School:
California State University, Northridge
Course:
Comp 106 -
Unformatted text preview:

February 7 2006 Data Types Outline Data Types Declarations and Initializations 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 types Larry Caretto Computer Science 106 Computing in Engineering and Science February 7 2005 2 Review of Last Week Review of Last Week II Basic elements of C programs Screen output using cout and Keyboard input using cin and Variables refer to memory locations 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 week Use letters numbers and Start with letter or 31 characters maximum Use meaningful names Variables are case sensitive x2 is not X2 3 4 Review 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 Computer Memory 100 12 2 104 3 5 Var 101 15 Address x1 102 PI 103 3 1415926 0 rad 105 nam 106 CSUN Value i Variable name y2 107 data 13 Cells show memory address associated variable name and value stored if any What is effect of y2 PI rad rad 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 3 5 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 changed 6 1 February 7 2006 Data Types Computer Memory 100 12 2 104 3 5 Var 101 15 x1 102 PI 103 3 1415926 0 rad 105 nam 106 CSUN Computer Memory i 12 2 y2 107 data 13 What is effect of data x1 100 104 3 5 PI 103 3 1415926 0 i rad 105 nam 106 CSUN y2 107 data 13 103 What happens to cell 100 The value of 12 2 is replaced by 12 2 3 5 15 7 Are any other cells affected No 7 8 Computer Memory Var 101 x1 102 PI 15 3 1415926 12 2 104 rad 105 nam 106 y2 3 5 CSUN x1 102 What is effect of var var 3 5 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 100 Var 101 15 Computer Memory i 0 107 data 13 What is effect of var var rad on cell 104 on cell 100 Var 101 x1 102 PI 15 3 1415926 12 2 104 rad 105 nam 106 y2 3 5 CSUN 100 103 i 0 107 data 13 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 error 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 7 9 10 Data Types Integer Data 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 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 chair Depends on language 11 Not rounded but truncated What is 12 7 1 What is 4 5 0 12 2 February 7 2006 Data Types Integer Data Types II Integer Data Types III Range of values for different types Range depends on compiler vendor 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 295 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 215 to 215 1 13 14 Integer Data Types IV Floating Point Data Types 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 integer Numbers with decimal points Stored as characteristic plus mantissa Numbers not stored exactly Smallest type float occupies four bytes Range for float is 3 402823466x1038 to 1 175494351x10 38 0 and 1 175494351x10 38 to 3 402823466x1038 15 16 Approximate Representation Floating Point Data Types II How do you represent 1 3 as decimal Type float has about seven significant figures of accuracy When does 1 1 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 one 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 double and long double Both same in Visual C 17 18 3 February 7 2006 Data Types Floating Point Data Types III Other Data Types Type double has about 15 16 significant figures of accuracy Range is 1 7976931348623158x10308 to 2 2250738585072014x10 308 0 and 2 2250738585072014x10 308 to 1 7976931348623158x10308 For double 2 2x10 16 Generally use type double for modern engineering science mathematics codes 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 bool variables true and false 19 Constant Types 20 Declaring Variables Program constants have types All variables must be declared as belonging to a specific data type the first time they appear in the code integer 37 is type int decimal 123 4 or 1 234e2 is type double Character char c string This is a string constant Failure to do so is a syntax error Can initialize a variable with declaration recommended practice or subsequent to declaration Can declare several variables in a single statement The only place where spaces matter …


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

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 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?