• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

ProjectsGeek

Lex and Yacc Calculator code using Unix Script

May 4, 2011 by ProjectsGeek Leave a Comment

Lex and Yacc Calculator code

Implementation of Calculator using LEX and YACC Calculator code.

Lex and Yacc Calculator code-Lex

%{  
 # include  
 # include "y.tab.h"  
 # include  
 %}  
 %%  
 [0-9]+(\.[0-9]+)? { yylval.dval=atof(yytext);return NUMBER;}  
 [\t] ; /* ignore whitespace */  
 \n {return 0;}  
 . return yytext[0];  
 %%  
 int yywrap(void)  
 {  
   return 1;  
 }

Lex and Yacc Calculator code-Yacc

 

 %{  
 # include  
 # include  
 %}  
 %union  
 {  
   double dval;  
 }  
 %token  NUMBER ;  
 %left '-''+'  
 %left '*''/'  
 %right '^'  
 %type  expr  
 %%  
 result:    
   '='expr   
   |expr {printf("%f",$1);}  
   ;  
 expr:  
   expr'+'expr {$$=$1+$3;}  
   | expr'-'expr {$$=$1-$3;}  
   | expr'*'expr {$$=$1*$3;}  
   | expr'^'expr   {  
       $$=pow($1,$3);}  
   | expr'/'expr {  
     if($3==0)  
       yyerror("devide by zero");  
     else  
       $$=$1/$3;}  
   | '-'expr   {$$=-$2;}  
   | '('expr')' { $$=$2;}  
   | NUMBER {$$=$1;}  
   ;  
 %%  
 void yyerror(char *s)  
 {  
   fprintf(stdout,"%s\n",s);  
 }  
 int main()  
 {  
   yyparse();  
   return 0;  
 }

 

 

Other Projects to Try:

  1. Calculator using LEX and YACC
  2. Lexical analyzer Code in C Language
  3. Regular Expression to DFA Code in C Language
  4. Inter process Communication(IPC)-Client Code
  5. 30+ Android Projects with Source Code

Filed Under: System Programming

Macro Processor Pass Two in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

Macro Processor Pass Two in C Language

Implementation of Macro Processor Pass Two. Following cases to be considered

a)Macro without any parameters

b)Macro with Positional Parameters

c)Macro with Key word parameters

d)Macro with positional and keyword parameters

Macro definition and Call Macro Expansion,Design of Macro Processor Definition and expansion processing.Algorithms along with Data structures Nested Macro calls,Call within a call and definition within a definition.

