MCA 4th Sem ADA Lab Mannual

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 26
At a glance
Powered by AI
The document discusses various sorting and searching algorithms like binary search, linear search, heap sort, merge sort, selection sort, prim's algorithm and floyd's algorithm.

The document discusses binary search, linear search, heap sort, merge sort, selection sort, prim's algorithm, floyd's algorithm and warshall's algorithm.

Binary search is a faster search algorithm that works by repeatedly dividing in half the range of possible values to check where the target value could exist, based on key comparisons.

MCE HASSAN

1)BINARY AND LINEAR SEARCH

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
int a[10000],n,pos,i,h,key,flag=0,l;
long m;
int bin(int a[],int l,int h,int key)
{
int mid;
if(l>h)
return -1;
mid=(h+l)/2;
if(a[mid]==key)
return mid;
else if(a[mid]>key)
return bin(a,l,mid-1,key);
else
return bin(a,mid+1,h,key);
}
int lin(int a[],int pos,int n,int key)
{
if(pos==n)
{
if(key==a[pos])
return pos;
else
return -1;
}
else if(a[pos]==key)
return pos;
else
return lin(a,pos+1,n,key);
}
void main()
{
int ch;
clock_t t1,t2;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)

ADA LAB PROGRAMS,MCA (VTU 2009) 1


MCE HASSAN

a[i]=rand();
printf("\n The elements considered are:\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
do
{
printf("\n Enter the key to be searched:");
scanf("%d",&key);
printf("\n1.Linear search \n2.Binary search \n3.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:t1=clock();
for(m=0;m<=pow(10,6);m++)
{
flag=lin(a,1,n,key);
t2=clock();
}
printf("\n Time taken=%fE-6",(t2-t1)/CLK_TCK);
break;
case 2:t1=clock();
l=1;
h=n;
for(m=0;m<=pow(10,6);m++)
{
flag=bin(a,l,n,key);
t2=clock();
}
printf("\n Time taken=%fE-6",(t2-t1)/CLK_TCK);
break;
case 3:exit(0);
default:printf("\n Invalid choice");
break;
}
if(flag==-1)
printf("\n Not found");
else
printf("\n %d found at position %d",key,pos+1);
}while(ch!=3);
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 2


MCE HASSAN

2) HEAP SORT

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
int a[2500];
void swap(int x,int y)
{
int temp;
temp=a[x];
a[x]=a[y];
a[y]=temp;
}
void constructors(int n)
{
int i,j,k,h;
for(k=n/2;k>0;k--)
{
i=k;
h=0;
while(h==0 && (2*i)<=n)
{
j=(2*i);
if(j+1<=n)
{
if(a[j+1]>a[j])
j++;
}
if(a[i]>a[j])
h=1;
else
{
swap(i,j);
i=j;
}
}
}
}

ADA LAB PROGRAMS,MCA (VTU 2009) 3


MCE HASSAN

void heapsort(int n)
{
int i;
constructors(n);
for(i=n;i>0;i--)
{
swap(1,i);
constructors(i-1);
}
}
void main()
{
int i,n;
long m;
clock_t t1,t2;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
a[i]=rand();
printf("\n The elements considered are:\n");
for(i=1;i<=n;i++)
printf("%d \t",a[i]);
t1=clock();
for(m=0;m<=pow(10,6);m++)
heapsort(n);
t2=clock();
printf("\n Sorted array:\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
printf("\n Time taken=%fE-6",(t2-t1)/CLK_TCK);
getch();
}

3) MERGE SORT

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
int a[2500];
void merge(int low,int mid,int high)
{
int i=low,l=low,j=mid+1,b[2500],k;

ADA LAB PROGRAMS,MCA (VTU 2009) 4


MCE HASSAN

while((l<=mid) && (j<=high))


{
if(a[l]<=a[j])
{
b[i]=a[l];
l++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(l>mid)
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
else
for(k=l;k<=mid;k++)
{
b[i]=a[k];
i++;
}
for(k=low;k<=high;k++)
a[k]=b[k];
}
void mergesort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
void main()
{
int n,i;
long m;

ADA LAB PROGRAMS,MCA (VTU 2009) 5


MCE HASSAN

clock_t t1,t2;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
a[i]=rand();
printf("\n The elements considered are:\n");
for(i=1;i<=n;i++)
printf("%d \t",a[i]);
t1=clock();
for(m=0;m<=pow(10,6);m++)
mergesort(1,n);
t2=clock();
printf("\n Sorted array elements are:\n");
for(i=1;i<=n;i++)
printf("%d \t",a[i]);
printf("\n Time taken=%fE-6",(t2-t1)/CLK_TCK);
getch();
}
4) SELECTION SORT

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
int select(int a[],int lb,int ub,int k)
{
int pivot=a[lb],down=lb,up=ub+1,temp;
if(lb>=ub)
return a[lb];
while(down<up)
{
do
{
down++;
}while(pivot>a[down]);
do
{
up--;
}while(pivot<a[up]);
if(down<up)
{
temp=a[down];
a[down]=a[up];

ADA LAB PROGRAMS,MCA (VTU 2009) 6


MCE HASSAN

a[up]=temp;
}
}
if((up-lb+1)==k)
return pivot;
a[lb]=a[up];
a[up]=pivot;
if((up-lb+1)<k)
return select(a,up+1,ub,k-up+lb-1);
else
return select(a,lb,up-1,k);
}
void main()
{
int n,i,a[2500],b[2500];
long m;
clock_t t1,t2;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
a[i]=rand();
printf("\n Selectd elements:\n");
for(i=1;i<=n;i++)
printf("%d \t",a[i]);
t1=clock();
for(m=0;m<=pow(10,6);m++)
{
for(i=1;i<=n;i++)
b[i]=select(a,1,n,i);
}
t2=clock();
printf("\n The sorted elements are:\n");
for(i=1;i<=n;i++)
printf("%d \t",b[i]);
printf("\n Time taken=%fE-6",(t2-t1)/CLK_TCK);
getch();
}
5) KNAPSACK PROBLEM

