Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

PROGRAMMING AND PROBLEM SOLVING

PROGRAMMING & PROBLEM SOLVING (MODULE 1)


1.1 POINTER TO POINTERS :
In C, we can also define a pointer to store the address of another pointer. This creates many layers of pointer and
therefore called as pointers to pointers (Double pointer / Multiple Indirection).
The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the
first pointer. Let's understand it by the diagram given below.

Consider the following example


#include <stdio.h>
#include <conio.h>
void main() 
{
  int i =100;
  int **pt1;
  int *pt2;
  pt2 = &i;
  pt1 = &pt2;
  printf("The value of **pt1 = %d" + "The value of *pt2 = %d", **pt1, *pt2);
  getch();
}

In the above example we have declared an integer variable i initialized to 100. The statement int ** pt creates a pointer
which points to pointer to a variable with int value. We have passed the value of i to pointer pt2 and to pass the value
of pt2 to pt1, we have used the statement pt1 = &pt2; which shows the Multiple Indirection.
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.3 DYNAMIC ALLOCATION
• In dynamic allocation, memory is allocated at run time.
• In dynamic allocation, memory can be increased while executing program
• Dynamic allocation makes efficient use of memory by allocating the required amount of
memory whenever is needed.
• Dynamic allocation allows building complex data structures such as linked lists.
• Dynamic allocation takes place in the heap segment of memory.
• C provides the following dynamic allocation :
(1) malloc() : allocates single block of requested memory.
(2) calloc() : allocates multiple block of requested memory.
(3) realloc() : reallocates the memory occupied by malloc() or calloc() functions.
(4) free() : frees the dynamically allocated memory.
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.2 POINTER TO FUNCTIONS :
• A pointer to a function points to the address of the executable code of the function.
EXAMPLE :
#include <stdio.h>
#include <string.h>
void check(char *a, char *b,
int (*cmp)(const char *, const char *));
int main(void)
{
char s1[80], s2[80];
int (*p)(const char *, const char *);
p = strcmp;
gets(s1);
gets(s2);
check(s1, s2, p);
return 0;
}
void check(char *a, char *b,
int (*cmp)(const char *, const char *))
{
printf("Testing for equality.\n");
if(!(*cmp)(a, b)) printf("Equal");
else printf("Not Equal");
}
PROGRAMMING & PROBLEM SOLVING (MODULE 1)
1.5 PASSING SINGLE DIMENSION ARRAYS TO FUNCTION
i n t main ( v o i d )
{
inti[10];
func1(i);
.
.
}
If a single dimension array is passing to a function we may declare its formal parameter in one of three ways: as a pointer, as a sized array, or as an
unsized array.
void func1 (int*x) /* pointer */
{
.
.
}
or
void func1 (intx [10]) /* size d array */
{
.
.
}
or finally as
void func1 (intx [ ]) /* unsized array */
{
.
.
.
}

All three declaration methods produce similar results The first declaration actually uses a pointer , second employs the standard array
declaration and the third is a modified version of an array declaration.
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
1. STRUCTURE :
Structure in c is a user-defined data type that enables us to store the collection of different data types.
[Simple Structure program / (Demo C program that In certain circumstances use of structure is better than array)]
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
2. ARRAY OF STRUCTURE :
An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about
different entities.
Example :
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
3. PASSING STRUCTURE TO FUNCTIONS :
We can pass structure in two ways : 1. Passing Structure Members to Functions 2. Passing Entire Structures to Functions
3.1 Passing Structure Members to Functions
When we pass a member of a structure to a function, we are passing the value of that member to the function.
struct fred
{
char x;
int y;
float z;
char s[2];
} mike;

Here each member being passed to a function :


