List
of Practical
Of
DSC
Write programs in C to
implement
1.
Sorting
an array
2.
The
addition of two matrices using functions
3.
The
multiplication of two matrices
4.
Push
and Pop Operation in stack
5.
Inserting
and deleting elements in queue
6.
Inserting
and deleting elements in circular queue
7.
Insertion
and deletion of elements in linked list
8.
Insertion
and deletion of elements in doubly linked list
9.
The
factorial of a given number with recursion and without recursion
10.
Fibonacci
series with recursion and without recursion
11.
Program
for binary search tree operation
12.
The
selection sort technique
13.
The
bubble sort technique
14.
The
quick sort technique
15.
The
merge sort technique
16.
The
binary search procedures to search an element in a given list
17.
The
linear search procedures to search an element in a given list
PROGRAM 1: - C PROGRAM to sort elements of array in ascending order
#include<stdio.h>
#define MAX_SIZE 100 // Maximum array size
intmain()
{
intarr[MAX_SIZE];
int size;
inti, j, temp;
/*
Input size of array */
printf("Enter size of
array: ");
scanf("%d",&size);
/*
Input elements in array */
printf("Enter elements
in array: ");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
/*
* Place currently selected element
array[i]
* to its correct place.
*/
for(j=i+1; j<size;j++)
{
/*
* Swap if currently selected array
element
* is not at its correct position.
*/
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]= temp;
}
}
}
/*
Print the sorted array */
printf("\nElements of
array in ascending order: ");
for(i=0;i<size;i++)
{
printf("%d\t",arr[i]);
}
return0;
}
Input: Enter size of array 5
15 18 10 12 3
Output:
Elements
of array in ascending order: 3 10 12 15
18
PROGRAM
No -2: - Write
a C PROGRAM for addition of two 3x3 matrix.
#include<stdio.h>
#include<conio.h>
int main()
{
int matA[3][3],matB[3][3],matC[3][3];
int r,c,k;
for(r=0;
r<3; r++)
{
for(c=0;
c<3; c++)
{
printf("Enter first matrix : ");
scanf("%d", &matA[r][c]);
}
}
for(r=0;
r<3; r++)
{
for(c=0;
c<3; c++)
{
printf("Enter second matrix : ");
scanf("%d", &matB[r][c]);
}
}
for(r=0;
r<3; r++)
{
for(c=0;
c<3; c++)
{
matC[r][c]=0;
for(k=0; k<3;k++)
matC[r][c] = matA[r][c] + matB[r][c];
}
}
printf("\n
New addition matrix : \n");
for(r=0;
r<3; r++)
{
for(c=0;
c<3; c++)
printf(" %d",matC[r][c]);
printf("\n");
}
getch();
return 0;
}
Input of PROGRAM: -
Enter first matrix :
2 3 4
2 5 6
2 3 4
Enter second matrix :
5 7 8
3 4 4
3 5 6
Output of PROGRAM:
New addition matrix :
7 10 12
5 9 10
5 8 10
PROGRAM No-3.: -
Matrix multiplication in C language
#include <stdio.h>
intmain()
{
int m, n, p, q, c, d, k, sum =0;
intfirst[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter elements of first matrix\n");
for(c =0; c < m;c++)
for(d =0; d < n; d++)
scanf("%d",&first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if(n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for(c =0; c < p;c++)
for(d =0; d < q; d++)
scanf("%d",&second[c][d]);
for(c =0; c < m;c++){
for(d =0; d < q; d++){
for(k =0; k < p; k++){
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d]= sum;
sum =0;
}
}
printf("Product of the matrices:\n");
for(c =0; c < m;c++){
for(d =0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return0;
}
Input:
Enter the
number of rows and columns of first matrix
2
3
Enter the
elements of first matrix
3 4 3
5 6 2
Enter the
number of rows and columns of second matrix
3
2
Enter the
elements of second matrix
2 6 5
4 3 1
Product of
entered matrices: -
35
37
46
56
PROGRAM No.-4: - PUSH
and POP operation in Stack using array.
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5 //Maximum number of elements that can be
stored
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
while(1) //infinite loop, will end when choice will
be 4
{
printf("\n *** Stack Menu
***");
printf("\n \n1.Push \n2.Pop
\n3.Display \n4.Exit");
printf("\n \n Enter your
choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n Wrong
Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\n Stack is
full!!");
}
else
{
printf("\n Enter element to
push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\n Stack is
empty!!");
}
else
{
printf("\n Deleted element is
%d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\n Stack is
empty!!");
}
else
{
printf("\n Stack
is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
Output
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):1
Enter element to push:3
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):1
Enter element to push:6
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):3
Stack is…
6
3
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):2
Deleted element is 6
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):3
Stack is…
3
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):2
Deleted element is 3
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit
Enter your choice(1-4):2
Stack is empty!!
PROGRAM NO.-5: - Insert and Delete an element
in a queue.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int q[25], n ,front=-1 , rear=-1 , item;
void insertion()
{
if((rear==n) &&
(front==(rear+1))||(front==rear+1))
{
printf(“\nQueue Overflow\n”);
}
else if (rear==0)
front = rear = 1;
else if(rear==n)
rear=1;
else
rear=rear+1;
printf(“Enter the item : “);
scanf(“%d”,&item);
q[rear] = item;
printf(“%d is inserted\n\n”,item);
}
void deletion()
{
if(front==0)
{
printf(“\nQueue Underflow\n\n”);
}
item=q[front];
if(front==rear)
{
front=0;
rear=0;
}
else if (front=n)
front=1;
else
front=front+1;
printf(“\n%d is deleted\n\n”,item);
}
void show()
{
for(int i=0;i<=rear;i++)
printf(“%d\t”,q[i]);
}
int main()
{
int op;
printf(“Enter the size of the queue : “);
scanf(“%d”,&n);
do
{
printf(“\n1 : Insert”);
printf(“\n2 : Delete”);
printf(“\n3 : Print”);
printf(“\n4 : Exit”);
printf(“\nEnter your choice : “);
scanf(“%d”,&op);
switch(op)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
show();
break;
//default:
// printf(“Invalid Option. Try
again.”);
}
}while(op!=4);
printf(“\n—THE END—\n”);
}
OUTPUT
PROGRAM NO.-6-: - Inserting and Deleting Element in Circular
Queue.
#include<stdio.h>
#include<conio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int cQueue[SIZE], front = -1, rear = -1;
void main()
{
int choice, value;
clrscr(); /* ‘clrscr’ This will not use */
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("\nEnter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nPlease select the correct choice!!!\n");
}
}
}
void enQueue(int value)
{
if((front == 0 && rear == SIZE - 1) || (front == rear+1))
printf("\nCircular Queue is Full! Insertion not possible!!!\n");
else{
if(rear == SIZE-1 && front != 0)
rear = -1;
cQueue[++rear] = value;
printf("\nInsertion Success!!!\n");
if(front == -1)
front = 0;
}
}
void deQueue()
{
if(front == -1 && rear == -1)
printf("\nCircular Queue is Empty! Deletion is not possible!!!\n");
else{
printf("\nDeleted element : %d\n",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}
void display()
{
if(front == -1)
printf("\nCircular Queue is Empty!!!\n");
else{
int i = front;
printf("\nCircular Queue Elements are : \n");
if(front <= rear){
while(i<= rear)
printf("%d\t",cQueue[i++]);
}
else{
while(i<= SIZE - 1)
printf("%d\t", cQueue[i++]);
i = 0;
while(i<= rear)
printf("%d\t",cQueue[i++]);
}
}
}
Output
PROGRAM NO.-7-: - Inserting and Deleting Elements in Linked
List.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*first=NULL;
void insert()
{
struct node *temp;
struct node *nn=(struct node*)malloc(sizeof(struct node));
printf("enter the data\n");
scanf("%d",&nn->data);
temp=first;
while(temp->next!=first)
temp=temp->next;
temp->next=nn;
nn->next=NULL;
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("no elements\n");
return;
}
printf("elements in linked list are\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
void deletion()
{
struct node*temp;
temp=first;
first=first->next;
temp->next=NULL;
free(temp);
}
int main()
{
int op;
do
{
printf("1.insertion\n2.deletion\n3.display\n4.exi\n");
printf("enter option\n");
scanf("%d",&op);
switch(op)
{
case1:insert();
break;
case2:deletion();
break;
case3:display();
break;
}
}while(op!=6);
}
output
1.insertion
2.deletion
3.display
4.exit
enter option
2
enter the data
25
PROGRAM
8-: - Inserting and Deleting Elements in Doubly Linked List.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct
node
{
struct node *previous;
int data;
struct node *next;
}*head,
*last;
void
insert_begning(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct
node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
void
insert_end(int value)
{
struct node *var,*temp;
var=(struct node *)malloc(sizeof(struct
node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
last=head;
while(last!=NULL)
{
temp=last;
last=last->next;
}
last=var;
temp->next=last;
last->previous=temp;
last->next=NULL;
}
}
int insert_after(int
value, int loc)
{
struct node *temp,*var,*temp1;
var=(struct node *)malloc(sizeof(struct
node));
var->data=value;
if(head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
}
else
{
temp=head;
while(temp!=NULL
&& temp->data!=loc)
{
temp=temp->next;
}
if(temp==NULL)
{
printf("\n%d
is not present in list ",loc);
}
else
{
temp1=temp->next;
temp->next=var;
var->previous=temp;
var->next=temp1;
temp1->previous=var;
}
}
last=head;
while(last->next!=NULL)
{
last=last->next;
}
}
int
delete_from_end()
{
struct node *temp;
temp=last;
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
printf("\nData
deleted from list is %d \n",last->data);
last=temp->previous;
last->next=NULL;
free(temp);
return 0;
}
int
delete_from_middle(int value)
{
struct node *temp,*var,*t, *temp1;
temp=head;
while(temp!=NULL)
{
if(temp->data == value)
{
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
else
{
var->next=temp1;
temp1->previous=var;
free(temp);
return 0;
}
}
else
{
var=temp;
temp=temp->next;
temp1=temp->next;
}
}
printf("data
deleted from list is %d",value);
}
void
display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List
is Empty");
}
while(temp!=NULL)
{
printf("->
%d ",temp->data);
temp=temp->next;
}
}
int main()
{
int value, i, loc;
head=NULL;
printf("Select
the choice of operation on link list");
printf("\n1.)
Insert at beginning\n2.) Insert at last\n3.) Insert at middle");
printf("\n4.)
Delete from end\n5.) Reverse the link list\n6.) Display list\n7.)exit");
while(1)
{
printf("\n\nEnter
the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("Enter
the value you want to insert in node ");
scanf("%d",&value);
insert_begning(value);
display();
break;
}
case 2:
{
printf("Enter
the value you want to insert in node at last ");
scanf("%d",&value);
insert_end(value);
display();
break;
}
case 3:
{
printf("After
which data you want to insert data ");
scanf("%d",&loc);
printf("Enter
the data you want to insert in list ");
scanf("%d",&value);
insert_after(value,loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
printf("Enter
the value you want to delete");
scanf("%d",value);
delete_from_middle(value);
display();
break;
}
case 6 :
{
display();
break;
}
case 7 :
{
exit(0);
break;
}
}
}
printf("\n\n%d",last->data);
display();
getch();
}
Output:
PROGRAM9(a)-: -PROGRAM for finding a Factorial of a given
number with recursion.
#include<stdio.h>
longintmultiplyNumbers(int n);
intmain()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n,multiplyNumbers(n));
return0;
}
longintmultiplyNumbers(int n)
{
if(n >=1)
return n*multiplyNumbers(n-1);
else
return1;
}
Output
Enter a positive integer: 4
Factorial of 4 = 24
PROGRAM 9(b)-: -PROGRAM for finding a Factorial of a given
number without recursion.
#include <stdio.h>
main()
{
int n, i;
long fact=1;
printf(" Enter any number: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
fact = fact*i;
printf(" Factorial = %ld", fact);
getch();
}
Output
Enter a positive integer: 5
Factorial of 5 = 120
PROGRAM NO.-10-: -PROGRAM for a Fibonacci Series with
recursion.
#include<stdio.h>
int f(int);
main()
{
int n, i = 0, c;
printf("Enter any value for Fibonacci Series\n");
scanf("%d",&n);
printf("Fibonacci series:\n");
for ( c = 1 ; c <= n
; c++ )
{
printf("%d\n", f(i));
i++;
}
return 0;
}
int f(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( f(n-1) +
f(n-2) );
}
Output
PROGRAM NO.-11-: -PROGRAM for a Binary Search tree
Operation(insert).
#include<stdio.h>
#include<stdlib.h>
structnode
{
intkey;
structnode *left, *right;
};
// A utility function to create a new BST node
structnode *newNode(intitem)
{
structnode *temp =
(structnode *)malloc(sizeof(structnode));
temp->key = item;
temp->left = temp->right =
NULL;
returntemp;
}
// A utility function to do inorder traversal of BST
voidinorder(structnode *root)
{
if(root != NULL)
{
inorder(root->left);
printf("%d
\n", root->key);
inorder(root->right);
}
}
/* A utility function to insert a new node with given key
in BST */
structnode* insert(structnode* node, intkey)
{
/* If the tree is empty, return a
new node */
if(node == NULL)
returnnewNode(key);
/* Otherwise, recur down the tree
*/
if(key < node->key)
node->left
= insert(node->left, key);
elseif(key > node->key)
node->right
= insert(node->right, key);
/* return the (unchanged) node
pointer */
returnnode;
}
// Driver PROGRAM to test above functions
intmain()
{
/* Let us create following BST
50
/
\
30
70
/
\ / \
20
40 60 80 */
structnode *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// print inoder traversal of the
BST
inorder(root);
return0;
}
|
Run on IDE
Output:
20
30
40
50
60
70
80
PROGRAM NO.-12: -PROGRAM for Selection Sort technique.
#include <stdio.h>
intmain()
{
intarray[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for( c=0; c < n ;c++)
scanf("%d",&array[c]);
for( c=0; c <( n -1);c++)
{
position = c;
for( d= c +1; d < n ; d++)
{
if( array[position]> array[d])
position = d;
}
if( position!= c )
{
swap = array[c];
array[c]= array[position];
array[position]= swap;
}
}
printf("Sorted list in ascending order:\n");
for( c=0; c < n ;c++)
printf("%d\n", array[c]);
return0;
}
OUTPUT: -
PROGRAM NO.-13-: -PROGRAM for bubble sort technique.
#include <stdio.h>
intmain()
{
intarray[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c =0; c < n;c++)
scanf("%d",&array[c]);
for(c =0; c <( n -1);c++)
{
for(d =0; d < n - c -1; d++)
{
if(array[d]> array[d+1])/* For decreasing order use < */
{
swap = array[d];
array[d]= array[d+1];
array[d+1]= swap;
}
}
}
printf("Sorted list in ascending order:\n");
for( c=0; c < n ;c++)
printf("%d\n", array[c]);
return0;
}
Output of PROGRAM:
PROGRAM NO.-14-: -PROGRAM for Quick Sort technique.
#include<stdio.h>
voidquicksort(int number[25],intfirst,intlast){
inti, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
intmain(){
inti, count,number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return0;
}
Output:
PROGRAM NO.-15-: -PROGRAM for Merge Sort technique using recursion.
#include<stdio.h>
voidmerge(array, low, mid, high )
{
inttemp[MAX];
inti= low;
int j = mid +1;
int k =low ;
while((i<= mid)&&(j <=high))
{
if(array[i]<=array[j])
temp[k++]=array[i++];
else
temp[k++]=array[j++];
}/*End of while*/
while(i<= mid )
temp[k++]=array[i++];
while( j<= high )
temp[k++]=array[j++];
for(i= low;i<= high ;i++)
array[i]=temp[i];
}/*End of merge()*/
voidmerge_sort(int low,int high )
{
int mid;
if( low!= high )
{
mid =(low+high)/2;
merge_sort( low, mid );
merge_sort( mid+1, high );
merge( low, mid, high );
}
}/*End of merge_sort*/
intmain()
{
inti,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&array[i]);
}
printf("Unsorted list is :\n");
for(i=0;i<n ;i++)
printf("%d",array[i]);
merge_sort(0, n-1);
printf("\nSorted list is :\n");
for(i=0;i<n ;i++)
printf("%d",array[i]);
printf("\n");
return0;
}/*End of main()*/
OUTPUT: -
PROGRAM NO.-16-: - PROGRAM for a Binary Search to search an
element in a given list.
#include <stdio.h>
intmain()
{
int c, first, last, middle, n, search,array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c =0; c < n;c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first =0;
last = n -1;
middle =(first+last)/2;
while(first <= last){
if(array[middle]< search)
first = middle +1;
elseif(array[middle]== search){
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle -1;
middle =(first + last)/2;
}
if(first > last)
printf("Not found! %d isn't present in the list.\n", search);
return0;
}
Output
of PROGRAM:
PROGRAM NO.-17-: -PROGRAM for a Linear Search to search an
element in a given list.
#include <stdio.h>
intmain()
{
intarray[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for(c =0; c < n;c++)
scanf("%d",&array[c]);
printf("Enter a number to search\n");
scanf("%d",&search);
for(c =0; c < n;c++)
{
if(array[c]==search)/* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if(c == n)
printf("%d isn't present in the array.\n", search);
return0;
}
Output of PROGRAM:
0 Comments