#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)

ADA LAB PROGRAMS,MCA (VTU 2009) 7


MCE HASSAN

{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("for item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("enter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;

ADA LAB PROGRAMS,MCA (VTU 2009) 8


MCE HASSAN

j=j-w[i];
i--;
}
else
i--;
}
printf("items included are\n");
printf("sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
printf("total profit = %d\n",profit);
getch();
}

6) DIJKSTRA’S ALGORITHM

#include<stdio.h>
#include<conio.h>
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");

ADA LAB PROGRAMS,MCA (VTU 2009) 9


MCE HASSAN

scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}

7) QUICK SORT

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
int a[2000];
void partition(int a[],int,int,int *);
void qsort(int a[],int lb,int ub)
{
int j;
if(lb<ub)
{
partition(a,lb,ub,&j);
qsort(a,lb,j);
qsort(a,j+1,ub);
}
}
void partition(int a[],int lb,int ub,int *pj)
{
int key=a[lb],down=lb,up=ub,temp;
while(key>=a[down] && down<ub)
down++;
while(key<a[up])
up--;

ADA LAB PROGRAMS,MCA (VTU 2009) 10


MCE HASSAN

if(down<up)
{
temp=a[down];
a[down]=a[up];
a[up]=temp;
}
a[lb]=a[up];
a[up]=key;
*pj=up;
}
void main()
{
int i,n;
long m;
clock_t t1,t2;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
a[i]=rand();
printf("\n The elements considered are:\n");
for(i=1;i<=n;i++)
printf("%d \t",a[i]);
t1=clock();
for(m=0;m<=pow(10,6);m++)
qsort(a,1,n);
t2=clock();
printf("\n The sorted array is:\n");
for(i=1;i<=n;i++)
printf("%d \t",a[i]);
printf("\n Time taken=%fE-6",(t2-t1)/CLK_TCK);
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 11


MCE HASSAN

8) KRUSKAL ALGORITHM

#include<stdio.h>
#include<conio.h>
#define MAX 20
int root[MAX],n=0;
struct edge
{
int u,v,w;
};
int finds(int x)
{
return(root[x]);
}
void unions(int x,int y)
{
int i;
if(x<y)
{
for(i=1;i<=n;i++)
if(root[i]==y)
root[i]=x;
}
else
{
for(i=1;i<=n;i++)
if(root[i]==x)
root[i]=y;
}
}
void main()
{
int ed=0,i,j,k=0,a,b,weight=0;
struct edge e[MAX],temp;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
root[i]=i;
printf("\n Enter the number of edges:");
scanf("%d",&ed);
for(i=1;i<=ed;i++)
{
printf("\n Enter the end vertices & cost of edge %d:",i);

ADA LAB PROGRAMS,MCA (VTU 2009) 12


MCE HASSAN

scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
}
for(i=1;i<=ed;i++)
for(j=1;j<=ed;j++)
if(e[j].w>e[j+1].w)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
printf("\n The weights in ascending order are:\n");
for(i=1;i<=ed;i++)
printf("%d \t",e[i].w);
printf("\n");
i=1;
while(ed && k!=n-1)
{
a=finds(e[i].u);
b=finds(e[i].v);
if(a!=b)
{
printf("<%d,%d>",e[i].u,e[i].v);
unions(a,b);
weight+=e[i].w;
k++;
}
ed--;
i++;
}
if(k<n-1)
printf("\n No network exists");
else
printf("\n Network exists \n The weight of tree is %d",weight);
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 13


MCE HASSAN

9A) BFS METHOD

