Itps - One Shot Notes of All Units
Itps - One Shot Notes of All Units
Itps - One Shot Notes of All Units
Compiled by : Subhayu
es
○ Design a systematic approach to solving the problem.
○ This can include choosing the right algorithm, techniques, and tools.
at
3. Carrying Out the Plan:
○ Implement the solution through coding or a series of steps.
pd
○ Execute the plan by writing the program or performing the operations.
4. Looking Back (Verifying):
○ Check the correctness of the solution.
U
○ Ensure that the solution meets the problem's requirements and performs as
expected.
U
● Common symbols:
○ Oval: Start or End.
○ Rectangle: Process or Action.
○ Diamond: Decision.
○ Parallelogram: Input/Output.
○ Arrows: Flow of control.
Algorithms:
● Preprocessor Directives:
○ #include <stdio.h> for standard input/output functions.
Main Function:
int main() {
// Code execution starts here
return 0;
es
}
●
at
● Functions:
pd
○ Functions are blocks of code designed to perform specific tasks.
I/O Functions in C:
U
Example:
C
Header Files:
● Predefined files that contain function prototypes, constants, and other useful things.
● Common headers: stdio.h, stdlib.h, math.h.
Data Types:
● Constant: A fixed value that cannot be changed during the execution (e.g., const int
MAX = 10;).
● Variable: A named storage that holds a value that can change during the execution (e.g.,
int age = 25;).
Operators:
es
● Arithmetic Operators: +, -, *, /, %
● Relational Operators: ==, !=, >, <, >=, <=
at
● Logical Operators: &&, ||, !
● Assignment Operators: =, +=, -=, *=, /=
pd
● Bitwise Operators: &, |, ^, ~, <<, >>
● Increment/Decrement Operators: ++, --
U
Type Conversion:
● Implicit Type Conversion: The compiler automatically converts one data type to another
(e.g., int to float).
● Explicit Type Conversion (Casting): The programmer manually converts types (e.g.,
(float)a).
● Operator Precedence: The priority of operators (e.g., * has higher precedence than +).
● Associativity: Determines the order of evaluation for operators of the same precedence
(left to right or right to left).
Chapter 3: Decision Control Structures in C
Decision-Making Statements:
1. if statement:
2. if-else statement:
es
Executes one block of code if the condition is true, and another block if it is false.
if (condition) { at
// If block
} else {
pd
// Else block
}
U
U
3. if-else-if statement:
C
4. switch statement:
○ A more efficient way to handle multiple conditions.
Used when checking multiple possible values of a single variable.
switch (variable) {
case value1:
// Code block for value1
break;
case value2:
// Code block for value2
break;
default:
// Default block if no case matches
}
es
A control structure inside another control structure (e.g., an if statement inside another if
statement). at
if (condition1) {
if (condition2) {
pd
// Nested if block
}
}
U
U
C
ASSESSMENT SECTION
es
7. Expressions in C: What is operator precedence? Write a C expression and explain how
the precedence of operators affects its evaluation.
at
8. Type Conversion: Explain the concept of type conversion in C. What are implicit and
explicit conversions? Provide examples of both.
9. Decision Control Structures: Discuss the different decision-making statements in C.
pd
How do if, if-else, and switch work? Provide an example for each.
10. Nesting of Decision Control Structures: What is nesting of decision structures in C?
U
Explain with an example where if-else statements are nested inside one another.
U
C
UNIT 2: DEALING WITH REAL-WORLD PROBLEMS
Looping Statements:
1. for loop:
○ Used when the number of iterations is known beforehand.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
es
Example:
for (int i = 0; i < 5; i++) { at
printf("%d\n", i);
}
pd
U
2. while loop:
○ Used when the number of iterations is unknown and the loop continues as long
U
Syntax:
while (condition) {
// Code to be executed
}
Example:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
3. do-while loop:
○ Similar to the while loop, but guarantees that the code will run at least once, as
the condition is checked after the loop body.
Syntax:
do {
// Code to be executed
} while (condition);
Example:
int i = 0;
do {
es
printf("%d\n", i);
i++;
} while (i < 5);
at
○
pd
Nested Loops:
U
● A loop inside another loop. Can be useful for multidimensional arrays or performing
repetitive tasks within tasks.
U
Jumping Statements:
1. goto:
○ Used to jump to a specific label within the program.
Syntax:
goto label;
○
Example:
if (x == 10) {
goto end; // Jump to 'end' label
}
// some code
end:
printf("Jumped to end\n");
○
2. break:
○ Used to exit a loop prematurely.
Example:
for (int i = 0; i < 10; i++) {
es
if (i == 5) {
break; // Exit the loop when i equals 5
at
}
printf("%d\n", i);
pd
}
○
U
3. continue:
○ Skips the current iteration of the loop and proceeds to the next iteration.
U
Example:
C
c
Copy code
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue; // Skip printing 5
}
printf("%d\n", i);
}
● Array: A collection of elements of the same data type stored in contiguous memory
locations.
Declaration:
data_type array_name[size];
●
○ Example: int arr[5];
● Initialization:
During declaration:
int arr[5] = {1, 2, 3, 4, 5};
es
Or after declaration:
int arr[5];
arr[0] = 1;
arr[1] = 2;
at
pd
○
U
One-Dimensional Arrays:
Two-Dimensional Arrays:
Accessing elements:
arr[0][2]; // Accesses the third element of the first row
Searching in Arrays:
1. Linear Search:
○ Iterates through the array sequentially to find a match.
Example:
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
printf("Element found at index %d\n", i);
break;
}
}
2. Binary Search:
○ Requires a sorted array. Divides the search interval in half.
es
Example (recursive):
int binarySearch(int arr[], int low, int high, int target) {
if (high >= low) { at
int mid = (high + low) / 2;
if (arr[mid] == target)
pd
return mid;
if (arr[mid] > target)
U
}
return -1;
C
Sorting in Arrays:
1. Bubble Sort:
○ Repeatedly steps through the list, compares adjacent elements, and swaps them
if they are in the wrong order.
Example:
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
2. Selection Sort:
○ Divides the array into sorted and unsorted sections and repeatedly selects the
minimum from the unsorted part to place at the end of the sorted section.
Example:
c
Copy code
es
for (int i = 0; i < n-1; i++) {
int minIdx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[minIdx])
at
pd
minIdx = j;
}
// Swap arr[i] with arr[minIdx]
U
arr[minIdx] = temp;
}
C
String Handling:
Declaration:
char str[50] = "Hello";
String Storage:
● In C, strings are stored as arrays of characters, and each string must have space
allocated for the null terminator \0.
es
Example:
char str[20] = "Hello"; at
printf("%s\n", str);
pd
●
U
Chapter 6: Functions
U
Concepts of Functions:
C
Function Syntax:
Function Declaration:
return_type function_name(parameter_list);
Function Definition:
return_type function_name(parameter_list) {
// Code to execute
}
Function Call:
function_name(arguments);
Parameter Types:
● Actual parameters: The values or variables passed to the function when called.
● Formal parameters: The parameters listed in the function declaration.
Parameter Passing:
es
1. Pass by Value: A copy of the argument is passed to the function.
○ Changes made to the parameter in the function do not affect the original
argument. at
2. Pass by Reference: A reference to the actual argument is passed to the function.
○ Changes made to the parameter affect the original argument.
pd
Recursive Functions:
U
Example:
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
Macros:
● Macro: A preprocessor directive that defines a code snippet that will be substituted
before compilation.
● Example:
#define SQUARE(x) ((x) * (x))
ASSESSMENT QUESTIONS
1. For Loop: Explain the syntax of the for loop in C. What are its components? How is it
different from the while loop?
2. While Loop: Describe the syntax and working of the while loop. When would you prefer
to use a while loop over a for loop?
3. Do-While Loop: What is the difference between a while loop and a do-while loop? In
which situations would a do-while loop be more useful?
es
4. Break and Continue: What is the purpose of the break and continue statements in C?
Explain with examples how these statements control loop execution.
at
5. Array Declaration and Initialization: Explain the declaration and initialization of arrays in
C. What is the difference between one-dimensional and two-dimensional arrays?
pd
6. Searching and Sorting in Arrays: Describe how searching and sorting are performed on
arrays. Explain the linear search and bubble sort algorithms with examples.
7. String Handling: Discuss the common string handling functions in C. How are strings
U
Pointers
Basics of Pointers:
Syntax:
data_type *pointer_name;
Example:
int a = 10;
int *ptr = &a; // ptr stores the address of a
es
Dereferencing: To access the value stored at the address the pointer points to.
printf("%d", *ptr); // Dereferencing the pointer to get the value of
at
a (10)
pd
Double Pointer:
U
Syntax:
C
data_type **pointer_name;
Example:
int a = 10;
int *ptr = &a;
int **ptr2 = &ptr; // ptr2 points to ptr, which points to a
Example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to arr[0]
printf("%d", *(ptr + 2)); // Prints 3 (equivalent to arr[2])
Pointer to Array:
Syntax:
int (*ptr)[size];
Example:
int arr[5] = {1, 2, 3, 4, 5};
int (*ptr)[5] = &arr; // ptr points to the entire array arr
printf("%d", (*ptr)[2]); // Prints 3
es
Array of Pointers:
Syntax:
data_type* function_name() {
// return pointer
}
Example:
int* returnPointer() {
static int x = 10; // static ensures x's memory persists
return &x;
}
Storage Classes:
Structures
Basics of Structures:
● A structure is a user-defined data type that allows the grouping of variables of different
es
data types.
Syntax: at
struct structure_name {
data_type member1;
pd
data_type member2;
// More members
U
};
Example:
U
struct Student {
char name[50];
C
int age;
};
Structure Members:
Accessing members:
struct Student s1;
s1.age = 20;
strcpy(s1.name, "John");
Structure vs. Union:
● Structure: Can store multiple variables of different data types, and each member has its
own memory.
● Union: Shares the same memory location for all members; only one member can store a
value at a time.
Example:
union Data {
int i;
float f;
};
es
Dot (.) Operator: Used for accessing members of a structure.
struct Student s1; at
s1.age = 25;
pd
Pointer to Structure: Use -> operator when accessing members via a pointer.
U
Nested Structures:
struct Student {
char name[50];
struct Address addr;
};
Array of Structures:
es
at
Pointers can be used to point to a structure, and the members can be accessed using the ->
pd
operator.
U
● Dynamic memory allocation refers to allocating memory at runtime using functions like
malloc(), calloc(), realloc(), and free().
● These functions are part of the stdlib.h library.
malloc():
● Allocates a specified number of bytes of memory and returns a pointer to the first byte.
● If allocation fails, it returns NULL.
Syntax:
void* malloc(size_t size);
Example:
int *arr = (int*)malloc(5 * sizeof(int)); // Allocates memory for 5
integers
if (arr == NULL) {
printf("Memory allocation failed\n");
}
calloc():
Syntax:
void* calloc(size_t num_elements, size_t size_of_element);
Example:
es
int *arr = (int*)calloc(5, sizeof(int)); // Allocates and initializes
5 integers to zero
at
pd
realloc():
U
Syntax:
void* realloc(void* ptr, size_t new_size);
C
Example:
arr = (int*)realloc(arr, 10 * sizeof(int)); // Resizes the array to
hold 10 integers
free():
Syntax:
free(ptr);
Example:
free(arr); // Frees the memory allocated for arr
Summary
● Pointers: Essential for dynamic memory management, and accessing and manipulating
data indirectly.
● Structures: Grouping of different data types under a single name, useful for handling
complex data.
● Dynamic Memory Allocation: Allows efficient memory use by allocating and deallocating
memory during runtime, avoiding fixed memory allocation limitations.
ASSESSMENT SECTION
es
Unit 3: Handling Heterogeneous Data and Memory Management
at
1. Basics of Pointers: What is a pointer in C? Explain how pointers are used to store
memory addresses and access data indirectly.
pd
2. Pointer to Array: How do you declare and use a pointer to an array in C? What is the
difference between using an array and using a pointer to an array?
3. Pointer and Functions: Discuss how pointers are used with functions. Explain how a
U
with an example how each is used and when to prefer one over the other.
6. Nested Structures: What is a nested structure in C? How can structures be used within
other structures? Provide an example.
7. Array of Structures: How do you declare and use an array of structures in C? Provide an
example to demonstrate accessing members of array elements.
8. Structures and Functions: How are structures passed to functions in C? Can structures
be returned from functions? Provide examples.
9. Dynamic Memory Allocation (malloc, calloc): Explain the use of malloc and calloc for
dynamic memory allocation in C. What is the difference between them? Provide an
example for each.
10. Realloc: What is the purpose of realloc in C? How does it help in resizing dynamically
allocated memory? Provide an example to demonstrate its use.