29
//. Program to implement 8-Queens Problem #include<stdio.h> #include<stdlib.h> #define QNO 8 voidplcqun(int, int*); int placed(int, int, int*); void view(int*); void main() { int x[QNO], i; printf("The 8 queens problem"); plcqun(0, x); printf("End"); } voidplcqun(int k, int *x) { inti, j; charch; for(i = 0; i< 8; i++) { if(placed(k, i, x)) { x[k] = i; if(k == 7) { view(x); printf(" Press any key to continue,, Press s to stop:"); scanf(" %c ", &ch); if(ch == 's' || ch == 'S') exit(0); } if(k < 7) plcqun(k + 1, x); } } } int placed(int k, inti, int *x) { int j; for(j = 0; j < k; j++)

19 storaged dev

Embed Size (px)

Citation preview

Page 1: 19 storaged dev

//. Program to implement 8-Queens Problem

#include<stdio.h>#include<stdlib.h>#define QNO 8voidplcqun(int, int*);int placed(int, int, int*);void view(int*);void main(){

int x[QNO], i;printf("The 8 queens problem");plcqun(0, x);printf("End");

}voidplcqun(int k, int *x){

inti, j;charch;for(i = 0; i< 8; i++){

if(placed(k, i, x)){

x[k] = i;if(k == 7){

view(x);printf(" Press any key to continue,, Press s to stop:");scanf(" %c ", &ch);if(ch == 's' || ch == 'S')exit(0);

}if(k < 7)plcqun(k + 1, x);

}}

}int placed(int k, inti, int *x){

int j;for(j = 0; j < k; j++){

if((abs(j - k) == abs(x[j] - i)) || (x[j] == i))return 0;

}return 1;

}

Page 2: 19 storaged dev

void view(int *x){

inti, j;printf(" \n----------------------------------------------\n ");printf(" ");for(i = 0; i< 8; i++){

printf(" %d ",(i + 1));printf(" ");

}for(i = 0; i< 8; i++){

printf(" \n \n %d ",(i + 1));for(j = 0; j < 8; j++){

if(j == x[i])printf(" Q ");

elseprintf(" - ");printf(" ");

}printf(" ");

}printf(" \n---------------------------------------------- ");

}

Page 3: 19 storaged dev

OUTPUT

The 8 queens problem ---------------------------------------------- 1 2 3 4 5 6 7 8 1 Q - - - - - - - 2 - - - - Q - - - 3 - - - - - - - Q 4 - - - - - Q - - 5 - - Q - - - - - 6 - - - - - - Q - 7 - Q - - - - - - 8 - - - Q - - - - ----------------------------------------------Press any key to continue,, Press s to stop:s

Page 4: 19 storaged dev

//To accept a matrix and to display its corresponding sparse matrix.

#include<stdio.h>main(){

int a[20][20],s[20][20],tr[10][10],i,j,r,c,count=0,t,k=0,p;printf("Enter the order of matrix:");scanf("%d%d",&r,&c);printf("Enter the elements of matrix:");for(i=0;i<r;i++){

for(j=0;j<c;j++){

scanf("%d",&a[i][j]);if(a[i][j]==0)count++;

}}

if(count>=(r*c)/2){

t=(r*c)-count;

s[0][0]=r;s[0][1]=c;s[0][2]=t;k=1;for(i=0;i<r;i++){

for(j=0;j<c;j++){

if(a[i][j]!=0){

s[k][0]=i+1;s[k][1]=j+1;s[k][2]=a[i][j];k++;

}

}}

}printf("\nSPARSE MATRIX IS:\n");for(i=0;i<=t;i++){

Page 5: 19 storaged dev

for(j=0;j<3;j++){

printf("%d\t",s[i][j]);

}printf("\n");

}printf("\n");

tr[0][0]=c;tr[0][1]=r;tr[0][2]=t;if(t>0){

k=1;for(j=1;j<=c;j++){

for(p=1;p<=t;p++){

if(s[p][1]==j){

tr[k][0]=s[p][1];tr[k][1]=s[p][0];tr[k][2]=s[p][2];k++;

}}

}}

}

