search an element.repeat the experiment for …mubarakpasha.yolasite.com/resources/ada_lab...

Post on 24-Mar-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PROGRAM 1

Implement recursive Binary and linear search and determine the time required to

search an element.Repeat the experiment for different values of n,the number of

elements in the list to be searched and plot a graph of the time taken versus n.

#include<stdio.h> #include<conio.h> #include<process.h> #include<time.h> #include<dos.h> clock_t start,end; int linear(int a[],int,int); int binary(int a[],int,int,int); void main() { int a[10],n,i,key,pos,choice; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); printf("\nEnter the array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the key element"); scanf("%d",&key); printf("1:linear\n2:Binary\n"); printf("\nEnter your choice:"); scanf("%d",&choice); if(choice==1) { start=clock(); delay(1000); pos=linear(a,n-1,key); end=clock(); } else if(choice==2) { start=clock(); delay(1000); pos=binary(a,0,n-1,key); end=clock(); } else { printf("\nInvalid choice");

getch(); exit(0); } if(pos!=-1) printf("\nSuccessful search,element found at pos %d",pos+1); else printf("\nUnsuccessful search"); printf("\nTime taken=%f",(end-start)/CLK_TCK); getch(); } int linear(int a[],int n,int key) { if(n==-1) return -1; if(key==a[n]) return n; else return linear(a,n-1,key); } int binary(int a[],int low,int high,int key) { int mid; if(low>high) return -1; else mid=(high+low)/2; if(a[mid]==key) return mid; else if(key<a[mid]) binary(a,low,mid-1,key); else binary(a,mid+1,high,key); }

OUTPUT:

Enter the number of elements: 7 Enter the array elements: 52 12 8 77 32 90 5 Enter the key element 90 1:linear 2:Binary Enter your choice:1 Successful search,element found at pos 6 Time taken=0.989011 Enter the number of elements:6 Enter the array elements:8 12 39 48 51 78 Enter the key element51 1:linear 2:Binary Enter your choice:2 Successful search,element found at pos 5 Time taken=0.989011

PROGRAM 2

Sort a given set of elements using the Heap sort method and determine the time required to sort the elements. Repeat the experiment for different values of n,the number of elements in the list to be sorted and plot a graph of the time taken versus n

#include<stdio.h> #include<conio.h> #include<time.h> #include<dos.h> clock_t start,end; void heap_set(int a[],int n) { int key,c,k,i; for(i=1;i<n;i++) { key=a[i]; k=i; c=(k-1)/2; while(k>0 && a[c]<key) { a[k]=a[c]; k=c; c=(k-1)/2; } a[k]=key; } } void heap_form(int a[],int n,int k) { int key ,c; key=a[k]; c=(2*k)+1; while(c<=n-1) { if(c+1<n-1) if(a[c+1]>a[c]) c++; if(a[c]>key) { a[k]=a[c]; k=c; c=(2*k)+1; }

else break; } a[k]=key; } void heap_sort(int a[],int n) { int i,temp; for(i=n-1;i>=0;i--) { temp=a[0]; a[0]=a[i]; a[i]=temp; heap_form(a,i,0); } } void main() { int i,n,a[20]; clrscr(); printf("Enter the number of elements: "); scanf("%d",&n); printf("\n\nEnter elements: \n"); printf("-----------------\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); start=clock(); delay(1000); heap_set(a,n); heap_sort(a,n); end=clock(); printf("\nThe sorted elements are: \n"); printf("-------------------------\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); printf("\nTime taken=%f",(end-start)/CLK_TCK); getch(); }

OUTPUT:

Enter the number of elements: 5 Enter elements: ----------------- 7 6 5 4 3 The sorted elements are: ------------------------- 3 4 5 6 7 Time taken=1.043956

PROGRAM 3

Sort a given set of elements using Merge sort method and determine the time required to sort the elements.Repeat the experiment for different values of n,the number of elements in the list to be sorted and plot a graph of the time taken versus n.

#include<stdio.h> #include<conio.h> #include<time.h> #include<dos.h> clock_t start,end; int i,j,k,a[20],temp[20]; void merge(int,int,int); 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 merge(int low,int mid,int high) { i=low; k=low; j=mid+1; while(i<=mid && j<= high) { if(a[i]<a[j]) { temp[k]=a[i]; k++; i++; } else { temp[k]=a[j]; k++; j++; } } while(i<=mid) {