Macro Processor Pass Two Code

 #include  
 #include  
 #include  
 #include  
 FILE *fp;  
 FILE *fp1;  
 FILE *fp2;  
 typedef struct pntab  
 {  
   char par[10];  
 }ptab;  
 typedef struct MNT  
 {  
   char name[10];  
   int nop;  
   int nok;  
   int mdtp;  
   int kpdtp;  
   int noe;  
 }MT;  
 MT mnt[1];  
 ptab pntab[100];  
 /*  
 typedef struct ssntab  
 {  
   char name[50];  
 }ssnt;  
 typedef struct sstab  
 {  
   int val;  
 }sst;  
 sst sstab[50];  
 ssnt ssntab[50];  
 */  
 typedef struct evntab  
 {  
   char name[50];  
 }evnt;  
 evnt evntab[50];  
 typedef struct evtab  
 {  
   int val;  
 }evt;  
 evt evtab[50];  
 typedef struct mdt  
 {  
   char entry[100];  
 }MDT;  
 MDT mdt[100];  
 typedef struct aptab  
 {  
   char value[10];  
 }at;  
 at aptab[100];  
 typedef struct kpdtab  
 {  
   char name[10];  
   char value[10];  
 }ktab;  
 ktab kpdtab[10];  
 void disp_aptab(int len)  
 {  
   printf("\nAPTAB:\n");  
   int i;  
   for(i=0;i<=len;++i)  
   {  
     printf("%d.%s\n",i+1,aptab[i].value);  
   }  
 }  
 void disp_mdt(int len)  
 {  
   printf("MDT:\n");  
   int i=1;  
   while(i<=len)  
   {  
     printf("%d.%s\n",i,mdt[i-1].entry);  
     i++;  
   }  
 }  
 void disp_pntab(int len)  
 {  
   int i;  
   for(i=0;i<=len;++i)  
   {  
     printf("%d.%s\n",i+1,pntab[i].par);  
   }  
 }  
 int search_pntab(char str[],int len)  
 {  
   int i;  
   for(i=0;i<=len;++i)  
   {  
     if(strcmp(str,pntab[i].par)==0)  
     {  
       return i;  
     }  
   }  
   return -1;  
 }  
 void disp_mnt()  
 {  
   printf("\tName\tPP\tKP\tMDTP\tKPDTP\n");  
   printf("\t%s\t%d\t%d\t%d\t%d\n",mnt[0].name,mnt[0].nop,mnt[0].nok,mnt[0].mdtp,mnt[0].kpdtp);  
 }  
 void main()  
 {  
   clrscr();  
   char str[100];  
   int i=0,ptr=-1,j;  
   int nop=0,nok=0,noe=0;  
   fp=fopen("mac.txt","r");  
   if(fp==NULL)  
   {  
     printf("File not found\n");  
     getch();  
     return;  
   }  
   fgets(str,100,fp);  
   if(strcmp("MACRO\n",str)==0)  
     printf("MAcro detected\nExecuting Pass1\n");  
   fgets(str,100,fp);  
   //Creating PNTAB and MNT  
   j=0;  
   while(str[i]!='\t')  
   {  
     mnt[0].name[j]=str[i];  
     i++;j++;  
   }  
   int kpdtab_ptr=0;  
   while(str[i]!='\n')  
   {  
     pntab[ptr].par[j]=NULL;  
     j=0;  
     ptr++;i++;  
     if(str[i]=='&')  
     {  
       i++;  
       ++nop;  
       while(str[i]!=','&&str[i]!='\n')  
       {  
         if(str[i]=='=')  
         {  
           nop--;  
           nok++;  
           i++;  
           strcpy(kpdtab[kpdtab_ptr].name,pntab[ptr].par);  
           if(str[i]!=',')  
           {  
             int k=0;  
             while(str[i]!=','&&str[i]!='\n')  
             {  
               kpdtab[kpdtab_ptr].value[k]=str[i];  
               i++;  
               k++;  
             }  
           }  
         }  
         else  
         {  
           pntab[ptr].par[j]=str[i];  
           j++;i++;  
         }  
       }  
     }  
   }  
   printf("\nPNTAB is\n");  
   disp_pntab(ptr);  
   mnt[0].nop=nop;  
   mnt[0].nok=nok;  
   mnt[0].mdtp=0;  
   mnt[0].kpdtp=0;  
   char label[10],mneu[10],op1[10],op2[10];  
   char intercode[100],buff[10];  
   int x;  
   int mdtp=0;  
   while(!feof(fp))  
   {  
     fgets(str,100,fp);  
     i=0;  
     if(str[0]=='\t')  
     {  
       strcpy(label,NULL);  
       i++;  
     }  
     else  
     {  
       j=0;  
       while(str[i]!='\t'&&str[i]!='\n')  
       {  
         label[j]=str[i];  
         i++;j++;  
       }  
       label[j]=NULL;  
       if(strcmp(label,"MEND")==0)  
       {  
         strcpy(intercode,"\tMEND\n");  
         strcat(intercode,NULL);  
         strcpy(mdt[mdtp].entry,intercode);  
         mdtp++;  
         break;  
       }  
       i++;  
     }  
     j=0;  
     while(str[i]!='\t')  
     {  
       mneu[j]=str[i];  
       i++;j++;  
     }  
     mneu[j]=NULL;  
     j=0;i+=2;  
     while(str[i]!=',')  
     {  
       op1[j]=str[i];  
       i++;j++;  
     }  
     op1[j]=NULL;  
     j=0;i+=2;  
     while(str[i]!='\n')  
     {  
       op2[j]=str[i];  
       i++;j++;  
     }  
     op2[j]=NULL;  
     if(strcmp(mneu,"LCL")==0)  
     {  
       noe++;  
     }  
     strcpy(intercode,NULL);  
     if(strcmp(label,NULL)==0)  
     {  
       strcat(intercode,"\t");  
     }  
     strcat(intercode,mneu);  
     strcat(intercode,"\t");  
     x=search_pntab(op1,ptr);  
     if(x!=-1)  
     {  
       strcat(intercode,"(P,");  
       itoa(x,buff,10);  
       strcat(intercode,buff);  
       strcat(intercode,"),");  
     }  
     if(op2[0]=='=')  
     {  
       strcat(intercode,op2);  
     }  
     else  
     {  
       x=search_pntab(op2,ptr);  
       if(x!=-1)  
       {  
         strcat(intercode,"(P,");  
         itoa(x,buff,10);  
         strcat(intercode,buff);  
         strcat(intercode,")\n\0");  
       }  
     }  
     end:  
     strcat(intercode,NULL);  
     strcpy(mdt[mdtp].entry,intercode);  
     mdtp++;  
 //    getch();  
   }  
   printf("\nMNT:\n");  
   disp_mnt();  
   disp_mdt(mdtp);  
   fp2=fopen("mca.txt","r");//other file is mcal  
   fgets(str,100,fp2);  
   i=0;  
   while(str[i]!=' ')  
     i++;  
   strcpy(buff,NULL);  
   int k=0;  
   int aptab_ptr=0;  
   while(str[i]!='\n')  
   {  
     j=0; i++;  
     while(str[i]!=',' && str[i]!='\n')  
     {  
       if(str[i]=='=')  
       {  
         buff[j]=NULL;  
         x=search_pntab(buff,ptr);  
         strcpy(buff,NULL);  
         j=0;  
         i++;  
         while(str[i]!=',' && str[i]!='\n')  
         {  
           buff[j]=str[i];  
           i++;j++;  
         }  
         buff[j]=NULL;  
         strcpy(aptab[x].value,buff);  
         goto yo;  
       }  
       buff[j]=str[i];  
       i++;j++;  
     }  
     buff[j]=NULL;  
     strcpy(aptab[aptab_ptr++].value,buff);  
     yo:  
   }  
   aptab_ptr+=nok;  
   i=0;  int y;  
   while(i<aptab_ptr) <br="">   {  
     if(strcmp(aptab[i].value,NULL)==0)  
     {  
       y=i-nop;  
       strcpy(aptab[i].value,kpdtab[y].value);  
     }  
     i++;  
   }  
   i=0;           j=0;  
   disp_aptab(ptr);  
   getch();  
   printf("Expanded MAcro is:\n");  
   strcpy(intercode,NULL);  
   while(i<mdtp-1) <br="">   {  
     strcpy(intercode,NULL);  
     j=0;  
     while(mdt[i].entry[j]!='\n')  
     {  
       if(mdt[i].entry[j]=='(')  
       {  
         if(mdt[i].entry[j+1]=='P')  
         {  
           x=(int)mdt[i].entry[j+3]-48;  
           //itoa(aptab[x].value,buff,10);  
           strcat(intercode,aptab[x].value);  
           j=j+4;  
         }  
       }  
       else  
       {  
         x=strlen(intercode);  
         intercode[x]=mdt[i].entry[j];  
         intercode[x+1]=NULL;  
       }  
       j++;  
     }  
     intercode[j]=NULL;  
     printf("%s \n",intercode);  
     i++;  
     //mdt[i].entry  
   }  
   /*while(!feof(fp))  
   {  
     fgets(str,100,fp);  
     printf(str);  
   } */  
   getch();  
 }

