Professional Documents
Culture Documents
Programming and Problem Solving Final
Programming and Problem Solving Final
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;
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.
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