Page 6: 19 storaged dev

OUTPUT

Enter the order of matrix:3 3

Enter the elements of matrix:1 0 0 0 0 2 0 0 0

nSPARSE MATRIX IS:

3 3 2

0 0 1

1 2 2

Page 7: 19 storaged dev

//. Program to add two sparse matrices.

#include<stdio.h>#define sz 50void main(){int a[sz][3], b[sz][3], c[sz][3], r1, c1, p, q, r, z, i, j;printf("Enter the ROWS AND COLUMNS\n");scanf("%d%d", &r1, &c1);printf("Enter the number of non-zero elements in the first matrix\n");scanf("%d", &z);a[0][0] = r1, a[0][1] = c1, a[0][2] = z;printf("Enter the first matrix in sparse form starting with the second row\n");for(i=1;i<=z;i++)for(j=0;j<3;j++)scanf("%d", &a[i][j]);printf("Enter the number of non-zero elements in the second matrix\n");scanf("%d", &z);b[0][0]=r1,b[0][1]=c1,b[0][2]=z;printf("Enter the second matrix in sparse form starting with the second row\n");for(i=1;i<=z;i++)for(j=0;j<3;j++)scanf("%d", &b[i][j]);p=q=r=1;for(i=1;i<=r1;i++){for(j=1;j<=c1;j++){if(a[p][0]==i&&a[p][1]==j&&b[q][0]==i&&b[q][1]==j){c[r][0]=i;c[r][1]=j;c[r][2]=a[p][2]+b[q][2];p++;q++;r++;}else if(a[p][0]==i&&a[p][1]==j)

Page 8: 19 storaged dev

{c[r][0]=i;c[r][1]=j;c[r][2]=a[p][2];p++;r++;}else if(b[q][0]==i&&b[q][1]==j){c[r][0]=i;c[r][1]=j;c[r][2]=b[q][2];q++;r++;}else{}}}c[0][0]=r1,c[0][1]=c1,c[0][2]=r-1;printf("\nMAT1\n");for(i=0;i<=a[0][2];i++){for(j=0;j<3;j++)printf("%d\t", a[i][j]);printf("\n");}printf("\nMAT2\n");for(i=0;i<=b[0][2];i++){for(j=0;j<3;j++)printf("%d\t", b[i][j]);printf("\n");}printf("\nSUM\n");for(i=0;i<=c[0][2];i++){for(j=0;j<3;j++)printf("%d\t", c[i][j]);printf("\n");

Page 9: 19 storaged dev

}}

Page 10: 19 storaged dev

OUTPUT

Enter the ROWS AND COLUMNS:3 3Enter the number of non-zero elements in the first matrix2Enter the first matrix in sparse form starting with the second row1 1 62 2 1Enter the number of non-zero elements in the second matrix2Enter the second matrix in sparse form starting with the second row1 2 42 2 2

MAT13 3 2 1 1 5 2 2 6

MAT23 3 2 1 2 5 2 2 3

SUM3 3 3 1 1 6 1 2 4 2 2 3

Page 11: 19 storaged dev

//Create& execute all the operations of a stack by using Arrays

#include<stdio.h>int stack[10],top=0,n,temp,i,ch;char ch1;void push();void pop();void main(){

printf("\n\nEnter the size of stack:");scanf("%d",&n);do{

printf("\n\n1.PUSH\n2.POP\n\nEnter the choice:");scanf("%d",&ch);switch(ch){

case 1:push();break;

case 2:pop();break;

default:printf("Sorry...invalid entry");break;

}printf("\n\nDo you want to continue?(y/n)");scanf(" %c",&ch1);}while(ch1=='Y'||ch1=='y');printf("\n");

}void push(){

if(top>=n){

printf("\nThe stack is full!!!Insertion not possible:");}else{

printf("\nEnter the elements you want to insert:");scanf("%d",&temp);top=top+1;stack[top]=temp;printf("The stack elements are..:\n");

Page 12: 19 storaged dev

for(i=top;i>=1;i--)printf("%d\n",stack[i]);

}}void pop(){

if(top<=0){

printf("\nTHE STACK IS EMPTY!!!!!");}else{

printf("\nThe element that is popped out is %d\n",stack[top]);top--;if(top==0)

printf("\nStack is empty\n");else{

printf("\nThe remaining stack elements are..:\n");for(i=top;i>=1;i--)printf("%d\n",stack[i]);

}}

}