Input File

MACRO
INCR    &MEM_VAL,&INCR_VAL,&REG
MOVER    &REG,&MEM_VAL
ADD    &REG,&INCR_VAL
MOVEM    &REG,&MEM_VAL
MEND

Other Projects to Try:

  1. Lexical analyzer Code in C Language
  2. Regular Expression to DFA Code in C Language
  3. Expression Tree using C Language
  4. Stack Operations using C Language
  5. Macro Processor Algorithm

Filed Under: System Programming

Tower of Hanoi problem code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

Tower of Hanoi problem code in C Language

Write a c program for  Tower of Hanoi problem. User  has to provide input to the program in terms of numbers of disc on each tower and the program should print the solution of tower of hanoi.

Tower of Hanoi code

 #include  
 #include  
 #include  
 #include  
 void toh(int n,char src,char dest,char by)  
 {  
   time_t t1;  
   if(n==1)  
     printf("Move frm %c to %c \n",src,dest);  
   else  
   {  
     toh(n-1,src,by,dest);  
     printf("Move frm %c to %c time: \n ",src,dest);  
     delay(500);  
     toh(n-1,by,dest,src);  
   }  
   //printf("time:%ld\n",stime(NULL));  
 }  
 void main()  
 {  
   clrscr();  
   printf("Enter the no of disks\n");  
   int n;  
   scanf("%d",&n);  
   time_t t1,t2;  
   t1=time(NULL);  
   toh(n,'A','C','B');  
   t2=time(NULL);  
   printf("The difference is: %f seconds\n",difftime(t2,t1));  
   getch();  
 }

 

 