temp[k]=a[i]; k++; i++; } while(j<=high) { temp[k]=a[j]; k++; j++; } for(i=low;i<=high;i++) { a[i]=temp[i]; } } void main() { int n; clrscr(); printf("\nEnter the number of elements"); scanf("%d",&n); printf("\nEnter the array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); start=clock(); delay(1000); mergesort(0,n-1); end=clock(); printf("\nThe sorted array is:"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } printf("\nTime taken=%f",(end-start)/CLK_TCK); getch(); }

OUTPUT:

Enter the number of elements 6 Enter the array elements 3 6 12 32 22 11 The sorted array is:3 6 11 12 22 32 Time taken=0.989011 Enter the number of elements 10 Enter the array elements 6 56 12 90 4 22 12 87 65 34 The sorted array is:4 6 12 12 22 34 56 65 87 90 Time taken=1.043956

PROGRAM 4

Sort a given set of elements using Selection sort and determine the time required to sort elements.Repeat the experiment for different values of n,the number of elements in the list to be sorted and plot a graph of the time taken versus n.

#include<stdio.h> #include<conio.h> #include<time.h> #include<dos.h> clock_t start,end; void main() { int i,j,n,a[20],small,pos,temp; clrscr(); printf("\nEnter the number of elements"); scanf("%d",&n); printf("\nEnter the array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); start=clock(); delay(1000); for(i=0;i<=n-2;i++) { small=a[i]; pos=i; for(j=i+1;j<=n-1;j++) { if(a[j]<small) { small=a[j]; pos=j; } } temp=a[i]; a[i]=a[pos]; a[pos]=temp; } end=clock(); printf("\nThe sorted array is"); for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\nThe time taken=%f",(end-start)/CLK_TCK); getch(); }

OUTPUT:

Enter the number of elements 7 Enter the array elements 90 23 78 12 55 22 11 The sorted array is11 12 22 23 55 78 90 The time taken=0.989011 Enter the number of elements10 Enter the array elements 78 11 56 22 87 45 1 32 21 52 The sorted array is1 11 21 22 32 45 52 56 78 87 The time taken=0.934066

PROGRAM 5

Implement 0/1 Knapsack problem using dynamic programming.

#include<stdio.h> #include<conio.h> int max(int i,int j) { if(i>j) return i; return j; } int m,n,v[10][10],w[10],p[10],x[10]; void main() { int i,j; clrscr(); printf("\nEnter the number of objects\n"); scanf("%d",&n); printf("\nEnter the capacity of thr Knapsack:\n"); scanf("%d",&m); for(i=1;i<=n;i++) { printf("\nEnter the weight and profit of %d object:\n",i); scanf("%d%d",&w[i],&p[i]); } for(i=0;i<=n;i++) { for(j=0;j<=m;j++) { if(i==0 || j==0) v[i][j]=0; else if (w[i]>j) v[i][j]=v[i-1][j]; else v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]); } } for(i=0;i<=n;i++) { for(j=0;j<=m;j++) { printf("%d\t",v[i][j]); } printf("\n");

} for(i=0;i<=n;i++) x[i]=0; i=n; j=m; while(i!=0 && j!=0) { if(v[i][j]!=v[i-1][j]) { x[i]=1; j=j-w[i]; } i--; } printf("\nThe objects selected are :\n"); for(i=1;i<=n;i++) { if(x[i]==1) printf("%d\t",i); } getch(); }

OUTPUT:

Enter the number of objects 4 Enter the capacity of thr Knapsack: 5 Enter the weight and profit of 1 object: 2 12 Enter the weight and profit of 2 object: 1 10 Enter the weight and profit of 3 object: 3 20 Enter the weight and profit of 4 object: 2 15 0 0 0 0 0 0 0 0 12 12 12 12 0 10 12 22 22 22 0 10 12 22 30 32 0 10 15 25 30 37 The objects selected are : 1 2 4

PROGRAM 6

From a given vertex in a weighted connected graph,find shortest paths to other vertices using Dijkstra’s algorithm.

#include<stdio.h> #include<conio.h> int n,cost[10][10],src,dis[10],vis[10],min,u; void dijkstra() { int i,j; for(i=1;i<=n;i++) { dis[i]=cost[src][i]; } vis[src]=1; for(i=1;i<=n-1;i++) { min=999; for(j=1;j<=n;j++) { if(vis[j]==0 && (dis[j]<min)) { min=dis[j]; u=j; } } vis[u]=1; for(j=1;j<=n;j++) { if(vis[j]==0 &&((dis[u]+cost[u][j])<dis[j])) { dis[j]=(dis[u]+cost[u][j]); } } } for(i=1;i<=n;i++) { printf("%d->%d=",src,i); printf("%d\t",dis[i]); }

} void main() { clrscr(); int i,j; printf("\nEnter the number of vertices"); scanf("%d",&n); printf("\nEnter the source vertex"); scanf("%d",&src); printf("\nEnter the cost adjacency matrix"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&cost[i][j]); } vis[i]=0; } printf("\nSingle src shortest path solution is"); dijkstra(); getch(); } OUTPUT:

Enter the number of vertices 5 Enter the source vertex 1 Enter the cost adjacency matrix 0 3 999 7 999 3 0 4 2 999 999 4 0 5 6 7 2 5 0 4 999 999 6 4 0 Single src shortest path solution is1->1=0 1->2=3 1->3=7 1->4=5 1->5=9

PROGRAM 7

Sort a given set of elements using Quick sort method and determine the time required to sort the elements.Repeat the experiment for different values of n,the number of elements in the list to be sorted and plot a graph of the time taken versus n.

#include<stdio.h> #include<conio.h> #include<time.h> #include<dos.h> clock_t start,end; int partition(int low,int high,int a[]) { int i,j,key,temp; i=low; j=high+1; key=a[low]; while(i<=j) { do i++; while(key>=a[i]); do j--; while(key<a[j]); if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[low]; a[low]=a[j]; a[j]=temp; return j; } void quick_sort(int low,int high,int a[]) { int mid; if(low<high) { mid=partition(low,high,a); quick_sort(low,mid-1,a); quick_sort(mid+1,high,a); } }

void main() { int n,i,a[20]; clrscr(); printf("\nEnter the number of elements"); scanf("%d",&n); printf("\nEnter the array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); start=clock(); delay(1000); quick_sort(0,n-1,a); end=clock(); printf("\nThe sorted array is"); for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\nThe time taken =%f",(end-start)/CLK_TCK); getch(); } OUTPUT:

Enter the number of elements 6 Enter the array elements 90 12 4 67 33 66 The sorted array is4 12 33 66 67 90 The time taken =0.989011 Enter the number of elements 9 Enter the array elements 45 12 5 2 96 56 32 21 18 The sorted array is2 5 12 18 21 32 45 56 96 The time taken =0.989011

PROGRAM 8

Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal’s algorithm.

#include<stdio.h> #include<conio.h> int find(int v,int parent[]) { while(parent[v]!=v) v=parent[v]; return v; } void unions(int i,int j,int parent[]) { if(i<j) parent[j]=i; else parent[i]=j; } void kruskal(int n,int a[10][10]) { int count,k,min,sum,i,j,t[10][10],u,v,parent[10]; count=0; k=0; sum=0; for(i=0;i<n;i++) parent[i]=i; while(count!=n-1) { min=999; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]<min && a[i][j]!=0) { min=a[i][j]; u=i; v=j; } } } i=find(u,parent); j=find(v,parent);

if(i!=j) { t[k][0]=u; t[k][1]=v; k++; sum=sum+a[u][v]; count++; unions(i,j,parent); } a[u][v]=a[v][u]=999; } if(count==n-1) { printf("\nSpanning tree exists\n"); printf("Minimum Spanning tree edges\n"); for(i=0;i<n-1;i++) { printf("%d\t%d",t[i][0],t[i][1]); printf("\n"); } printf("Cost of the minimum spanning tree is %d",sum); } else printf("\nSpanning tree does not exist"); getch(); } void main() { int n,a[10][10],i,j; clrscr(); printf("\nEnter the number of nodes in the graph\n"); scanf("%d",&n); printf("\nEnter the cost adjacency matrix\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } kruskal(n,a); }

OUTPUT:

Enter the number of nodes in the graph 5 Enter the cost adjacency matrix 0 5 999 6 999 5 0 1 3 999 999 1 0 4 6 6 3 4 0 2 999 999 6 2 0 Spanning tree exists Minimum Spanning tree edges 1 2 3 4 1 3 0 1 Cost of the minimum spanning tree is 11

PROGRAM 9A

Print all the nodes reachable from a given starting node in a digraph using BFS method.

#include<stdio.h> #include<conio.h> int a[10][10],vis[10],n; void bfs(int); void main() { int i,j,source; clrscr(); printf("Enter the number of vertices\n"); scanf("%d",&n); printf("Enter the adjacency matrix"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } vis[i]=0; } printf("\nEnter the source vertex:"); scanf("%d",&source); bfs(source); getch(); } void bfs(int v) { int q[10],r=1,f=1,u,i; q[r]=v; vis[v]=1; while(f<=r) { u=q[f]; printf("%d",u); for(i=1;i<=n;i++) { if(a[u][i]==1 && vis[i]==0) { r++; q[r]=i;

vis[i]=1; } } f++; } } OUTPUT:

