Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

Similar presentations


Presentation on theme: "1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003."— Presentation transcript:

1 1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2 2 Data Object A data object, also known as a variable or identifier, can be characterized by four components: L, N, V, and T. –L is the location and holds the value of a data object. The action that allocates a location for a data object is called allocation. An allocation performed before execution is called static allocation; and if performed at run time it is called dynamic allocation. –N is the name of an identifies and is used to refer to the data object. –V is the value of data object and is represented in coded form in the allocated location of the data object. –T is the data type and represents the possible form of values that a data object can hold.

3 3 Data Object (cnt’d) A local variable is a variable that is declared within a block for use only within that block. A global variable is a variable that is declared in the outermost block of the program and can be used anywhere in a program.

4 4 Data Type In any programming language data and operations are related through a mechanism known as a data type, which usually has a set of operations that can be applied to values.

5 5 Design and Implementation: For each data type four important elements must be considered: –Set of values –Set of operations –The internal representation –The external representation

6 6 Type Information From the declaration point of view type information can be categorized as –Implicit type information –Explicit type information.

7 7 Implicit type information Implicit type information includes types of constants and types of variables that may not be given in a declaration. In this case the type must be inferred by the translator, either from context information or from standard rules. For example, 10 is implicitly an integer, true is a Boolean, I is an integer data object in the FORTRAN programming language, 2.5 is a double in C++.

8 8 Explicit type information Explicit type information is contained in declarations. Variables can be declared to be a specific data type, such as intx; floaty; where x and y are two variables of integer and real types, respectively.

9 9 User’s point of view: Type information classification Built-in data types, which are included within the language definition and can be used through the declaration of the data objects User-defined data types, which are defined through a declaration of the type before its use in a data object declaration.

10 10 Data Type Categories in Programming Languages Primitive data types Composite data types Recursive data types

11 11 Primitive data types Primitive data types, sometimes called unstructured data types, have elements consisting of indivisible entities and therefore cannot be decomposed into simple. –Integer Type –Real Type –Boolean Type –Character Type

12 12 Integer Type Most languages are implementation dependent. –-32768----32767 (2 byte integer) –-2 31 ------2 31 -1 (4 byte integer) Java integer is definite. type Size (bits) byte byte8 short short16 int int32 long long64

13 13 Real Type Floating-point numbers used to store data containing a decimal point and a fractional part. (Language dependent) –2.345, -15.0, 1.56e2, 1.56e-2, … –Eg: s, exp, f1, f2 (s0.1f1f2*2 (exp) ) ANSI C: – float, double, long double Java –The "float" class takes 4 bytes of storage, and have 23 binary digits of precision. The "double" class takes 8 bytes of storage, and have 52 binary digits of precision. –3.0d is a double precision floating-point number constant. 3.0, or alternatively 3.0f is a single precision floating-point number constant.

14 14 Boolean Type One bit True (1) / false (0) NOT, AND, OR

15 15 Character Type Expresses a single character Internal expression: ASCII code (7/8 bits) –http://www.cplusplus.com/doc/papers/ascii.ht ml http://www.cplusplus.com/doc/papers/ascii.ht mlhttp://www.cplusplus.com/doc/papers/ascii.ht mlJava: – ISO Unicode (16 bits, 65536=2 16 )

16 16 Composite Data Types Composite data types, sometimes called structured data types, are compound types such as arrays, records, tuples, lists, sets, functions, strings, pointers, linked lists, stacks, queues, and serial and direct files.

17 17 Composite data types classification Composite data types are classified: –Heterogeneous, whose elements are of different types (e.g., records and structures) –Homogeneous, whose elements are of the same type (e.g., arrays).

18 18 Array A collection of two or more adjacent memory cells, called array elements. In C++: – int arr[ ]= {1,2,3,4,5}; – char str[] = new char [20]; – char *str = new char [20]; – char str[20]; In Java – int [] arr = new int [5]; – char [] charArray = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; –String [] TAs = {“Kelly Brown”, “Andy Shulte”, “Brent Haas”};

