Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Itps - One Shot Notes of All Units

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

ITPS (24CSH-101) - One shot Notes of ALL UNITS

Compiled by : Subhayu

UNIT 1: INTRODUCTION TO BASIC BUILDING BLOCKS OF PROGRAMMING

Chapter 1: Introduction to Problem Solving

Problem Solving Phases:

1. Understanding the Problem:


○ Analyze the problem, understand the requirements, and identify the inputs and
expected outputs.
○ Break down the problem into smaller, manageable parts.
2. Making a Plan of Solution:

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

○ Debug any errors or issues that arise.


C

Flowcharts: A diagrammatic representation of a process or algorithm.

● Common symbols:
○ Oval: Start or End.
○ Rectangle: Process or Action.
○ Diamond: Decision.
○ Parallelogram: Input/Output.
○ Arrows: Flow of control.

Algorithms:

● A step-by-step procedure to solve a problem.


● Should be:
○ Finite: The algorithm must terminate after a finite number of steps.
○ Unambiguous: Each step must be clear and understandable.
○ Effective: Every step should be executable.
Chapter 2: Fundamentals of C

Features & Applications of C Language:

● General-purpose language: Used for system/software development.


● Structured: Divides programs into functions for easier readability and maintainability.
● Portability: C programs can be run on different machines with minimal modification.

Structure of Writing a C Program:

● 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

● printf(): Used to display output on the screen.


● scanf(): Used to take input from the user.
U

Example:
C

printf("Enter a number: ");


scanf("%d", &num);

Indentation and Comments:

● Indentation: Helps in maintaining readability of the code.


● Comments:
○ Single-line: // comment here
○ Multi-line: /* comment here */

Header Files:

● Predefined files that contain function prototypes, constants, and other useful things.
● Common headers: stdio.h, stdlib.h, math.h.

Data Types:

● Primitive types: int, char, float, double.


● Derived types: arrays, pointers, structures.
● Void type: Used for functions that do not return any value.

Constants and Variables:

● 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

Expressions and Evaluation:


U

● Expression: A combination of operators and operands that results in a value (e.g., a +


b).
C

● Evaluation of Expressions: The process of computing the value of an expression


according to operator precedence and associativity.

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).

Precedence and Associativity:

● 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:

Executes a block of code if a specified condition is true.


if (condition) {
// Code block
}

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

Tests multiple conditions in sequence.


if (condition1) {
// If block 1
} else if (condition2) {
// If block 2
} else {
// Else block
}

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
}

Nesting of Decision Control Structures:

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

Unit 1: Introduction to Basic Building Blocks of Programming

1. Problem Solving Phases: Explain the four phases of problem-solving in programming.


Provide an example of how these phases can be applied to solve a simple problem.
2. Flowchart: Define a flowchart. What are its components? Explain with an example how a
flowchart helps in problem-solving.
3. Algorithm: What is an algorithm? Discuss the characteristics of a good algorithm and
write a basic algorithm to find the sum of two numbers.
4. C Program Structure: Describe the basic structure of a C program. What are the different
sections of a C program? Explain with an example.
5. I/O Functions in C: What is the purpose of the printf() and scanf() functions in C?
Explain their syntax and use cases.
6. Operators in C: Discuss the different types of operators in C. Provide examples of
arithmetic, relational, and logical operators.

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

Chapter 4: Loop Control Structures in C

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

as the condition is true.


C

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

Example (nested for loop):


C

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
printf("i=%d, j=%d\n", i, j);
}
}

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);
}

Chapter 5: Arrays & Strings


Concepts of Array:

● 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:

A simple list of values.


U

int arr[5] = {1, 2, 3, 4, 5};


C

Two-Dimensional Arrays:

An array of arrays, useful for representing matrices or grids.


int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

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 binarySearch(arr, low, mid - 1, target);


return binarySearch(arr, mid + 1, high, 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

int temp = arr[i];


arr[i] = arr[minIdx];
U

arr[minIdx] = temp;
}
C

String Handling:

● Strings are arrays of characters terminated by the null character \0.

Declaration:
char str[50] = "Hello";

Common String Functions (from string.h):


strlen(): Returns the length of a string (excluding the null character).
int len = strlen(str);

1. strcpy(): Copies one string to another.


strcpy(dest, src);
2. strcmp(): Compares two strings lexicographically.
int result = strcmp(str1, str2);
3. strcat(): Concatenates two strings.
strcat(str1, str2);

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

● Library Functions: Predefined functions available in C, e.g., printf(), scanf(),


strlen().
● User-Defined Functions: Functions created by the programmer to perform specific
tasks.

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

● A function that calls itself.


● Used to solve problems that can be broken down into smaller, similar problems (e.g.,
U

factorial, Fibonacci sequence).


C

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

Unit 2: Dealing with Real-World Problems

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

stored and manipulated in C? Provide examples.


8. Functions in C: What is the role of functions in C? Describe the syntax of a function
U

declaration and function definition. How are arguments passed to functions in C?


9. Recursive Functions: What is recursion? Explain how a recursive function works with an
C

example, such as calculating the factorial of a number.


10. Macros in C: What are macros in C? Explain how to define and use macros with an
example. What are the advantages and disadvantages of using macros?
UNIT 3: HANDLING HETEROGENEOUS DATA AND MEMORY MANAGEMENT

Pointers

Basics of Pointers:

● A pointer is a variable that stores the memory address of another variable.

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

● A pointer that points to another 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

Pointer and Array:

● An array name acts as a pointer to the first element of the array.

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:

● A pointer that holds the address of the whole 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:

● An array where each element is a pointer.


at
Syntax:
pd
data_type *arr[size];
Example:
U

int a = 10, b = 20;


int *arr[2] = {&a, &b};
printf("%d", *arr[1]); // Prints 20
U
C

Functions Returning a Pointer:

● A function can return a pointer to a variable.

Syntax:
data_type* function_name() {
// return pointer
}
Example:
int* returnPointer() {
static int x = 10; // static ensures x's memory persists
return &x;
}
Storage Classes:

● Defines the scope, lifetime, and visibility of variables.


● Auto: Default for local variables, automatically created and destroyed.
● Register: Suggests storing the variable in CPU registers.
● Static: Retains its value across function calls.
● Extern: Allows variables/functions to be used across multiple files.

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:

● Each individual variable within the structure is called a member.

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;
};

Accessing Structure Members:

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

struct Student *ptr = &s1;


ptr->age = 25;
U
C

Nested Structures:

A structure can have another structure as a member.


struct Address {
char street[100];
char city[50];
};

struct Student {
char name[50];
struct Address addr;
};
Array of Structures:

Allows storing multiple structures in an array.


struct Student students[5];
students[0].age = 20;

Structure and Functions:

● Structures can be passed to functions by value or by reference.

Passing by reference is efficient as it avoids copying the entire structure.


void display(struct Student s) {
printf("%s", s.name);
}

Structures and Pointers:

es
at
Pointers can be used to point to a structure, and the members can be accessed using the ->
pd
operator.
U

struct Student *ptr = &s1;


ptr->age = 20;
U

Dynamic Memory Allocation


C

Introduction to Dynamic Memory Allocation:

● 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():

● Similar to malloc(), but it also initializes the allocated memory to zero.

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

● Resizes previously allocated memory block.


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():

● Deallocates memory previously allocated by malloc(), calloc(), or realloc().

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

function can return a pointer in C with an example.


4. Double Pointers: What is a double pointer in C? Explain how to declare and use a double
U

pointer with an example.


5. Structure vs. Union: What is the difference between a structure and a union in C? Explain
C

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.

You might also like