Enter the number of vertices 8 Enter the adjacency matrix 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 1 1 0 Enter the source vertex: 1 1 2 3 4 5 6 7 8 Enter the number of vertices 4 Enter the adjacency matrix 0 1 0 0 0 0 0 1 1 0 0 1 0 0 0 1 Enter the source vertex: 2 2 4

PROGRAM 9B

Check whether a given graph is connected or not using DFS method.

#include<stdio.h> #include<process.h> #include<conio.h> int a[10][10],vis[10],n; void dfs(int); void main() { int i,j,src; clrscr(); printf("\nEnter the number of vertices:"); scanf("%d",&n); printf("Enter the adjacency matrix"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } vis[i]=0; } for(i=1;i<=n;i++) { src=i; for(j=1;j<=n;j++) { vis[j]=0; } dfs(src); } for(j=1;j<=n;j++) { if(vis[j]==0) { printf("\nGraph is not connected"); getch(); exit(0); } } printf("\nGraph is connected"); getch();

} void dfs(int v) { int i; vis[v]=1; for(i=1;i<=n;i++) { if(a[v][i]==1 && vis[i]==0) dfs(i); } } OUTPUT:

Enter the number of vertices: 4 Enter the adjacency matrix 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 Graph is connected

PROGRAM 10

Find a subset of a given set S = {sl,s2,.....,sn} of n positive integers whose sum is equal to a given positive integer d. For example, if S= {1,2, 5, 6, 8} and d = 9 there are two solutions{1,2,6}and{1,8}.A suitable message is to be displayed if the given problem instance doesn't have a solution.

#include<stdio.h> #include<conio.h> #include<process.h> #define max 10 int s[max],x[max],d; void sumofsub(int,int,int); void main() { int i,n,sum=0; clrscr(); printf("\nenter the size of the set : "); scanf("%d",&n); printf("\nenter the elements of the set in increasing order \n"); for(i=1;i<=n;i++) scanf("%d",&s[i]); printf("\nenter the calue for d : "); scanf("%d",&d); for(i=1;i<=n;i++) sum=sum+s[i]; if(sum<d || s[1]>d) printf("\n !no subsets possible \n"); else sumofsub(0,1,sum); getch(); } void sumofsub(int m,int k,int r) { int i; x[k]=1; if((m+s[k])==d) { for(i=1;i<=k;i++) if(x[i]==1) printf("\t%d",s[i]); printf("\n"); } else

if(m+s[k]+s[k+1]<=d) sumofsub(m+s[k],k+1,r-s[k]); if((m+r-s[k]>=d) && (m+s[k+1]<=d)) { x[k]=0; sumofsub(m,k+1,r-s[k]); } } OUTPUT

enter the size of the set : 5 enter the elements of the set in increasing order 1 2 5 6 8 enter the Value for d : 9 1 2 6 1 8

PROGRAM 11A

Implement Horspool algorithm for String Matching.

#include<stdio.h> #include<conio.h> #include<string.h> char str[100],ptr[20]; int res,m,n,i,j,l,k,table[1000]; void shift(char p[]) { l=strlen(p); for(i=0;i<1000;i++) table[i]=l; for(j=0;j<=l-2;j++) table[p[j]]=l-1-j; } int hp(char p[],char t[]) { shift(p); m=strlen(p); n=strlen(t); i=m-1; while(i<=n-1) { k=0; while((k<=m-1)&& (p[m-1-k]==t[i-k])) k++; if(k==m) return (i-m+1); else i=i+table[t[i]]; } return -1; } void main() { clrscr(); printf("\n"); printf("\nEnter the text:"); gets(str); printf("\n Enter the pattern :"); gets(ptr); res=hp(ptr,str); if(res==-1)

printf("Not found"); else printf("Found at %d",res+1); getch(); } OUTPUT:

Enter the text:Lab manual Enter the pattern :man Found at 5

PROGRAM 11B

Find the binomial coefficient using Dynamic programming.

#include<stdio.h> #include<conio.h> int min(int i,int j) { if(i<j) return i; else return j; } void main() { int n,i,j,k,c[10][10]; clrscr(); printf("\nEnter the values of n and k"); scanf("%d%d",&n,&k); for(i=0;i<=n;i++) { for(j=0;j<=min(i,k);j++) { if(j==0 || j==i) c[i][j]=1; else c[i][j]=c[i-1][j-1]+c[i-1][j]; } } printf("\nThe value of the binomial coefficient is %d",c[n][k]); getch(); }

OUTPUT:

Enter the values of n and k 6 3 The value of the binomial coefficient is 20 Enter the values of n and k 7 3 The value of the binomial coefficient is 35