Other Projects to Try:

  1. Dijkstra Algorithm in C Language
  2. Regular Expression to DFA Code in C Language
  3. Tower of Hanoi
  4. Prims algorithm Code in C Language
  5. N queen problem code in C Language

Filed Under: Design and Analysis of Algorithms

Regular Expression to DFA Code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

Regular Expression to DFA Code in C Language 

Regular Expression to DFA ( To be taken from compiler point of view) .The implementation to be done as per the algorithm covered in the book Compiler Design and Principles.

Regular Expression to DFA Code

 #include  
 #include  
 #include  
 #include  
 typedef struct tree  
 {  
   char ch;  
   int pos;  
   int nullable;  
   int fpos[5];  
   int lpos[5];  
   struct tree * lc;  
   struct tree * rc;  
 }node;  
 int dfaa[30],df=0;  
 typedef struct foll  
 {  
   int follpos[10];  
   char ch;  
 }follpos;  
 follpos folltab[100];  
 char inpt[10];  
 void follow(node *);  
 node* alloc(char ch)  
 {  
   node * temp;  
   temp=(node *)malloc(sizeof(node));  
   temp->nullable=1;  
   temp->lc=NULL;  
   temp->rc=NULL;  
   temp->ch=ch;  
   temp->fpos[0]=-1;  
   temp->lpos[0]=-1;  
   return temp;  
 }  
 void unio(int [],int []);  
 void sort(int []);  
 int check(int[] ,int);  
 void print_follow(int n)  
 {  
   printf("FOLLOWPOS\n");  
   printf("POS\tNAME\tFOLLOWPOS\n");  
   for(int i=1;i<=n;++i)  
   {  
     printf("%d\t%c\t",i,folltab[i].ch);  
     int j=0;  
     while(folltab[i].follpos[j]!=-1)  
     {  
       printf("%d ",folltab[i].follpos[j]);  
       j++;  
     }  
     printf("\n");  
   }  
 }  
 void print_nullable(node *root)  
 {  
   if(root!=NULL)  
   {  
     print_nullable(root->lc);  
     print_nullable(root->rc);  
     printf("%c\t",root->ch);  
     int i=0;  
     while(root->fpos[i]!=-1)  
     {  
       printf("%d ",root->fpos[i]);  
       i++;  
     }  
     printf("\t");  
     i=0;  
     while(root->lpos[i]!=-1)  
     {  
       printf("%d ",root->lpos[i]);  
       i++;  
     }  
     printf("\n");  
   }  
 }  
 node * create(char str[],int *l)  
 {  
   node * nw;  
   nw=alloc(str[*l]);  
   if(str[*l]=='*'||str[*l]=='|'||str[*l]=='.')  
   {  
     if(str[*l]!='*')  
     {  
       (*l)--;  
       nw->nullable=0;  
       nw->rc=create(str,l);  
     }  
     (*l)--;  
     nw->lc=create(str,l);  
   }  
   else  
     nw->nullable=0;  
   return nw;  
 }  
 void inorder(node *root)  
 {  
   if(root!=NULL)  
   {  
     inorder(root->lc);  
     printf("%c",root->ch);  
     inorder(root->rc);  
   }  
 }  
 void create_nullable(node * root,int *pos)  
 {  
   if(root->lc!=NULL)  
     create_nullable(root->lc,pos);  
   if(root->rc!=NULL)  
     create_nullable(root->rc,pos);  
   if(root->lc==NULL && root->rc==NULL)  
   {  
     root->pos=(*pos);  
     root->fpos[0]=root->lpos[0]=(*pos);  
     root->fpos[1]=root->lpos[1]=-1;  
     folltab[*pos].ch=root->ch;  
     folltab[*pos].follpos[0]=-1;  
     (*pos)++;  
   }  
   else  
   {  
     if(root->ch=='|')  
     {  
       unio(root->fpos,root->lc->fpos);  
       unio(root->fpos,root->rc->fpos);  
       unio(root->lpos,root->lc->lpos);  
       unio(root->lpos,root->rc->lpos);  
     }  
     else if(root->ch=='*')  
     {  
       unio(root->fpos,root->lc->fpos);  
       unio(root->lpos,root->lc->lpos);  
     }  
     else if(root->ch=='.')  
     {  
       if(root->lc->nullable==1)  
       {  
         unio(root->fpos,root->rc->fpos);  
       }  
         unio(root->fpos,root->lc->fpos);  
         sort(root->fpos);  
       if(root->rc->nullable==1)  
       {  
         unio(root->lpos,root->lc->lpos);  
       }  
         unio(root->lpos,root->rc->lpos);  
         sort(root->lpos);  
     }  
     follow(root);  
   }  
 }  
 void follow(node *root)  
 {  
   int i=0;  
   if(root->ch=='*')  
   {  
     while(root->lpos[i]!=-1)  
     {  
       unio(folltab[root->lpos[i]].follpos,root->fpos);  
       i++;  
     }  
   }  
   else if(root->ch=='.')  
   {  
      while(root->lc->lpos[i]!=-1)  
      {  
       unio(folltab[root->lc->lpos[i]].follpos,root->rc->fpos);  
       i++;  
      }  
   }  
 }  
 void unio(int arr1[],int arr2[])  
 {  
   int i=0;  
   while(arr1[i]!=-1)  
     i++;  
   int j=0;int k=0;  
   while(arr2[j]!=-1)  
   {  
     for(k=0;k<i;++k) <br="">     {  
       if(arr2[j]==arr1[k])  
         break;  
     }  
     if(k==i)  
     {  
       arr1[i]=arr2[j];  
       i++;  
     }  
     j++;  
   }  
   arr1[i]=-1;  
 }  
 void sort(int a[])//insertion sort  
 {  
   int i,j,temp;  
   for(i=1;a[i]!=-1;i++)  
   {  
     temp=a[i];  
     for(j=i-1;j>=0&&temp<a[j];j--) <br="">       a[j+1]=a[j];  
     a[j+1]=temp;  
   }  
 }  
 int state[10][10];  
 void dfa()  
 {  
   int j=0,k=0,temp[10];  
   temp[0]=-1;  
   int nos=1;  
   for(int i=0;i<10;++i)  
     state[i][0]=-1;  
   i=0;    int m;  
   unio(state[0],folltab[1].follpos);  
   while(1)  
   {  
     for(i=0;inpt[i]!=NULL;++i)  
     {  
       for(j=0;state[k][j]!=-1;++j)  
       {  
         if(folltab[state[k][j]].ch==inpt[i])  
           { unio(temp,folltab[state[k][j]].follpos);  }  
         }  
           m=check(temp,nos);  
            if(m==-1)  
            {  
             unio(state[nos++],temp);  
             m=nos-1;  
            }  
           dfaa[df++]=m;  
       temp[0]=-1;  
     }  
     if(k==nos-1)  
       break;  
     k++;  
   }  
 }  
 int check(int temp[],int nos)  
 {  
   for(int i=0;i<nos;++i) <br="">   {  
     for(int j=0;temp[j]!=-1;++j)  
     {  
       if(temp[j]!=state[i][j])  
         break;  
     }  
     if(temp[j]==-1 && state[i][j]==-1)  
       return i;  
   }  
   return -1;  
 }  
 void display_dfa()//displaying DFA table  
 {  
   int i,j,k;  
   printf("\nDFA TABLE\n ");  
   for(i=0;inpt[i]!=NULL;i++)  
     printf("\t%c",inpt[i]);  
   for(j=0;j<(df/i);j++)  
   {  
     printf("\n%c\t",j+65);  
     for(k=j*i;k<(j*i)+i;k++)  
       printf("%c\t",dfaa[k]+65);  
   }  
   getch();  
 }  
 void main()  
 {  
   clrscr();  
   char str[50];  
   inpt[0]=NULL;  
   printf("Enter the postfix expression\n");  
   scanf("%s",str);  
   node * root;   
   int l;  
   strcat(str,"#.\0");  
   l=strlen(str);  
   l--;  
   int j=0;  
   for(int i=0;i<l-1;++i) <br="">   {  
     j=0;  
     while(inpt[j]!=NULL)  
     {  
       if(inpt[j]==str[i])  
         break;  
       j++;  
     }  
     if(inpt[j]!=str[i] && str[i]!='|' && str[i]!='*' && str[i]!='.')  
     {  
       inpt[j]=str[i];  
       inpt[j+1]=NULL;  
     }  
   }  
   int pos=1;  
   root=create(str,&l);  
   create_nullable(root,&pos);  
   printf("NULLABLE TABLE\nElement\tFPOS\tLPOS\n");  
   print_nullable(root->lc);  
   print_follow(pos-2);  
   dfa();  
   display_dfa();  
 }

 