#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 14


MCE HASSAN

9B) DFS METHOD

#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("\n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();

ADA LAB PROGRAMS,MCA (VTU 2009) 15


MCE HASSAN

ADA LAB PROGRAMS,MCA (VTU 2009) 16


MCE HASSAN

10) SUBSET

#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
void sumset(int i,int wt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("\n{\t");
for(j=0;j<=i;j++)
if(inc[j])
printf("%d\t",w[j]);
printf("}\n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
void main()
{
int i,j,n,temp,total=0;
clrscr();
printf("\n Enter how many numbers:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
total+=w[i];

ADA LAB PROGRAMS,MCA (VTU 2009) 17


MCE HASSAN

}
printf("\n Input the sum value:");
scanf("%d",&sum);
for(i=0;i<=n;i++)
for(j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("\n The given %d numbers in ascending order:\n",n);
for(i=0;i<n;i++)
printf("%d \t",w[i]);
if((total<sum))
printf("\n Subset construction is not possible");
else
{
for(i=0;i<n;i++)
inc[i]=0;
printf("\n The solution using backtracking is:\n");
sumset(-1,0,total);
}
getch();
}

11a) HORSPOOL ALGORITHM

#include<stdio.h>
#include<string.h>
#include<conio.h>
#define MAX 500
int t[MAX];
void shifttable(char p[])
{
int i,j,m;
m=strlen(p);
for(i=0;i<MAX;i++)
t[i]=m;
for(j=0;j<m-1;j++)
t[p[j]]=m-1-j;
}
int horspool(char src[],char p[])

ADA LAB PROGRAMS,MCA (VTU 2009) 18


MCE HASSAN

{
int i,j,k,m,n;
n=strlen(src);
m=strlen(p);
printf("\nLength of text=%d",n);
printf("\n Length of pattern=%d",m);
i=m-1;
while(i<n)
{
k=0;
while((k<m)&&(p[m-1-k]==src[i-k]))
k++;
if(k==m)
return(i-m+1);
else
i+=t[src[i]];
}
return -1;
}
void main()
{
char src[100],p[100];
int pos;
clrscr();
printf("Enter the text in which pattern is to be searched:\n");
gets(src);
printf("Enter the pattern to be searched:\n");
gets(p);
shifttable(p);
pos=horspool(src,p);
if(pos>=0)
printf("\n The desired pattern was found starting from position
%d",pos+1);
else
printf("\n The pattern was not found in the given text\n");
getch();
}

11b) BINOMIAL CO-EFFICIENT USING DYNAMIC


PROGRAMMING

#include<stdio.h>

ADA LAB PROGRAMS,MCA (VTU 2009) 19


MCE HASSAN

#include<conio.h>
void main()
{
int i,j,n,k,min,c[20][20]={0};
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the value of k:");
scanf("%d",&k);
if(n>=k)
{
for(i=0;i<=n;i++)
{
min=(i<k)?i:k;
for(j=0;j<=min;j++)
if(j==k || j==0)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(i>=j)
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("\n Invalid input \n Enter value n>=k \n");
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 20


MCE HASSAN

12) PRIM’S ALGORITHM

#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 21


MCE HASSAN

13a) FLOYD’S ALGORITHM

#include<stdio.h>
#include<conio.h>
int min(int,int);
void floyds(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b)
{
if(a<b)
return(a);
else
return(b);
}
void main()
{
int p[10][10],w,n,e,u,v,i,j;;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:\n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("\n Matrix of input data:\n");
for(i=1;i<=n;i++)

ADA LAB PROGRAMS,MCA (VTU 2009) 22


MCE HASSAN

{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\n Transitive closure:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
printf("\n The shortest paths are:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
getch();
}
13b) WARSHALL’S PROBLEM

#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b)
{ ;
if(a>b)
return(a);
else
return(b);
}
void main()

ADA LAB PROGRAMS,MCA (VTU 2009) 23


MCE HASSAN

{
int p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}

14) N QUEENS PROBLEM


#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}

ADA LAB PROGRAMS,MCA (VTU 2009) 24


MCE HASSAN

return 1;
}
void print_sol(int n)
{
int i,j;
count++;
printf("\n\nSolution #%d:\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main()
{

ADA LAB PROGRAMS,MCA (VTU 2009) 25


MCE HASSAN

int i,n;
clrscr();
printf("Enter the number of Queens\n");
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}

ADA LAB PROGRAMS,MCA (VTU 2009) 26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy