Jaipur National University: MCA - III Semester Lab Assignment
Jaipur National University: MCA - III Semester Lab Assignment
UNIVERSITY
JAGATPURA, JAIPUR
SADTM CAMPUS
ANS>>
#include<stdio.h>
#include<conio.h>
void TowerOfHanoi(int n, char x,char y,char z)
{
if(n==1)
{
printf("\n\n Move %c to %c",x,z);
return ;
}
TowerOfHanoi(n-1,x,z,y);
printf("\n\n Move %c to %c",x,z);
TowerOfHanoi(n-1,y,x,z);
}
void main()
{
int n;
char x='X',y='Y',z='Z';
clrscr();
printf("\n\n Enter no of disks");
scanf("%d",&n);
TowerOfHanoi(n,x,y,z);
getch();
}
Output:
Enter no of disks:3
Move A to C
Move A to B
Move C to B
Move A to C
Move B to A
Move B to C
Move A to C
Q 2. C program for find the MIN and MAX element in array
using divide and conquer method?
ANS>>
#include<stdio.h>
#include<conio.h>
int a[100],max=100,min=100;
void minmax(int,int);
void main()
{
int n,i;
clrscr();
printf("\nEnter the size of list : ");
scanf("%d",&n);
printf("\nEnter %d elements in the list : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
minmax(0,n-1);
printf("\nList element's are :- ");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\n\nMaximum element is %d : Minimum element is %d ",max,min);
getch();
}
void minmax(int i,int n)
{
int mid,max1,min1;
if(i==n)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
else
{
mid=(i+n)/2;
minmax(i,mid);
minmax(mid+1,n);
}
}
Output:
8
4
7
1
3
maximum=8
minimum=1
ANS>>
#include<conio.h>
#include<stdio.h>
void perm(char arr[],int,int);
void main()
{
char arr[]={'a','b','c'};
clrscr();
perm(arr,0,2);
getch();
}
void perm(char arr[],int k,int n)
{
char temp;
int i;
if(k==n)
{
for(i=0;i<=n;i++)
printf("%c",arr[i]);
printf("\n");
}
else
{
for(i=k;i<=n;i++)
{
temp=arr[k];
arr[k]=arr[i];
arr[i]=temp;
perm(arr,k+1,n);
temp=arr[k];
arr[k]=arr[i];
arr[i]=temp;
}
}
}
Output:
abc
acb
bac
bca
cba
cab
ANS>>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
void heapsort(int a[],int );
void adjust(int heap[],int ,int );
void heapfy(int a[],int );
void main()
{
int i,n,a[MAX];
clrscr();
printf("\nEnter size of array::");
scanf("%d",&n);
if(n>MAX)
{
printf("\nInput size of array greater than declared size\n");
exit(1);
}
printf("\n enter elements in the array:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n\nSorted list of elemets\n\n");
for(i=1;i<=n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
getch();
}
void heapsort(int a[],int n)
{
int i,temp;
heapfy(a,n);
for(i=n;i>1;i--)
{
temp=a[1];
a[1]=a[i];
a[i]=temp;
adjust(a,1,i-1);
}
}
void adjust(int a[],int start,int finish)
{
int index,lchild,rchild,maximum,temp;
lchild = 2 * start;
rchild = lchild + 1;
if(lchild<=finish)
{
maximum=a[lchild];
index=lchild;
if(rchild<=finish)
{
if(a[rchild]>maximum)
{
maximum=a[rchild];
index=rchild;
}
}
if(a[start]<a[index])
{
temp=a[start];
a[start]=a[index];
a[index]=temp;
adjust(a,index,finish);
}
}
}
void heapfy(int a[],int n)
{
int i,index;
index = n/2;
for(i=index;i>=1;i--)
adjust(a,i,n);
}
Output:
3 4 7 8 9
ANS>>
#include<conio.h>
#include<stdio.h>
void merge_sort(int a[],int b,int e);
void merge(int a[],int lb,int le,int rb,int re);
int lb,le,rb,re,n;
void main()
{
int a[20],i,b,e;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
printf("Enter the arrays/n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
merge_sort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void merge_sort(int a[],int b,int e)
{
int m,i;
if(b<e)
{
m=(b+e)/2;
merge_sort(a,b,m);
merge_sort(a,m+1,e);
merge(a,b,m,m+1,e);
}
}
void merge(int a[],int lb,int le,int rb,int re)
{
int n1,n2,n3,i,j,k;
int c[20];
n1=lb;
n2=rb;
n3=lb;
while((n1<=le) && (n2<=re))
{
if(a[n1]<a[n2])
c[n3]=a[n1++];
else
c[n3]=a[n2++];
n3++;
}
if(n1>le)
{
while(n2<=re)
c[n3++]=a[n2++];
}
else
{
while(n1<=le)
c[n3++]=a[n1++];
}
for(k=lb;k<=re;k++)
a[k]=c[k];
}
Output:
1
2
3
5
7
ANS >>
#include<stdio.h>
#include<conio.h>
int a[size][size];
int m[size][size];
int i,j,k;
int n;
void println()
{
printf("\n=========================================================
==================\n");
}
void input()
{
printf("\n\t\t\t\tInput Data");
println();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("Element %d%d : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
println();
}
void shortest()
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][j]==0)
m[i][j]=0;
else
m[i][j]=a[i][j];
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(m[i][j] <= (m[i][k] + m[k][j]))
{
m[i][j] = m[i][j];
}
else
{
m[i][j] = m[i][k] + m[k][j];
}
}
}
}
}
void output()
{
printf("\nThe shortest path matrix by implementing Warshall algorithm is...\n");
println();
printf("\n");
printf("\t\t");
for(i=0;i<n;i++)
printf("%d\t" , i+1);
printf("\n----------|-----------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\n\t%d |",i+1);
for(j=0;j<n;j++)
{
printf("\t%d",m[i][j]);
}
printf("\n");
}
printf("\n----------|-----------------------------------------------------\n");
}
void main()
{
clrscr();
println();
printf("\t\t\tShortest Path Warshall algorithm");
println();
printf("\n");
input();
shortest();
output();
println();
getch();
}
OUTPUT >>