Page 13: 19 storaged dev

OUTPUT

Enter the size of stack:3

1.PUSH

2.POP

Enter the choice:1

Enter the elements you want to insert:27

The stack elements are..:

27

Do you want to continue?(y/n)y

1.PUSH

2.POP

Enter the choice:1

Enter the elements you want to insert:10

The stack elements are..:

10

27

Do you want to continue?(y/n)y

1.PUSH

2.POP

Enter the choice:1

Enter the elements you want to insert:55

Page 14: 19 storaged dev

The stack elements are..:

55

10

27

Do you want to continue?(y/n)y

1.PUSH

2.POP

Enter the choice:2

The element that is popped out is 55

The remaining stack elements are..:

10

27

Do you want to continue?(y/n)n

Page 15: 19 storaged dev

//Create& execute all the operations of a queue by using Arrays

#include<stdio.h>inti,lmt,rear=0,front=0,e;void add(int q[],int n);void delete(int q[],int n);void display(int q[]);void main(){

intch,q[20];char ch1;printf("Enter the limit:");scanf("%d",&lmt);do{

printf("\n1.ADD ITEM");printf("\n2.DELETE ITEM");printf("\n\n\nEnter your choice:");scanf("%d",&ch);switch(ch){

case 1:add(q,n);break;

case 2:delete(q,n);break;

default:printf("Sorry ...invalid input");break;

}printf("\n\nDo you want to continue?(y/n)");scanf(" %c",&ch1);}while(ch1=='Y'||ch1=='y');

}void add(int q[],intlmt){

if(rear>=lmt){

printf("\n\nQueue is full!!!Addition not possible");}else{

printf("\nEnter the element you want to insert:");scanf("%d",&e);rear=rear+1;q[rear]=e;

Page 16: 19 storaged dev

display(q);}

}void delete(int q[],intlmt){

if(front==rear){

printf("\n\nQueue is empty!!!Deletion is not possible");}else{

front=front+1;e=q[front];printf("\nThe deleted element is %d\n",e);display(q);

}}void display(int q[]){

printf("\nElements in the queue :\n");for(i=front+1;i<=rear;i++)

printf("%d\t",q[i]);

}

Page 17: 19 storaged dev

OUTPUT

Enter the limit:5

1.ADD ITEM2.DELETE ITEM

Enter your choice:1

Enter the element you want to insert:30

Elements in the queue :30

Do you want to continue?(y/n)y

1.ADD ITEM2.DELETE ITEM

Enter your choice:1

Enter the element you want to insert:12

Elements in the queue :30 12

Do you want to continue?(y/n)y

1.ADD ITEM2.DELETE ITEM

Enter your choice:1

Enter the element you want to insert:20

Elements in the queue :30 12 20

Page 18: 19 storaged dev

Do you want to continue?(y/n)y

1.ADD ITEM2.DELETE ITEM

Enter your choice:2

The deleted element is 30

Elements in the queue :12 20

Do you want to continue?(y/n)n

Page 19: 19 storaged dev

//Create& execute all the operations of Circular queue using Arrays

#include<stdio.h>voidaddq();voiddeleteq();voiddisplayq();inti,n,qu[20],rear=0,front=0,e;void main(){