func(mike.x); /* passes character value of x */
func2(mike.y); /* passes integer value of y */
func3(mike.z); /* passes float value of z */
func4(mike.s); /* passes address of string s */
func(mike.s[2]); /* passes character value of s[2] */
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
3. PASSING STRUCTURE TO FUNCTIONS :
3.2 Passing Structure Entire Members to Functions
When a structure is used as an argument to a function, the entire structure is passed using the normal call-by-value method.
Example :
#include<stdio.h>
struct add
{
int var1;
int var2;
}a;
void show(struct add a)
{
int sum;
sum=a.var1+a.var2;
printf("Added value is %d",sum);
}
void main()
{
struct add a;
printf("Enter variable 1 = ");
scanf("%d",&a.var1);
printf("Enter variable 2 = ");
scanf("%d",&a.var2);
show(a);
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
4. STRUCTURE POINTER :
The pointer which points to the address of the memory block that stores a structure is known as the structure pointer.
#include <stdio.h>
struct Employee
{
char name[30];
int id;
int age;
char gender[30];
char city[40];
};
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
4. STRUCTURE POINTER :
struct Employee emp1,*ptr1;
int main()
{
ptr1 = &emp1;
printf (" Enter the name of the Employee (emp1): ");
scanf (" %s", &ptr1->name);
printf (" Enter the id of the Employee (emp1): ");
scanf (" %d", &ptr1->id);
printf (" Enter the age of the Employee (emp1): ");
scanf (" %d", &ptr1->age);
printf (" Enter the gender of the Employee (emp1): ");
scanf (" %s", &ptr1->gender);
printf (" Enter the city of the Employee (emp1): ");
scanf (" %s", &ptr1->city);

printf ("\n Display the Details of the Employee1 using Structure Pointer");
printf(" Name: %s", ptr1->name);
printf(" Id: %d", ptr1->id);
printf(" Age: %d\n", ptr1->age);
printf(" Gender: %s\n", ptr1->gender);
printf(" City: %s\n", ptr1->city);
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
5. BIT FIELDS :
 C has a built-in feature called a bit-field that allows us to access a single bit.
Example :
#include <stdio.h>
#include <string.h>
struct
{
unsigned int widthValidated;
unsigned int heightValidated;
} status1;
struct
{
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( )
{
printf( "Memory size occupied by status1 : %d", sizeof(status1));
printf( "Memory size occupied by status2 : %d", sizeof(status2));
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
(1) BINARY TREE :
A binary tree is a data structure that is defined as a collection of elements called nodes.

From the above fig:


R is the root node and the two trees T1 and T2 are called the left and right sub-trees of R.
T1 is said to be the left successor of R & T2 is called the right successor of R.
The left sub-tree of the root node consists of the nodes: 2, 4, 5, 8, and 9 & the right sub-tree of the root
node consists of nodes: 3, 6, 7,10, 11, and 12.
In the tree,
Root Node 1 has two successors: 2 and 3.
Node 2 has two successor nodes: 4 and 5.
Node 3 has two successor nodes: 6 and 7.
Node 4 has two successors: 8 and 9.
Node 5 has no successor.
Node 6 has two successors: 10 and 11.
Node 7 has only one successor: 12
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
(2) COMPLETE BINARY TREE :
 A complete binary tree is a binary tree that satisfies two properties.
(i) First, in a complete binary tree, every level, except possibly the last, is completely filled
(ii) Second, all nodes appear as far left as possible
• The below figure is a complete binary tree
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
(4) TRAVERSING A BINARY TREE
(i) Pre-order traversal :
To traverse a non-empty binary tree in pre-order, the following operations are performed recursively at each
node. The algorithm works by:
1. Visiting the root node, 2. Traversing the left sub-tree, and finally 3. Traversing the right sub-tree.

The pre-order traversal of the tree is given as A, B, C. Root node first, the left sub-tree next, and then the right
sub-tree. In this algorithm, the left sub-tree is always traversed before the right sub-tree.
(ii) In-order traversal :
To traverse a non-empty binary tree in in-order, the following operations are performed recursively at each
node. The algorithm works by:
2. Traversing the left sub-tree, 2. Visiting the root node, and finally 3. Traversing the right sub-tree.

The in-order traversal of the tree is given as B, A, C. Left sub-tree first, the root node next, and then the right
sub-tree. In this algorithm, the left sub-tree is always traversed before the root node and the right subtree
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
(4) TRAVERSING A BINARY TREE
(iii) Post-order traversal :
To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each
node. The algorithm works by:
1. Traversing the left sub-tree, 2. Traversing the right sub-tree, and finally 3. Visiting the root node.

The post-order traversal of the tree is given as B, C, and A. Left sub-tree first, the right sub-tree next,
and finally the root node. In this algorithm, the left sub-tree is always traversed before the right
sub-tree and the root node.
Examples of all of these traversals

Example of Pre-order traversal : TRAVERSAL ORDER: A, B, D, G, H, L, E, C, F, I, J, and K


Example of In-order traversal : TRAVERSAL ORDER: G, D, H, L, B, E, A, C, I, F, K, and J
Example of Post-order traversal : TRAVERSAL ORDER: G, L, H, D, E, B, I, K, J, F, C, and A
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
(5) BREADTH FIRST SEARCH ALGORITHM
• Breadth-first search is the most common search strategy for traversing graph. This algorithm searches
breadthwise in a graph, so it is called breadth-first search.
• BFS algorithm starts searching from the root node and expands all successor node at the current level
before moving to nodes of next level.
Algorithm for breadth first search
Step 1: SET STATUS=1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS=2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS=3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS=1) and set their STATUS=2
[END OF LOOP]
Step 6 : EXIT

You might also like