Other Projects to Try:

  1. Prims algorithm Code in C Language
  2. BFS AND DFS Algorithm using C Language
  3. Regular Expression to DFA
  4. Expression Tree using C Language
  5. Lexical analyzer Code in C Language

Filed Under: System Programming

Prims algorithm Code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

Prims algorithm Code in C Language

Write a program for Prims algorithm Code in C Language for finding Minimum spanning tree.

 Prims algorithm Code

 #include  
 #include  
 #define max 50  
 #define infinity 32767  
 void input(int adj[max][max],int n)  
 {  
   int i,j;  
   char ch;  
   for(i=0;i<n;++i) <br="">   {  
     for(j=0;j<n;++j) <br="">     {  
       adj[i][j]=infinity;  
     }  
   }  
   int s,d,w;  
   while(1)  
   {  
     printf("Do You want to enter another edge\n");  
     flushall();  
     scanf("%c",&ch);  
     if(ch=='y'||ch=='Y')  
     {  
       flushall();  
       printf("Source:");  
       scanf("%d",&s);  
       printf("Dest:");  
       scanf("%d",&d);  
       printf("Weight:");  
       scanf("%d",&w);  
       adj[s][d]=w;  
       adj[d][s]=w;  
     }  
     else  
       break;  
   }  
 }  
 int check(int arr[],int pos)  
 {  
   while(1)  
   {  
     if(arr[pos]!=-1)  
       pos=arr[pos];  
     else  
       return pos;  
   }  
   //return -1;  
 }  
 void print(int mst[],int n)  
 {  
   printf("SOURCE\tDEST\tWEIGHT\n");  
   for(int i=0;i<(n/3);++i)  
   {  
     for(int j=i*3;j<(i+1)*3;++j)  
     {  
       printf("%d\t",mst[j]);  
     }  
     printf("\n");  
   }  
 }  
 void main()  
 {  
   int adj[max][max];  
   int n;  
   clrscr();  
   printf("Enter the no of vertices\n");  
   scanf("%d",&n);  
   input(adj,n);  
   int visited[max];  
   visited[0]=-1;  
   for(int i=1;i<n;++i) <br="">   {  
     visited[i]=0;  
   }  
   int min=infinity;  
   int pos,ptr=0;  
   int mst[max];  
   for(int j=0;j<n-1;++j) <br="">   {  
     min=infinity;  
     for(i=0;i<n;++i) <br="">     {  
       if(visited[i]!=-1)  
       {  
         if(min>adj[i][visited[i]])  
         {  
           pos=i;  
           min=adj[i][visited[i]];  
         }  
       }  
     }  
     mst[ptr++]=pos;  
     mst[ptr++]=visited[pos];  
     mst[ptr++]=adj[pos][visited[pos]];  
     visited[pos]=-1;  
     for(i=0;i<n;++i) <br="">     {  
       if(visited[i]!=-1 && adj[i][pos]<adj[i][visited[i]] )="" <br="">       {  
         visited[i]=pos;  
       }  
     }  
   }  
   print(mst,ptr);  
   getch();  
 }

 