intch;char ch1;printf("Enter the size: ");scanf("%d",&n);do{

printf("CIRCULAR QUEUE OPERATIONS");printf("\n1.ADD");printf("\n2.DELETE");printf("\n3.DISPLAY"); printf("\nEnter your choice: ");scanf("%d",&ch);switch(ch){

case 1:addq();

break;case 2:

deleteq();break;

case 3:displayq();break;default:printf("Wrong Selection");

}printf("\nDo you want to continue?(y/n)");scanf(" %c",&ch1);

}while(ch1=='Y'||ch1=='y');}voidaddq(){

if((rear+1)%n==front)printf("Queue is full\n");

else{

rear=(rear+1)%n;printf("Enter an element\n");

Page 20: 19 storaged dev

scanf("%d",&qu[rear]);}

}voiddeleteq(){

if(front==rear)printf("Queue is Empty,Deletion is not possible");

else{

front=(front+1)%n;printf("Deleted item is %d",qu[front]);

}}voiddisplayq(){

ints,f=front;if(f==rear)

printf("The queue is empty\n");else{

printf("The queue is\n");for(;f!=rear;f=(f+1)%n){

s=(f+1)%n;printf("%d\n",qu[s]);

}}

}

Page 21: 19 storaged dev

OUTPUT

Enter the size : 5CIRCULAR QUEUE OPERATIONS1.ADD2.DELETE3.DISPLAYEnter your choice : 1Enter an element30

Do you want to continue?(y/n)yCIRCULAR QUEUE OPERATIONS1.ADD2.DELETE3.DISPLAYEnter your choice : 1Enter an element12

Do you want to continue?(y/n)yCIRCULAR QUEUE OPERATIONS1.ADD2.DELETE3.DISPLAYEnter your choice : 1Enter an element20

Do you want to continue?(y/n)yCIRCULAR QUEUE OPERATIONS1.ADD2.DELETE3.DISPLAYEnter your choice : 3The queue is30

12

20Do you want to continue?(y/n)y

Page 22: 19 storaged dev

CIRCULAR QUEUE OPERATIONS1.ADD2.DELETE3.DISPLAYEnter your choice : 2Deleted item is 30Do you want to continue?(y/n)yCIRCULAR QUEUE OPERATIONS1.ADD2.DELETE3.DISPLAYEnter your choice : 3The queue is12

20Do you want to continue?(y/n)n

Page 23: 19 storaged dev

//To accept& display polynomial equations.

#include<stdio.h>

int main()

{

int a[90],i,k1,n;

printf(" Enter the order of polynomial number");

scanf("%d",&n);

printf("\nEnter the degrees and coefficients:");

for(i=0;i<2*n;i++)

scanf("%d", &a[i]);

printf("\nFirst polynomial is:");

k1=0;

if(a[k1+1]==1)

printf("x^%d", a[k1]);

else

printf("%dx^%d", a[k1+1],a[k1]);

k1+=2;

while (k1<i)

{

printf("+%dx^%d", a[k1+1],a[k1]);

k1+=2;

}

return 0;

}

Page 24: 19 storaged dev

OUTPUT

Enter the order of polynomial number :3

Enter the degrees and coefficients:3 52 41 6

First polynomial is:5x^3+4x^2+6x^1

o/pEnter the order of polynomial number :3

Enter the degrees and coefficients:3 32 51 6

First polynomial is:3x^3+5x^2+6x^1

o/pEnter the order of polynomial number : 3

Enter the degrees and coefficients:3 62 71 8

First polynomial is:6x^3+7x^2+8x^1o/pEnter the order of polynomial number 3

Enter the degrees and coefficients:3 22 91 3

First polynomial is:2x^3+9x^2+3x^1

Page 25: 19 storaged dev

//11.Program to find the minimum cost spanning tree using Prim’s Method.

#include<stdio.h>inta,b,u,v,n,i,j,ne=1;int visited[10]={0},min,mincost=0,cost[10][10];void main(){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);}

Page 26: 19 storaged dev

OUTPUT

Enter the number of nodes:4

Enter the adjacency matrix:

0 3 8 6

3 0 4 0

8 4 0 2

6 0 2 0

Edge 1:(1 2) cost:3

Edge 2:(2 3) cost:4

Edge 3:(3 4) cost:2

Minimun cost=9