PROGRAM 12

Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm.

#include<stdio.h> #include<conio.h> void prims(); int cost[10][10],vt[10],et[10][10],vis[10],e=0,sum=0,n; void main() { int i,j; clrscr(); printf("\nEnter the number of vertices"); scanf("%d",&n); printf("\nEnter the cost adj matrix"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&cost[i][j]); } vis[i]=0; } prims(); printf("Edges of the spanning tree"); for(i=1;i<=e;i++) { printf("\n%d\t%d\n",et[i][0],et[i][1]); } printf("Total weight"); printf("%d",sum); getch(); } void prims() { int x,min,k,m,i,j,u,v; x=1; vt[x]=1; vis[x]=1; for(i=1;i<=n-1;i++) { j=x; min=999;

while(j>0) { k=vt[j]; for(m=2;m<=n;m++) { if(vis[m]==0) { if(cost[k][m]<min) { min=cost[k][m]; u=k; v=m; } } } j--; } vt[++x]=v; vis[v]=1; et[i][0]=u; et[i][1]=v; e++; sum=sum+cost[u][v]; } }

OUTPUT:

Enter the number of vertices 5 Enter the cost adj matrix 0 5 7 999 2 5 0 999 6 3 7 999 0 4 4 999 6 4 0 5 2 3 4 5 0 Edges of the spanning tree 1 5 5 2 5 3 3 4 Total weight 13

PROGRAM 13A

Implement Floyd’s algorithm for All-Pairs-Shortest-Paths problem.

#include<stdio.h> #include<conio.h> int min(int,int); void main() { int n,i,j,k,d[10][10],c[10][10]; clrscr(); printf("\nEnter the number of vertices"); scanf("%d",&n); printf("\nEnter the cost adjacency matrix"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&c[i][j]); } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { d[i][j]=c[i][j]; } } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } } printf("\nThe solution to the All pairs shortest path problem is:\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%d\t",d[i][j]); }

printf("\n"); } getch(); } int min(int i,int j) { if(i<j) return i; else return j; } OUTPUT:

Enter the number of vertices 4 Enter the cost adjacency matrix 0 1 7 8 999 0 3 999 999 999 0 2 999 999 999 0 The solution to the All pairs shortest path problem is: 0 1 4 6 999 0 3 5 999 999 0 2 999 999 999 0

PROGRAM 14B

Compute the transitive closure of a given directed graph using Warshall’s algorithm.

#include<stdio.h> #include<conio.h> void main() { int n,i,j,k,p[10][10],a[10][10]; clrscr(); printf("\nEnter the number of vertices"); scanf("%d",&n); printf("\nEnter the adjacency matrix"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { p[i][j]=a[i][j]; } } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(p[i][j]!=1 && (p[i][k]==1 && p[k][j]==1)) p[i][j]=1; } } } printf("\nThe path matrix is:\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%d\t",p[i][j]); }

printf("\n"); } getch(); } OUTPUT:

Enter the number of vertices 4 Enter the adjacency matrix 0 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 The path matrix is: 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 Enter the number of vertices 4 Enter the adjacency matrix 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 The path matrix is: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1

PROGRAM 14

Implement Queen’s problem using Back Tracking.

#include<stdio.h> #include<conio.h> #include<math.h> int canplace(int r,int c[50]) { int i; for(i=0;i<r;i++) { if(c[i]==c[r]||(abs(c[i]-c[r])==abs(i-r))) return (0); } return (1); } void display(int c[50],int n) { int i,j; char cb[10][10]; for(i=0;i<n;i++) { for(j=0;j<n;j++) cb[i][j]='-'; } for(i=0;i<n;i++) cb[i][c[i]]='Q'; printf("---------------------------------\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%c\t",cb[i][j]); printf("\n---------------------------------\n"); } } void nqueen(int n) { int r,c[50]; c[0]=-1; r=0; while(r>=0) { c[r]++;

while(c[r]<n && !canplace(r,c)) { c[r]++;} if(c[r]<n) { if(r==n-1) { display(c,n); printf("\n\n"); } else { r++; c[r]=-1; } } else r--; } } void main() { int n; clrscr(); printf("\nEnter the value of n"); scanf("%d",&n); nqueen(n); getch(); }

OUTPUT:

Enter the value of n 4 --------------------------------- - Q - - --------------------------------- - - - Q --------------------------------- Q - - - --------------------------------- - - Q - --------------------------------- --------------------------------- - - Q - --------------------------------- Q - - - --------------------------------- - - - Q --------------------------------- - Q - - ---------------------------------

top related