Primitive data
- Primitive Data Types
Definition: Primitive data types are the basic data types provided by a programming language to store simple values such as numbers, characters, and logical values.
Types and Their Definitions:
1. int: Used to store whole numbers.
Example:
int age = 20; // Stores 20
2. float: Used to store decimal numbers.
Example:
float pi = 3.14; // Stores 3.14
3. char: Used to store single characters.
Example:
char grade = 'A'; // Stores 'A'
4. double: Used to store large decimal numbers.
Example:
double value = 12345.6789; // Stores large decimal numbers
Uses:
To perform basic operations like storing marks, names, or IDs.
Merits:
Fast to use and predefined by the language.
Efficient for small programs.
Demerits:
Limited flexibility compared to advanced data structures.
— -
2. Self-Referential Structures
Definition: A self-referential structure is a structure that contains a pointer to the same type of structure.
Example:
struct Node {
int data;
struct Node *next; // Points to another Node
};
Uses:
Essential for creating linked lists, trees, and graphs.
Merits:
Allows dynamic memory usage.
Facilitates complex data representations like graphs.
Demerits:
Requires proper memory management (e.g., freeing memory).
— -
3. Pointers and Structures
Definition: A pointer stores the memory address of another variable. When combined with structures, it allows indirect access to structure members.
Example:
struct Student {
int roll;
char name[20];
};
struct Student s1 = {101, "Rahul"};
struct Student *ptr = &s1; // Pointer to structure
printf("%d %s", ptr->roll, ptr->name); // Access with ->
Uses:
Efficiently handle and modify structures dynamically.
Simplifies linked list implementations.
Merits:
Allows dynamic memory allocation and reuse.
Demerits:
Can lead to memory leaks if not managed properly.
— -
4. Dynamic Memory Allocation
Definition: Allocating memory at runtime instead of at compile time.
Types and Definitions:
1. malloc: Allocates uninitialized memory.
Example:
int *arr = (int *)malloc(5 * sizeof(int)); // Allocates memory for 5 integers
2. calloc: Allocates memory and initializes it to 0.
Example:
int *arr = (int *)calloc(5, sizeof(int)); // Allocates and initializes to 0
3. realloc: Resizes previously allocated memory.
Example:
arr = (int *)realloc(arr, 10 * sizeof(int)); // Resizes to hold 10 integers
4. free: Frees allocated memory.
Example:
free(arr); // Releases allocated memory
Uses:
Useful for creating dynamic data structures like resizable arrays.
Merits:
Optimizes memory usage.
Enables flexible and scalable programs.
Demerits:
Can cause memory leaks if not handled properly.
— -
5. Matrix Multiplication
Definition: The process of multiplying rows of one matrix by columns of another matrix.
Example:
For Matrix A and Matrix B:
A = 1 2 B = 5 6
3 4 7 8
Result:
C = (1*5 + 2*7) (1*6 + 2*8) => 19 22
(3*5 + 4*7) (3*6 + 4*8) => 43 50
Uses:
Important in graphics, simulations, and algorithms.
Merits:
Easy to implement for small matrices.
Demerits:
Computationally expensive for large matrices.
— -
6. Data Structures
Definition: A way to organize and store data efficiently.
Types and Definitions:
1. Linear: Data stored sequentially.
Examples: Arrays, Linked Lists.
2. Non-Linear: Data stored hierarchically.
Examples: Trees, Graphs.
Uses:
Arrays for indexing, Trees for searching, Graphs for networks.
Merits:
Optimizes operations like searching and sorting.
Demerits:
Some require more memory or complex implementations.
— -
7. Mathematical Notations (Big O, Omega, Theta)
Definition: Measures an algorithm’s efficiency in terms of time and space.
Types and Definitions:
1. Big O (O): Upper bound (worst case).
Example: O(n²) for Bubble Sort in the worst case.
2. Omega (Ω): Lower bound (best case).
Example: Ω(n) for Bubble Sort if sorted.
3. Theta (Θ): Average case.
Uses:
To evaluate algorithm performance.
Merits:
Provides clear efficiency comparison.
Demerits:
Theoretical and doesn’t account for real-world factors.
— -
8. Complexity
Definition: The measure of the efficiency of an algorithm in terms of time and space.
Types and Definitions:
1. Time Complexity: Time required for an algorithm to execute.
Example: O(n) for Linear Search.
2. Space Complexity: Memory required for execution.
Example: O(n) for an array of size n.
Uses:
Helps in choosing efficient algorithms.
Merits:
Saves resources by choosing optimal solutions.
Demerits:
Doesn’t account for real-world constraints.
— -
9. Trade-Off
Definition: Balancing between two conflicting goals, such as time and space.
Example:
Sorted Array: Faster search but uses more memory to maintain order.
Unsorted Array: Saves space but slower search.