19 19 String The values are consist of sequences of characters. In C++: – char s[20]; – string s; //STL In Java: – String s = “good”; – String s; – s = “good”;

20 20 Enumeration List all the values that can be taken. In C++: –enum weekday = {Sun, Mon, Tue, Wed, Thu, Fri, Sat} Internal expression: –several bits or one bit one value to get high speed.

21 21 Pointer Contains the address of another variable. – int r, *p, q; – r =5; – p = &r; – q = *p; © 2003 Brooks/Cole Publishing / Thomson Learning™

22 22 Structure Constructed by using objects of different data types. In C: – struct course – { – int number; – char grade; – } – struct course prog; – prog.number = 234; – prog.grade =‘A’;

23 23 Function A function is actually a struct in C. A function is actually a struct in C. – struct function { – string function_name; – string returntype; – string parameters[]; – string declarations[]; – string statements[]; –} Function pointers – you can transfer a function name to a function pointer as a parameter of another function.

24 24 List An ordered sequence of objects, in the form of (x :: y), x is the 1 st element, y is the tail (a list) Simple lists: –[1,2,3,4] –[“ab”, “cd”, “ef”] –(cons 'pine '(fir oak maple)) –=(pine fir oak maple)

25 25 Linked List struct node { struct node { int number; int number; char grade; char grade; struct node * next; struct node * next; } © 2003 Brooks/Cole Publishing / Thomson Learning™

26 26 To be continued Composite types –Stacks –Queues Recursive types Type binding Type checking Type conversion

27 27 Stacks A stack is a linear data structure that can be accessed only from the top for storing and retrieving data. Design and Implementation: The stack is called a last- in first-out (LIFO) data structure. A stack is defined in terms of operations that change its status and operations that check this status. The operations are as follows. –Clear():Clear the stack –IsEmpty():Check to see if the stack in empty –IsFull():Check to see if the stack is full –Push(element):Put the item element on top of the stack –Pop():Remove the element from the top of the stack

28 28 Figure 2.3 Push and Pop operations performed on a stack © 2003 Brooks/Cole Publishing / Thomson Learning™

29 29 Stack for a recursive function int f(int i) int f(int i) { if (i =0) return 0; if (i =0) return 0; else i+f(i-1); else i+f(i-1);} f(5) f(5) 5 4545 345345 23452345 1234512345 012345012345 012345012345 1234512345 33453345 645645 10 515 Push; …. … Pop, pop, add, push; …. …

30 30 Queues A queue is a linear data structure that grows by adding elements to its top and shrinks by taking elements from its bottom. Design and Implementation: The queue is called a first-in first-out (FIFO) structure, and operations are similar to stack operations. The operations are as follows. –Clear():Clear the queue –IsEmpty():Check to see if the queue in empty –IsFull():Check to see if the queue is full –Enqueue(element):Put the item element on top of the queue –Dequeue():Remove the element from the bottom of the queue

31 31 Figure 2.4 Enqueue and Dequeue operations performed on a queue © 2003 Brooks/Cole Publishing / Thomson Learning™

32 32 Recursive Data Types Sometimes referred to as circular data types, have values that are composed from values of the same type. In C, we have struct Node { int data; Node * ptr; } In Pascal: type link = ^cell; type cell = record info: integer; next: link; end;

33 33 Type Binding The determination of one of the components of a data object is called binding. Here is an example of a declaration in C programming language: int A; The declaration is interpreted as: –A location able to hold an integer number is created and a reference to it is given the name A. In this example, the identifier A is bound to some locations (known to compiler, but not to the programmer) and to the type integer.

34 34 A =B+C A binding between names and locations must be performed to obtain the addresses of A,B and C. A binding between locations and values must be performed to retrieve the data from the addresses of B and C Compute the value of expression B+C A new binding between the location and its computed value must be established to store the resulting value in the address of A

35 35 Figure 2.5 A data object and its bindings © 2003 Brooks/Cole Publishing / Thomson Learning™

36 36 Type Binding Binding can occur at three specific times, which give them their names. –Compile time binding, also known as static binding, sometimes referred to as early bindings, occurs when the program is being translated into machine language. –Load time binding occurs when the machine code generated by the compiler is being stored to memory locations; location binding occurs at load time. –Run time binding, also known as dynamic binding, sometimes referred to as late bindings, occurs when the program is being executed.

37 37 Symbol Table Mathematically speaking, the symbol table is a function from names to attributes: Symbol Table : Names => Attributes It is a data structure used to store information about the source language constructs.

38 38 Figure 2.6 Binding of attributes to names © 2003 Brooks/Cole Publishing / Thomson Learning™ #include main() { int x, *p; x = 10 } Symbol table Nametype address value … main function0x-------- xint0x---0->10 ppointer0x---0 Environment memory

39 39 Figure 2.7 Symbol table maintained by a compiler © 2003 Brooks/Cole Publishing / Thomson Learning™

40 40 Figure 2.8 Symbol Table maintained by an interpreter © 2003 Brooks/Cole Publishing / Thomson Learning™

41 41 Type Checking Type checking is the process a translator goes through to verify that all constructs in a program are valid in terms of the types of its constants, variables, functions and other entities. For example, in the following statements in C language: Z  X  5  Y; Switch (X  2, 2.5, Y); Y must be of a type that permits multiplication by an integer, (5  Y). Similarly, the types of the actual parameters for the call to function Switch must be checked for compatibility with the types of the formal parameters.

42 42 Type Checking (cnt’d) Type checking can occur at compile time, at run time, or not at all. Compile time checking: Indicating that type checking must be performed by the compiler with regard to the declaration of data objects. It can be viewed as static type checking, in which type information is maintained and checked at translation time. (C++) Run time checking: Indicating that type checking must be performed every time a data object is accessed. It can be viewed as dynamic type checking in which type information is maintained and checked at execution time. (Smalltalk)

43 43 Type Conversion Type conversion deals with two different data types in the evaluation of an expression. For example, in the assignment statement A  B  C; the right operand of the assignment operator “  ” is the expression B  C that assigns a value to the left operand A as the result of the execution. Converting data types may be done either implicitly or explicitly. Conversion needs internal mechanisms to support.

44 44 Implicit Data Type Conversion Programming languages that allow implicit data type conversions provide a list of all pairs of types for which data type conversion is permitted. For example, in Pascal the assignment statement x:=y is legal when y is of type int and x is of type real, indicating that the integer value y is converted to a real value to be stored in x. x= y is valid in C++, too. x= y is valid in C++, too. int x; int x; float y = 1.25; float y = 1.25; x = y ; x = y ;

45 45 Mixed Types In C, mixed types are permitted. For instance, the assignment statement x  y  2.5; where x and y are type int, can be performed with conversion types. In this case, y is converted to a real, and the real value of y  2.5 is truncated to an integer and can be assigned to x. Consider the following statements in Java language. byteA, B, C; A  B  C; In this case, the values of B and C are coerced to int and an integer addition is performed. Then the sum (B  C) is converted to byte and stored in variable A.

46 46 Explicit Data Type Conversion When specific functions are called to perform the conversion from one type to another, the data type conversion is explicit. For example, in Ada the function FLOAT can convert any type to an equivalent type float. Such conversions are normally permitted between numeric data types.

47 47 Examples Functions trunc and round in Pascal and TRUNC and FLOAT in Modula-2. Another kind of explicit type conversion is with a cast, meaning that a value or an object of one type can be converted to an equivalent value of another type. For example, the C fragment inta; inta; floatb  12.5; a  (int) b;// same as a = int(b) in C++; truncates the real value 12.5 to 12, thus performing an actual conversion with 12 as the value of the variable a.

48 48 Summary Data object (4 components) –L,N,V,T Type –Set of values –Set of operations –The internal representation –The external representation Type Checking (2 kinds) –Compile time and run time Type conversion (2 kinds) –Implicit and explicit


Download ppt "1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003."

Similar presentations


Ads by Google