Other Projects to Try:

  1. Breadth First and Depth First Search C Language
  2. Hash table code in C Language
  3. Bankers Algorithm using C Language OS problem
  4. Kruskal’s Algorithm , Prims Algorithm
  5. Krushkals algorithm Code in C Language

Filed Under: Design and Analysis of Algorithms

N queen problem code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

N queen problem code in C Language

Write a program in C Language to Implement N queen problem using C Language and print the number of Solution to the problem as output.

Recursive backtracking algorithm, iterative backtracking method queens problem. Sum of subsets and Graph coloring, Hamiltonian cycle and Knapsack Problem etc.

N queen problem code

 

 #include  
 #include  
 #include  
 #include  
 int check(int *s,int k)  
 {  
   for(int i=0;i<k;++i) <br="">   {  
     if( (*(s+i)==*(s+k)) || ( abs(i-k)==abs(*(s+i)-*(s+k)) ) )  
       return 1;  
   }  
   return 0;  
 }  
 void display(int * s,int n)  
 {  
   printf("Solution\n");  
   for(int i=0;i<n;++i) <br="">   {  
     printf("\t%d",i);  
   }  
   printf("\n");  
   for(i=0;i<n;++i) <br="">   {  
     printf("%d\t",i);  
     for(int j=0;j<n;++j) <br="">     {  
       if(*(s+i)==j)  
         printf("x\t");  
       else  
         printf("\t");  
     }  
     printf("\n");  
   }  
 }  
 void queen(int *s,int n)  
 {  
   int k;  int count=0;  
   while(*(s+0) <= n-1)  
   {  
     k=1;  
     while(k>0)  
     {  
       while(*(s+k)         ++(*(s+k));  
       if(*(s+k)<n) <br="">       {  
         if(k==n-1)  
         {  
           count++;  
           display(s,n);  
           //if(count%3==0)  
             getch();  
           ++(*(s+k));  
         }  
         else  
           k++;  
       }  
       else  
       {  
         *(s+k)=0;  
         k--;  
         ++*(s+k);  
       }  
     }  
   }  
   printf("\n Total no of solutions are:%d",count);  
 }  
 void main()  
 {  
   int n;  
   clrscr();  
   printf("Enter the no of queens\n");  
   scanf("%d",&n);  
   int *s;  
   s=(int *)calloc(1,2*n);  
   queen(s,n);  
   getch();  
 }

 

Other Projects to Try:

  1. Hash table code in C Language
  2. Bankers Algorithm using C Language OS problem
  3. 8 Queen Problem
  4. Prims algorithm Code in C Language
  5. Krushkals algorithm Code in C Language

Filed Under: Design and Analysis of Algorithms

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 125
  • Page 126
  • Page 127
  • Page 128
  • Page 129
  • Interim pages omitted …
  • Page 135
  • Go to Next Page »

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy