1.
Elementary and Structured Data Types
Elementary Data Types
Elementary (or primitive) data types are the building blocks for programming.
1. Integer
o Specification:
Stores whole numbers.
Size varies: typically 4 bytes (32 bits).
Range:
Signed: −231-2^{31}−231 to 231−12^{31}-1231−1.
Unsigned: 000 to 232−12^{32}-1232−1.
o Implementation:
Stored in binary.
Arithmetic operations are performed using CPU instructions.
2. Float/Double
o Specification:
Represents real numbers with fractional parts.
Float: 4 bytes (single precision).
Double: 8 bytes (double precision).
Representation: IEEE 754 standard.
o Implementation:
Binary format: sign bit, exponent, mantissa.
3. Character
o Specification:
Stores a single character (e.g., 'A', '1').
Size: 1 byte (ASCII) or 2-4 bytes (Unicode).
o Implementation:
Encoded as integers (ASCII or Unicode values).
4. Boolean
o Specification:
Represents logical values: true or false.
Size: 1 bit or 1 byte (depending on the language).
o Implementation:
Stored as 0 (false) or 1 (true).
Structured Data Types
Structured types combine elementary types to create more complex entities.
1. Arrays
o Specification:
Collection of elements of the same type.
Fixed size (static arrays) or dynamic size.
o Implementation:
Contiguous memory allocation.
Accessed via indices: arr[index].
2. Records (Structs)
o Specification:
Group of fields, each with a name and type.
o Implementation:
Memory allocated contiguously for fields.
Accessed using dot notation: record.field.
3. Strings
o Specification:
Sequence of characters.
Fixed or variable length.
o Implementation:
Null-terminated arrays of characters (C-style) or dynamic objects (e.g., std::string in
C++).
4. Lists
o Specification:
Ordered collections that can grow/shrink dynamically.
o Implementation:
Linked list or dynamic arrays.
5. Sets
o Specification:
Unordered collection of unique elements.
o Implementation:
Hash tables or balanced binary trees.
2. Type Checking and Type Conversion
Type Checking
Definition: Verifying the compatibility of data types during operations.
Types:
o Static Type Checking: Performed at compile-time (e.g., C, Java).
o Dynamic Type Checking: Performed at runtime (e.g., Python).
Type Conversion
Definition: Changing a value from one type to another.
Types:
1. Implicit Conversion (Type Coercion):
Automatically handled by the compiler/interpreter.
Example: int x = 5; float y = x;
2. Explicit Conversion (Type Casting):
Done manually by the programmer.
Example: (float) 10 / 3;
3. Vector Arrays
Definition: One-dimensional dynamic arrays, useful in numerical computations.
Specification:
o Elements are homogeneous.
o Allow operations like addition, scalar multiplication, dot product.
Implementation:
o Dynamically allocated arrays.
o Libraries (e.g., NumPy in Python) optimize storage and operations.
4. Records
Definition: A composite data structure grouping related fields of potentially different types.
Specification:
o Fields (attributes) have names and types.
o Accessed using the field name.
Implementation:
o Contiguous memory allocation.
o Examples:
5. Character Strings
Definition: Sequences of characters used for text processing.
Specification:
o Fixed-length or dynamic.
o Encodings: ASCII, UTF-8, UTF-16.
Implementation:
o C-style: Null-terminated array of characters (char str[20] = "Hello";).
o Object-oriented: Classes or libraries (std::string in C++).
6. Variable Size Data Structures
Definition: Structures whose size can change dynamically during execution.
Examples:
o Dynamic Arrays (e.g., Python lists, Java ArrayList).
o Linked Lists: Use pointers to connect elements.
o Trees: Dynamic hierarchical structures.
o Graphs: Networks of nodes and edges.
Implementation:
o Uses dynamic memory allocation (e.g., malloc() in C, new in C++).
7. Sets
Definition: Unordered collections of unique elements.
Operations:
o Union, intersection, difference, membership checking.
Implementation:
o Hash tables (e.g., Python’s set).
o Balanced binary trees (e.g., C++ std::set).
8. Input and Output Files
Input Files
Definition: Used to read data into a program.
Operations:
o Open, read, close.
Implementation:
Output Files
Definition: Used to write data from a program.
Operations:
o Open, write, close.
Implementation: