• 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

C Assignments

Matrix Operations in C Language

April 6, 2011 by ProjectsGeek Leave a Comment

Matrix Operations in C Language

Implement following Matrix operations as Given Below: 

  • Addition with arrays.
  • Multiplication without pointers to arrays
  • Transpose with  arrays
  • Saddle point without pointers to arrays

Matrix Operations in C Code

#include  
 #include  
 template  
 class matrix  
 {  
   public:  
   T mat[10][10];  
   int i;  
   matrix()  
   {  
     cout<<"\n\tMATRIX IS:";  
     for(i=0;i<m;i++) <br="">       for(j=0;j<n;j++) <br="">         mat[i][j]=0;  
   }  
   matrix(int m,int n)  
   {  
     cout<<"\n\tMATRIX IS:";  
     for(int i=0;i<m;i++) <br="">       for(int j=0;j<n;j++) <br="">         cin>>mat[i][j];  
   }  
   void display(int m,int n)  
   {  
     for(int i=0;i<m;i++) <br="">     {  
       cout<<"\n";  
       for(int j=0;j<n;j++) <br="">       {  
         cout<<"\t";  
         cout<<mat[i][j]; <br="">       }  
     }  
     getch();  
   }  
 };  
 template  
 void add(T1 mat1,T2 mat2,int r,int c)  
 {  
    int i,j;  
    float mat[20][20];  
    for(i=0;i<r;i++) <br="">    {  
     for(j=0;j<c;j++) <br="">     {  
       mat[i][j]=mat1[i][j]+mat2[i][j];  
     }  
    }  
    disp_res(mat,r,c);  
    getch();  
 }  
 template  
 void disp_res(T mat,int m,int n)  
 {  
     for(int i=0;i<m;i++) <br="">     {  
       cout<<"\n";  
       for(int j=0;j<n;j++) <br="">       {  
         cout<<"\t";  
         cout<<mat[i][j]; <br="">       }  
     }  
     getch();  
 }  
 template  
 void sub(T1 mat1,T2 mat2,int r,int c)  
 {  
    int i,j;  
    float mat[20][20];  
    for(i=0;i<r;i++) <br="">    {  
     for(j=0;j<c;j++) <br="">     {  
       mat[i][j]=mat1[i][j]-mat2[i][j];  
     }  
    }  
    disp_res(mat,r,c);  
    getch();  
 }  
 template  
 void mul(T1 mat1,T2 mat2,int r,int c,int a)  
 {  
   int i,j,k;  
   float mat[10][10];  
   for(i=0;i<r;i++) <br="">   {  
     for(j=0;j<a;j++) <br="">     {  
       mat[i][j]=0;  
       for(k=0;k<c;k++) <br="">       mat[i][j]+=mat1[i][k]*mat2[k][j];  
     }  
   }  
   disp_res(mat,r,a);  
   getch();  
 }  
 template  
 void trans(T mat,int m,int n)  
 {  
     int i,j;  
     T t;  
     if(m>=n)  
     {  
       for(i=0;i<m;i++) <br="">       {  
         for(j=0;j<i;j++) <br="">         {  
           t[i][j]=mat[i][j];  
           mat[i][j]=mat[j][i];  
           mat[j][i]=t[i][j];  
         }  
       }  
       disp_res(mat,n,m);  
     }  
     else  
     {  
       for(i=0;i<n;i++) <br="">       {  
         for(j=0;j<i;j++) <br="">         {  
           t[i][j]=mat[i][j];  
           mat[i][j]=mat[j][i];  
           mat[j][i]=t[i][j];  
         }  
       }  
       disp_res(mat,m,n);  
     }  
     getch();  
 }  
 void main()  
 {  
   int ch,r,c,ch1;  
   matrixt3();  
   do  
   {  
     clrscr();  
     cout<<"\n\t##### MATRIX OPERATIONS ######"  
       <<"\n\t1.INPUT"  
       <<"\n\t2.DISPLAY"  
       <<"\n\t3.ADDITION"  
       <<"\n\t4.SUBTRACTION"  
       <<"\n\t5.MULTIPLICATION"  
       <<"\n\t6.TRANSPOSE"  
       <<"\n\t7.EXIT";  
     cout<<"\n\tENTER UR CHOICE:";  
     cin>>ch;  
     switch(ch)  
     {  
       case 1:  
         cout<<"\n\tENTER NO. OF ROWS:";  
         cin>>r;  
         cout<<"\n\tENTER NO. OF COLUMNS:";  
         cin>>c;  
         matrixt1(r,c);  
         matrixt2(r,c);  
         break;  
       case 2: do  
         {  
           cout<<"\n\t#### DISPLAY MENU ####";  
           cout<<"\n\t1.1st MATRIX";  
           cout<<"\n\t2.2nd MATRIX";  
           cout<<"\n\t3.EXIT";  
           cout<<"\n\tENTER UR CHOICE:";  
           cin>>ch1;  
           clrscr();  
           switch(ch1)  
           {  
             case 1: cout<<"\n\t1st MATRIX";  
               t1.display(r,c);  
               break;  
             case 2: cout<<"\n\t2nd MATRIX";  
               t2.display(r,c);  
               break;  
             case 3: break;  
           }  
         }while(ch1!=3);  
         break;  
       case 3: cout<<"\n\tADDITION IS:";  
         add(t1.mat,t2.mat, r,c);  
         break;  
       case 4: cout<<"\n\tSUBTRACTION IS:";  
         sub(t1.mat,t2.mat, r,c);  
         break;  
       case 5: cout<<"\n\tMULTIPLICATION IS:";  
         mul(t1.mat,t2.mat, r,c,r);  
         break;  
       case 6: do  
         {  
           cout<<"\n\t#### TRANSPOSE MENU ####";  
           cout<<"\n\t1.1st MATRIX";  
           cout<<"\n\t2.2nd MATRIX";  
           cout<<"\n\t3.EXIT";  
           cout<<"\n\tENTER UR CHOICE:";  
           cin>>ch1;  
           clrscr();  
           switch(ch1)  
           {  
             case 1: cout<<"\n\tTRANSPOSE OF 1st MATRIX:";  
               trans(t1.mat,r,c);  
               trans(t1.mat,r,c);  
               break;  
             case 2: cout<<"\n\tTRANSPOSE OF 2nd MATRIX:";  
               trans(t2.mat,r,c);  
               trans(t2.mat,r,c);  
               break;  
             case 3: break;  
           }  
         }while(ch1!=3);  
         break;  
       case 7:  
         break;  
     }  
   }while(ch!=7);  
 }

 

Other Projects to Try:

  1. How to Implement Hash Table using C language
  2. Hash table code in C Language
  3. Matrix operations in c language
  4. Matrix Operations in C Language
  5. Sparse Matrix Operations Code using C Langauge

Filed Under: C Assignments, Computer Graphics Tagged With: Computer Graphics

Student Database in C Language

April 6, 2011 by ProjectsGeek 4 Comments

Student Database in C Language 

Accept student information (e.g. Roll No, Name, Percentage etc.).

  • Display the data in descending order of Percentage (Bubble Sort).
  • Display data for Roll No specified by user (Linear Search).
  • Display the number of passes and comparisons for different test cases (Worst,
  • Average, Best case).

Student Database in C Language  Code

 #include  
 #include  
 #include  
 #include  
 class A  
 {  
   protected:  
   char blood[4],name[20];  
   int date,month,year;  
   public:  
   void inputa()  
   {  
     cout<<"ENTER NAME";  
     gets(name);  
     cout<<"ENTER DATE OF BIRTH";  
     cin>>date>>month>>year;  
     cout<<"ENTER BLODD GROUP!!";  
     gets(blood);  
   }  
   virtual void display()  
   {  
     cout<<"\nNAME:"<<name; <br="">     cout<<"\nDATE OF BIRTH:"<<date<<" "<<month<<"="" "<<year;="" <br="">     cout<<"\nBLOOD GROUP:"<<blood; <br="">     getch();  
   }  
 };  
 class B:virtual public A  
 {  
   protected:  
   int height;  
   int weight;  
   public:  
   void inputb()  
   {  
     cout<<"ENTER HEIGHT";  
     cin>>height;  
     cout<<"ENTER weight";  
     cin>>weight;  
   }  
   virtual void display()  
   {  
     cout<<"\nheight :"<<height; <br="">     cout<<"\nweight:"<<weight; <br="">     getch();  
   }  
 };  
 class C:virtual public A  
 {  
   protected:  
   int hno ;  
   char city[10];  
   unsigned long int insno;  
   public:  
   void inputc()  
   {  
     cout<<"ENTER ADDRESS OF CARD HOLDER";  
     cout<<"HOUSE NO";  
     cin>>hno;  
     cout<<"CITY";  
     cin>>city;  
     cout<<"ENTER INSURANCE NO";  
     cin>>insno;  
   }  
   virtual void display()  
   {  
     cout<<"\nADDRESS OF CARD HOLDER:"<<hno<<" "<<city;="" <br="">     cout<<"\nINSURANCE NO OF CARD HOLDER:"<<insno; <br="">     getch();  
   }  
 };  
 class D:public B,public C  
 {  
   int licno;  
   unsigned long int teleno;  
   public:  
   int id;  
   D()  
   {  
     id=0;  
     strcpy(name,'\0');  
     date=0;  
     month=0;  
     year=0;  
     height=0 ;  
     weight=0;  
     strcpy(blood,'\0');  
     hno=0;  
     strcpy(city,'\0');  
     insno=0;  
     teleno=0;  
     licno=0;  
   }  
   D(D &  d)  
   {  
     id=d.id;  
     strcpy(name,d.name);  
     date=d.date;  
     month=d.month;  
     year=d.year;  
     height=d.height;  
     weight=d.weight;  
     strcpy(blood,d.blood);  
     hno=d.hno;  
     strcpy(city,d.city);  
     insno=d.insno;  
     teleno=d.teleno;  
     licno=d.licno;  
   }  
   void inputd()  
   {  
     cout<<"ENTER TELEPHONE NO AFTER FIRST DIGIT ";  
     cin>>teleno;  
     cout<<"ENTER LICENCE NO";  
     cin>>licno;  
   }  
   void display()  
   {  
     clrscr();  
     cout<<"\nID NO"<<id; <br="">     cout<<"\nNAME:"<<name; <br="">     cout<<"\nDATE OF BIRTH:"<<date<<" "<<month<<"="" "<<year;="" <br="">     cout<<"\nHEIGHT:"<<height; <br="">     cout<<"\nWEIGHT:"<<weight; <br="">     cout<<"\ncBLOOD GROUP"<<blood; <br="">     cout<<"\nADDRESS:"<<hno<<" "<<city;="" <br="">     cout<<"\nINSURANCE NO:"<<insno; <br="">     cout<<"\nPHONE NO:"<<"9"<<teleno; <br="">     cout<<"\nLICENCE NO:"<<licno; <br="">     getch();  
   }  
 };  
 void main()  
 {  
   D d[20];  
   int ch=0,i=0,j;  
   do  
   {  
     clrscr();  
     cout<<"\t\t\t\t1.BUILD A MASTER TABLE\n" ;  
     cout<<"\t\t\t\t2.DISPLAY THE ENTRIES\n" ;  
     cout<<"\t\t\t\t3.INSERT A NEW DATA\n";  
     cout<<"\t\t\t\t4.DELETE A DATA\n" ;  
     cout<<"\t\t\t\t5.SEARCH FOR SPECIFIC ENTRY\n";  
     cout<<"\t\t\t\t6.EXIT\n" ;  
     cout<<"\t\t\t\tENTER UR CHOICE";  
     cin>>ch;  
     switch(ch)  
     {  
       case 1:  
         cout<<"\n\t\tBUILD A RECORD FOR CREDIT CARD!!";  
         i=0;  
         do  
         {  
           d[i].id=i+1;  
           d[i].inputa();  
           d[i].inputb();  
           d[i].inputc();  
           d[i].inputd();  
       i++;  
           cout<<"WANT TO ENTER MORE DATA";  
         }while(getche()=='y');  
         break;  
       case 2:  
         cout<<"\n\t\tDISPLAY CREDIT CARD RECORD!!";  
         for(j=0;j<i;j++) <br="">         {  
           d[j].display();  
         }  
         break;  
       case 3:  
         cout<<"\n\t\tINSERT A NEW RECORD IN CREDIT CARD ENTRY!!";  
         int k=i;  
         for(j=0;j<i;j++) <br="">           if(d[j].id!=j+1)  
           {  
             k=j;  
             while(j<i) <br="">               d[j+1]=d[j++];  
             break;  
           }  
         d[k].id=k+1;  
         d[k].inputa();  
         d[k].inputb();  
         d[k].inputc();  
         d[k].inputd();  
         i++;  
         break;  
       case 4:  
         cout<<"\n\t\tDELETE A CREDIT CARD RECORD \n\t\tENTER ID";  
         cin>>k;  
         for(j=0;j<i;j++) <br="">           if(d[j].id==k)  
           {  
             while(j<(i-1))  
              {  
               d[j]=d[j+1];  
               break;  
              }  
           }  
           i--;  
         break;  
       case 5:  
         cout<<"\n\t\tSEARCH FOR SPECIFIC ENTRY\n\t\tENTER ID";  
         cin>>k;  
         for(j=0;j<i;j++) <br="">           if(d[j].id==k)  
             break;  
         if(j!=i)  
         {  
             d[j].display();  
         }  
         else  
           cout<<"SORRY RECORD NOT FOUND";  
         getch();  
         break;  
       case 6:  
         break;  
     }  
   }while(ch!=6);  
   getch();  
 }

 

Other Projects to Try:

  1. Matrix Operations in C Language
  2. Friend Function Implementation using C++
  3. Operator Overloading on String Functions C++ Language
  4. Simple Student Database using C Language
  5. Student Database using Virtual functions in C++

Filed Under: C Assignments, Computer Graphics Tagged With: Computer Graphics

Boundary and Flood Fill Algorithms in C++ Language

April 6, 2011 by ProjectsGeek 2 Comments

Boundary and Flood Fill Algorithms

Write a program for Boundary and Flood Fill Algorithms in C++ Language . Boundary and Flood Fill Algorithms in C++ should accept the coordinates for polygon.

  • INPUT
  • FLOOD FILL
  • BOUNDARY FILL
  • EXIT

After selecting Boundary and Flood fill algorithm option , It must fill polygon by both algorithms.

Boundary and Flood Fill Algorithms Code

 #include  
 #include  
 #include  
 #include  
 #define max 100000  
 class fill  
 {  
   int edge,a[50],sx,sy,midx,midy;  
   public:  
   void input()  
   {  
     cout<<"\nENTER NO OF EDGES:";  
     cin>>edge;  
     int j=0;  
     for(int i=0;i<edge;i++) <br="">     {  
       cout<<"\nENTER THE X"<<i+1<<"coordinate:"; <br="">       cin>>a[j++];  
       cout<<"\nENTER THE Y"<<i+1<<"coordinate:"; <br="">       cin>>a[j++];  
     }  
   }  
   void b_fill(int x,int y,int clr)  
   {  
     int j=0;  
     midx=getmaxx()/2;  
     midy=getmaxy()/2;  
     setcolor(WHITE);  
     line(0,midy,2*midx,midy);  
     line(midx,0,midx,2*midy);  
     setcolor(RED);  
     for(int i=0;i<edge;i++) <br="">     {  
       if(edge-i!=1)  
         line(a[j]+midx,midy-a[j+1],a[j+2]+midx,midy-a[j+3]);  
       else  
         line(a[j]+midx,midy-a[j+1],a[0]+midx,midy-a[1]);  
       j=j+2;  
     }  
     getch();  
     int st[max][2],top=-1;  
     x=midx+x;  
     y=midy-y;  
     st[++top][0]=x;  
     st[top][1]=y;  
     while(top!=-1)  
     {  
       x=st[top][0];  
       y=st[top][1];  
       top--;  
       if((getpixel(x+1,y)!=clr)&&(getpixel(x+1,y)!=RED))  
       {  
         putpixel(x+1,y,clr);  
         st[++top][0]=x+1;  
         st[top][1]=y;  
       }  
       if((getpixel(x-1,y)!=clr)&&(getpixel(x-1,y)!=RED))  
       {  
         putpixel(x-1,y,clr);  
         st[++top][0]=x-1;  
         st[top][1]=y;  
       }  
       if((getpixel(x,y+1)!=clr)&&(getpixel(x,y+1)!=RED))  
       {  
         putpixel(x,y+1,clr);  
         st[++top][0]=x;  
         st[top][1]=y+1;  
       }  
       if((getpixel(x,y-1)!=clr)&&(getpixel(x,y-1)!=RED))  
       {  
         putpixel(x,y-1,clr);  
         st[++top][0]=x;  
         st[top][1]=y-1;  
       }  
      }  
   }  
   void f_fill(int x,int y,int clr)  
   {  
     int j=0;  
     midx=getmaxx()/2;  
     midy=getmaxy()/2;  
     setcolor(WHITE);  
     line(0,midy,2*midx,midy);  
     line(midx,0,midx,2*midy);  
     setcolor(RED);  
     for(int i=0;i<edge;i++) <br="">     {  
       if(edge-i!=1)  
         line(a[j]+midx,midy-a[j+1],a[j+2]+midx,midy-a[j+3]);  
       else  
         line(a[j]+midx,midy-a[j+1],a[0]+midx,midy-a[1]);  
       j=j+2;  
     }  
     getch();  
     int st[max][2],top=-1;  
     x=midx+x;  
     y=midy-y;  
     st[++top][0]=x;  
     st[top][1]=y;  
     putpixel(x,y,col);  
     while(top!=-1)  
     {  
       x=st[top][0];  
       y=st[top][1];  
       top--;  
       if((getpixel(x+1,y)!=clr)&&(getpixel(x+1,y)==7))  
       {  
         putpixel(x+1,y,clr);  
         st[++top][0]=x+1;  
         st[top][1]=y;  
       }  
       if((getpixel(x-1,y)!=clr)&&(getpixel(x-1,y)==7))  
       {  
         putpixel(x-1,y,clr);  
         st[++top][0]=x-1;  
         st[top][1]=y;  
       }  
       if((getpixel(x,y+1)!=clr)&&(getpixel(x,y+1)==7))  
       {  
         putpixel(x,y+1,clr);  
         st[++top][0]=x;  
         st[top][1]=y+1;  
       }  
       if((getpixel(x,y-1)!=clr)&&(getpixel(x,y-1)==7))  
       {  
         putpixel(x,y-1,clr);  
         st[++top][0]=x;  
         st[top][1]=y-1;  
       }  
      }  
   }  
 };  
 void menu()  
 {  
   clrscr();  
   cout<<"\n\t##### MAIN MENU #####";  
   cout<<"\n\t1.INPUT";  
   cout<<"\n\t2.FLOOD FILL";  
   cout<<"\n\t3.BOUNDARY FILL";  
   cout<<"\n\t4.EXIT";  
   cout<<"\n\tENTER UR CHOICE:";  
 }  
 void main()  
 {  
   int gd=DETECT,gm;  
     initgraph(&gd,&gm,"c:\\tc\\bgi");  
   int ch,x,y,clr;  
   fill f;  
   do  
   {  
     menu();  
     cin>>ch;  
     clrscr();  
     switch(ch)  
     {  
       case 1:  
         f.input();  
         break;  
       case 2:  
         cout<<"\n#### FLOOD FILL ####";  
         cout<<"\nENTER THE COLOR CODE TO FILL:";  
         cin>>clr;  
         cout<<"\nENTER THE X-COORDINATE OF SEED POINT:";  
         cin>>x;  
         cout<<"\nENTER THE Y-COORDINATE OF SEED POINT:";  
         cin>>y;  
         f.f_fill(x,y,clr);  
         getch();  
         break;  
       case 3:  
         cout<<"\n#### BOUNDARY FILL ####";  
         cout<<"\nENTER THE COLOR CODE TO FILL:";  
         cin>>clr;  
         cout<<"\nENTER THE X-COORDINATE OF SEED POINT:";  
         cin>>x;  
         cout<<"\nENTER THE Y-COORDINATE OF SEED POINT:";  
         cin>>y;  
         f.b_fill(x,y,clr);  
         getch();  
         break;  
       case 4:  
         break;  
     }  
   }while(ch!=4);  
 }

 

 

Other Projects to Try:

  1. Graphics Editor code Using C++ Language
  2. Transformations computer graphics using C++ Language
  3. Computer graphics codes in C++ Language
  4. Paging Algorithms using C language OS problem
  5. Bressenham and DDA Line Drawing algorithms

Filed Under: C Assignments, Computer Graphics Tagged With: Computer Graphics

Hash table code in C Language

April 5, 2011 by ProjectsGeek 4 Comments

Hash table code in C

Implementation of Hash table using array and handle collisions using Linear probing with replacement and Chaining without replacement.

Hash table code in C Language Code

 #include  
 #include  
 #define MAX 8  
 void createwor(int [][2]);  
 void insertwor(int [][2]);  
 void deletwor(int [][2]);  
 void displaywor(int [][2]);  
 void createwr(int [][2]);  
 void insertwr(int [][2]);  
 void deletwr(int [][2]);  
 void displaywr(int [][2]);  
 void searchwr(int [][2]);  
 void searchwor(int [][2]);  
 void main()  
 {  
 int ch,ch1,i,j;  
 int a[MAX][2],b[MAX][2];  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 b[i][j]=-1;  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 a[i][j]=-1;  
 do  
 {  
 clrscr();  
 printf("\n\t\tHASH TABLE\n\t\t1.CREATE HASH TABLE\n\t\t2.INSERT\n\t\t3.DELETE\n\t\t4.DISPLAY\n\t\t5.SEARCH\n\t\t6.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 do  
 {  
 clrscr();  
 printf("CREATE HASH TABLE\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 createwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 createwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 2:  
 do  
 {  
 clrscr();  
 printf("\n\t\tINSERT\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 insertwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 insertwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 3:  
 do  
 {  
 clrscr();  
 printf("\n\t\tDELETE\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 deletwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 deletwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 4:  
 do  
 {  
 clrscr();  
 printf("\n\t\tDISPLAY\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 displaywr(a) ;  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 displaywor(b) ;  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 5:  
 do  
 {  
 clrscr();  
 printf("\n\t\tSEARCH\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 searchwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 searchwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 6:  
 break;  
 }  
 }while(ch!=6);  
 getch();  
 }  
 void createwor(int b[][2])  
 {  
 int i,j;  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 b[i][j]=-1;  
 do  
 {  
 insertwor(b);  
 printf("DO U WANT TO ENTER MORE");  
 }while(getche()=='y');  
 }  
 void insertwor(int b[][2])  
 {  
 int r,s,t,a;  
 printf("\nENTER DATA");  
 scanf("%d",&a);  
 s=a%5;  
 if(b[s][0]==-1)  
 b[s][0]=a;  
 else  
 {  
 //printf("COLLISION OCCURS");  
 //getch();  
 while(1)  
 {  
 printf("\nTRUE COLLISION OCCURS");  
 getch();  
 if(b[s][1]==-1)  
 {  
 t=s;  
 while(b[s][0]!=-1)  
 { //s++;  
 s=(s+1)%MAX;  
 if(s==t)  
 {  
 printf("OVERFLOW");  
 break;  
 }  
 }  
 if(s!=t)  
 {  
 b[s][0]=a;  
 b[t][1]=s;  
 }  
 break;  
 }  
 else  
 s=b[s][1];  
 }  
 }  
 }  
 void deletwr(int b[][2])  
 {  
 int r,s,t,a;  
 printf("ENTER DATA WHICH U WANT TO DELETE");  
 scanf("%d",&a);  
 t=s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 t=s;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 while(1)  
 {  
 if(b[b[t][1]][0]!=a)  
 b[t][0]=b[b[t][1]][0];  
 r=b[t][1];  
 if(b[r][1]==-1)  
 {  
 b[t][1]=b[r][1];  
 break;  
 }  
 t=r;  
 }  
 b[r][0]=-1;  
 }  
 }  
 void displaywor(int b[][2])  
 {  
 int i,r,s,t,a;  
 printf("\nINDEX\tDATA\tCHAIN");  
 for(i=0;i<max;i++) <br=""> {  
 if(b[i][0]!=-1)  
 {  
 printf("\n%d",i);  
 printf("\t%d",b[i][0]);  
 //if(b[i][1]!=-1)  
 printf("\t%d",b[i][1]);  
 }  
 }  
 getch();  
 }  
 void createwr(int b[][2])  
 {  
 int i,j;  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 b[i][j]=-1;  
 do  
 {  
 insertwr(b);  
 printf("DO U WANT TO ENTER MORE");  
 }while(getche()=='y');  
 }  
 void insertwr(int b[][2])  
 {  
 int r,s,t,a,i;  
 printf("\nENTER DATA");  
 scanf("%d",&a);  
 s=a%5;  
 if(b[s][0]==-1)  
 b[s][0]=a;  
 else if(s==(b[s][0]%5))  
 {  
 //printf("TRUE COLLISION OCCURS");  
 //getch();  
 while(1)  
 {  
 printf("\nTRUE COLLISION OCCURS");  
 getch();  
 if(b[s][1]==-1)  
 {  
 t=s;  
 while(b[s][0]!=-1)  
 {  
 s=(s+1)%MAX;  
 if(t==s)  
 {  
 printf("OVERFLOW");  
 getch();  
 break;  
 }  
 }  
 if(t!=s)  
 {  
 b[s][0]=a;  
 b[t][1]=s;  
 }  
 break;  
 }  
 else  
 s=b[s][1];  
 }  
 }  
 else  
 {  
 printf("FALSE COLLISION OCCURS");  
 getch();  
 r=i=b[s][0]%5;  
 while(1)  
 {  
 if(b[i][1]==s)  
 {  
 b[i][1]=b[s][1];  
 break;  
 }  
 i=b[i][1];  
 }  
 t=i;  
 while(b[i][1]!=-1)  
 i=b[i][1];  
 t=i;  
 i=(i+1)%MAX;  
 while(b[i][0]!=-1)  
 {  
 i=(i+1)%MAX;  
 if(r==i)  
 {  
 printf("OVERFLOW");  
 getch();  
 break;  
 }  
 }  
 if(r!=i)  
 {  
 b[t][1]=i;  
 b[i][0]=b[s][0];  
 //b[i][1]=-1;  
 b[s][0]=a;  
 b[s][1]=-1;  
 //b[r][1]=i;  
 }  
 }  
 }  
 void displaywr(int b[][2])  
 {  
 int i,r,s,t,a;  
 printf("\nINDEX\tDATA\tCHAIN");  
 for(i=0;i<max;i++) <br=""> {  
 if(b[i][0]!=-1)  
 {  
 printf("\n%d",i);  
 printf("\t%d",b[i][0]);  
 //if(b[i][1]!=-1)  
 printf("\t%d",b[i][1]);  
 }  
 }  
 getch();  
 }  
 void searchwr(int b[][2])  
 {  
 int r,s,a;  
 printf("ENTER DATA WHICH U WANT TO SEARCH");  
 scanf("%d",&a);  
 s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 printf("\nINDEX IS %d:",s);  
 printf("\nDATA IS %d:",b[s][0]);  
 printf("\nINDEX WHICH IS CHAINED FROM IT:%d",b[s][1]);  
 }  
 getch();  
 }  
 void searchwor(int b[][2])  
 {  
 int r,s,a;  
 printf("ENTER DATA WHICH U WANT TO SEARCH");  
 scanf("%d",&a);  
 s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 printf("\nINDEX IS %d:",s);  
 printf("\nDATA IS %d:",b[s][0]);  
 printf("\nINDEX WHICH IS CHAINED FROM IT:%d",b[s][1]);  
 }  
 getch();  
 }  
 void deletwor(int b[][2])  
 {  
 int r,s,t,a,k;  
 printf("ENTER DATA WHICH U WANT TO DELETE");  
 scanf("%d",&a);  
 t=s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 t=s;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 r=t;  
 while(1)  
 {  
 if(b[b[r][1]][0]!=a&&b[b[r][1]][0]%5!=b[r][1])  
 {  
 b[t][0]=b[b[r][1]][0];  
 t=r;  
 }  
 else if(b[b[t][1]][1]==-1)  
 {  
 b[t][1]=-1;  
 break;  
 }  
 r=b[r][1];  
 if(b[r][1]==-1)  
 {  
 b[t][1]=b[r][1];  
 break;  
 }  
 if(b[b[t][1]][0]!=a&&b[b[t][1]][0]%5!=b[t][1])  
 {  
 t=r;  
 k=r;  
 }  
 }  
 b[r][0]=b[k][1]=-1;  
 }  
 }  

 

Other Projects to Try:

  1. Sparse Matrix Operations Code using C Langauge
  2. Matrix operations in c language
  3. Breadth First and Depth First Search C Language
  4. Krushkals algorithm Code in C Language
  5. How to Implement Hash Table using C language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Primitive operations on Sequential file in C language

April 5, 2011 by ProjectsGeek Leave a Comment

Primitive operations on Sequential file in C language

Implement all primitive operations on Sequential file in C

Primitive operations on Sequential file in C language

 
 #include  
 #include  
 #include  
 typedef struct stud  
 {  
 int roll;  
 char name[20];  
 struct add  
 {  
 int hno;  
 char city[10];  
 }add;  
 char clas[4];  
 int mark;  
 }stud;  
 stud alloc(stud );  
 void insert(char [20]);  
 void create();  
 void display();  
 void searchnr(char [20]);  
 void searchcr(char [20]);  
 void modify();  
 void delet();  
 void main()  
 {  
 int ch,ch1;  
 char filename[20];  
 do  
 {  
 clrscr();  
 printf("\n\t\tFILE OPERATION\n\t\t1.CREATE\n\t\t2.INSERT\n\t\t3.DISPLAY\n\t\t4.SEARCH\n\t\t5.MODIFY\n\t\t6.DELETE\n\t\t7.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 printf("\n\t\tCREATE");  
 create();  
 break;  
 case 2:  
 printf("\n\t\tINSERT");  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 insert(filename);  
 break;  
 case 3:  
 printf("\n\t\tDISPLAY");  
 display();  
 break;  
 case 4:  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 do  
 {  
 clrscr();  
 printf("\n\t\tSEARCH\n\t\t1.BY NAME N ROLL NO\n\t\t2.BY ROLL NO N CLASS\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("SEARCH BY NAME N ROLL NO");  
 searchnr(filename);  
 break;  
 case 2:  
 printf("SEARCH BY CLASS N ROLL NO");  
 searchcr(filename);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 5:  
 printf("\n\t\tMODIFY");  
 modify();  
 break;  
 case 6:  
 printf("\n\t\tDELETE");  
 delet();  
 break;  
 case 7:  
 break;  
 }  
 }while(ch!=7);  
 }  
 void create()  
 {  
 FILE *fp;  
 char filename[20];  
 printf("\nENTER THE FILE NAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 if(fp==NULL)  
 {  
 fclose(fp);  
 fopen(filename,"r");  
 }  
 else  
 {  
 printf("\nFILE IS ALREADY PRESENT,DO U WANT TO OVERWRITE PRESS 'y'");  
 if(getche()=='y')  
 {  
 fclose(fp);  
 fopen(filename,"w");  
 }  
 else  
 {  
 fclose(fp);  
 fopen(filename,"a");  
 }  
 }  
 fclose(fp);  
 do  
 {  
 insert(filename);  
 printf("DO U WANT TO CONTINUE:y/n");  
 }while(getche()=='y');  
 }  
 void insert(char filename[20])  
 {  
 FILE *fp;  
 char cl[10];  
 int i=0,r;  
 stud st,st1;  
 fp=fopen(filename,"r");  
 while(1)  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&st.roll);  
 if(st.roll<0&&st.roll>60)  
 printf("\nINVALID ROLL NO");  
 else  
 break;  
 }  
 while(1)  
 {  
 printf("\nENTER CLASS:fe/se/te/be only");  
 scanf("%s",st.clas);  
 if(strcmp(st.clas,"fe")!=0&&strcmp(st.clas,"se")!=0&&strcmp(st.clas,"te")!=0&&strcmp(st.clas,"be")!=0)  
 printf("\nINVALID CLASS");  
 else  
 break;  
 }  
 if(fp==NULL)  
 {  
 printf("\nTHIS IS FIRST ENTRY IN THIS FILE");  
 }  
 else  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st1.roll,&st1.mark,&st1.add.hno,st1.add.city,st1.name,st1.clas);  
 if(st.roll==st1.roll&&(strcmp(st.clas,st1.clas))==0)  
 {  
 printf("\nALREADY PRESENT");  
 getch();  
 i++;  
 break;  
 }  
 }  
 fclose(fp);  
 fp=fopen(filename,"a");  
 if(i==0)  
 {  
 st=alloc(st);  
 fprintf(fp,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 fclose(fp);  
 }  
 void display()  
 {  
 FILE *fp;  
 char filename[20];  
 int i=0;  
 stud st;  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 clrscr();  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 printf("\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.clas);  
 i++;  
 getch();  
 if(i%2==0)  
 clrscr();  
 }  
 fclose(fp);  
 }  
 void searchnr(char filename[])  
 {  
 FILE *fp;  
 char nam[10];  
 int r,i=0;  
 stud st;  
 fp=fopen(filename,"r");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&r);  
 printf("\nENTER NAME:");  
 scanf("%s",nam);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==r&&strcmp(st.name,nam)==0)  
 {  
 i=1;  
 printf("\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.clas);  
 getch();  
 }  
 }  
 }  
 if(i==0)  
 printf("\nNOT FOUND");  
 fclose(fp);  
 getch();  
 }  
 void searchcr(char filename[20])  
 {  
 FILE *fp;  
 char cl[10];  
 int r,i=0;  
 stud st;  
 fp=fopen(filename,"r");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&r);  
 printf("\nENTER CLASS:");  
 scanf("%s",cl);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==r&&strcmp(st.clas,cl)==0)  
 {  
 i=1;  
 printf("\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.name);  
 getch();  
 break;  
 }  
 }  
 }  
 if(i==0)  
 printf("\nNOT FOUND");  
 fclose(fp);  
 getch();  
 }  
 void modify()  
 {  
 FILE *fp,*fp1;  
 char filename[20],filename1[20],cl[10];  
 int r,i=0,rol,ch;  
 stud st;  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 strcpy(filename1,"t");  
 strcat(filename1,filename);  
 fp1=fopen(filename1,"w");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&rol);  
 printf("\nCLASS:");  
 scanf("%s",cl);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==rol&&strcmp(st.clas,cl)==0)  
 {  
 printf("\nOLD DAT IS:\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.clas);  
 getch();  
 do  
 {  
 clrscr();  
 printf("\nWHICH ENTRY U WANT TO MODIFY\n\t\t1.MARKS\n\t\t2.HOUSE NO\n\t\t3.CITY\n\t\t4.NAME\n\t\t5.EXIT");  
 printf("\nENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 while(1)  
 {  
 printf("\nENTER NEW MARKS:");  
 scanf("%d",&st.mark);  
 if(st.mark<0&&st.mark>100)  
 printf("\nINVALID MARKS");  
 else  
 break;  
 getch();  
 }  
 break;  
 case 2:  
 while(1)  
 {  
 printf("\nENTER NEW HNO:");  
 scanf("%d",&st.add.hno);  
 if(st.add.hno<0&&st.add.hno>32000)  
 printf("\nINVALID h.NO");  
 else  
 break;  
 getch();  
 }  
 break;  
 case 3:  
 printf("\nENTER CITY:");  
 scanf("%s",st.add.city);  
 break;  
 case 4:  
 printf("\nENTER NAME:");  
 scanf("%s",st.name);  
 break;  
 case 5:  
 break;  
 }  
 }while(ch!=5);  
 i=1;  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 break;  
 }  
 else  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 }  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 fclose(fp);  
 fclose(fp1);  
 if(i==0)  
 {  
 printf("\nNOT FOUND");  
 remove(filename1);  
 }  
 else  
 {  
 remove(filename);  
 rename(filename1,filename);  
 }  
 getch();  
 }  
 void delet()  
 {  
 FILE *fp,*fp1;  
 char filename[20],cl[10],filename1[20];  
 int r,i=0;  
 stud st;  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 strcpy(filename1,"t");  
 strcat(filename1,filename);  
 fp1=fopen(filename1,"w");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&r);  
 printf("\nENTER CLASS:");  
 scanf("%s",cl);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==r&&strcmp(st.clas,cl)==0)  
 {  
 i=1;  
 break;  
 }  
 else  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 }  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 fclose(fp);  
 fclose(fp1);  
 if(i==0)  
 {  
 printf("\nNOT FOUND");  
 remove("filename1");  
 }  
 else  
 {  
 remove(filename);  
 rename(filename1,filename);  
 printf("DATA IS DELETED");  
 }  
 getch();  
 }  
 stud alloc(stud st)  
 {  
 while(1)  
 {  
 printf("\nENTER MARKS:");  
 scanf("%d",&st.mark);  
 if(st.mark<0&&st.mark>100)  
 printf("\nINVALID ROLL NO");  
 else  
 break;  
 }  
 while(1)  
 {  
 printf("\nENTER HNO:");  
 scanf("%d",&st.add.hno);  
 if(st.add.hno<0&&st.add.hno>32000)  
 printf("\nINVALID ROLL NO");  
 else  
 break;  
 }  
 printf("\nENTER CITY:");  
 scanf("%s",st.add.city);  
 printf("\nENTER NAME:");  
 scanf("%s",st.name);  
 getch();  
 return st;  
 }

 

 

Other Projects to Try:

  1. string operations such as Copy, Length, Reversing, Palindrome, Concatenation
  2. Hash table code in C Language
  3. Queue Operations using C Language
  4. Stack Operations using C Language
  5. Matrix Operations in C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Hoffmans algorithm in C Language

April 5, 2011 by ProjectsGeek Leave a Comment

Hoffmans algorithm in C

Implement Huffmans algorithm in C Language.

Hoffmans algorithm in C Language Code

 

 
/*Hoffmans algorithm in C Language*/
 #include  
 #include  
 #include  
 #define MAX 26  
 typedef struct str  
 {  
 int freq;  
 char alpha;  
 }str;  
 typedef struct huff  
 {  
 struct str info;  
 struct huff *lc,*rc;  
 }huff;  
 typedef struct sll  
 {  
 int flag;  
 union data  
 {  
 struct huff * addr;  
 struct str info;  
 }data;  
 struct sll *link;  
 }sll;  
 sll *insert(sll *,huff*,sll *);  
 void insertsort(str *,int );  
 sll * createsll(str *);  
 sll * input(str*);  
 huff * huffman(sll *,huff *);  
 void itpost(huff *);  
 huff *freepa(huff *);  
 void leveldis(huff *f);  
 void prefix(huff *,int [],int );  
 void main()  
 {  
 int ch,a[25],i;  
 str *s;  
 sll *f=NULL;  
 huff *pa=NULL;  
 s=(str *)calloc(1,26*sizeof(str));  
 do  
 {  
 clrscr();  
 printf("\n\t\tHUFFMAN TREE CREATION\n\t\t1.INPUT\n\t\t2.HUFFMAN TREE\n\t\t3.DISPLAY\n\t\t4.PREFIX CODE\n\t\t5.LEVEL VISE DISPLAY\n\t\t6.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 printf("INPUT");  
 f=input(s);  
 break;  
 case 2:  
 if(pa!=NULL)  
 pa=freepa(pa);  
 printf("HUFFMAN TREE");  
 pa=huffman(f,pa);  
 printf("HUFFMAN TREE IS CREATED");  
 break;  
 case 3:  
 if(pa==NULL)  
 printf("MAKE HUFFMAN TREE FIRST");  
 else  
 {  
 printf("DISPLAY");  
 itpost(pa);  
 }  
 break;  
 case 4:  
 if(pa==NULL)  
 printf("MAKE HUFFMAN TREE FIRST");  
 else  
 {  
 printf("PREFIX CODE");  
 i=-1;  
 prefix(pa,a,i);  
 getch();  
 }  
 break;  
 case 5:  
 if(pa==NULL)  
 printf("MAKE HUFFMAN TREE FIRST");  
 else  
 {  
 printf("LEVEL VISE DISPLAY");  
 leveldis(pa);  
 }  
 break;  
 }  
 getch();  
 }while(ch!=6);  
 }  
 sll* input(str *s)  
 {  
 sll *f=NULL;  
 char st[100];  
 int k=0,j,i;  
 for(i=0;i<max;i++) <br=""> (s+i)->freq=0;  
 flushall();  
 gets(st);  
 for(i=0;st[i]!=NULL;i++)  
 {  
 if((64<st[i]&&st[i]<91)||(96<st[i]&&st[i]<123)) <br=""> {  
 for(j=0;j<k;j++) <br=""> {  
 if(st[i]==(s+j)->alpha)  
 break;  
 }  
 if(j==k||i==0)  
 {  
 (s+k)->alpha=st[i];  
 (s+k)->freq++;  
 k++;  
 }  
 else  
 {  
 (s+j)->freq++;  
 }  
 }  
 }  
 insertsort(s,k);  
 f=createsll(s);  
 return f;  
 }  
 sll * createsll(str *s)  
 {  
 sll *nw,*t,*f=NULL;  
 int i;  
 for(i=0;(s+i)->freq!=0;i++)  
 {  
 nw=(sll *)malloc(sizeof(sll));  
 nw->flag=0;  
 nw->link=NULL;  
 nw->data.info.freq=(s+i)->freq;  
 nw->data.info.alpha=(s+i)->alpha;  
 nw->link=NULL;  
 if(f==NULL)  
 f=nw;  
 else  
 {  
 t=f;  
 while(t->link!=NULL)  
 t=t->link;  
 t->link=nw;  
 }  
 }  
 return f;  
 }  
 void insertsort(str *a,int n)  
 {  
 int i,j;  
 str temp;  
 for(i=1;i<n;i++) <br=""> {  
 temp.freq=(a+i)->freq;  
 temp.alpha=(a+i)->alpha;  
 for(j=i-1;j>=0&&(a+j)->freq>temp.freq;j--)  
 {  
 (a+j+1)->freq=(a+j)->freq;  
 (a+j+1)->alpha=(a+j)->alpha;  
 }  
 (a+j+1)->freq=temp.freq;  
 (a+j+1)->alpha=temp.alpha;  
 }  
 }  
 huff * huffman(sll *f,huff *pa)  
 {  
 huff *q=NULL,*nw,*lch,*rch,*p;  
 sll *t,*s;  
 while(f!=NULL)  
 {  
 if(f->flag==0)  
 {  
 nw=(huff *)malloc(sizeof(huff));  
 nw->info=f->data.info;  
 nw->lc=nw->rc=NULL;  
 lch=nw;  
 }  
 else  
 lch=f->data.addr;  
 t=f;  
 f=f->link;  
 free(t);  
 if(f!=NULL)  
 {  
 if(f->flag==0)  
 {  
 nw=(huff *)malloc(sizeof(huff));  
 nw->info=f->data.info;  
 nw->lc=nw->rc=NULL;  
 rch=nw;  
 }  
 else  
 rch=f->data.addr;  
 t=f;  
 f=f->link;  
 nw=(huff *)malloc(sizeof(huff));  
 nw->info.freq=lch->info.freq+rch->info.freq;  
 nw->lc=lch;  
 nw->rc=rch;  
 p=nw;  
 p->info.alpha='\0';  
 t->flag=1;  
 t->data.addr=p;  
 t->link=NULL;  
 f=insert(f,p,t);  
 }  
 else  
 p=lch;  
 }  
 q=p;  
 free(t);  
 return(q);  
 }  
 sll *insert(sll *f,huff*p,sll *t)  
 {  
 sll *s;  
 s=f;  
 if(f!=NULL)  
 {  
 if(((p->info.freqdata.info.freq)&&f->flag==0)||((p->info.freqdata.addr->info.freq)&&f->flag==1))  
 {  
 t->link=f;  
 f=t;  
 }  
 else  
 {  
 while(s->link!=NULL)  
 {  
 if(s->link->data.info.freq>t->data.addr->info.freq&&s->link->flag==0)  
 {  
 t->link=s->link;  
 break;  
 }  
 else if(s->link->data.addr->info.freq>t->data.addr->info.freq&&s->link->flag==1)  
 {  
 t->link=s->link;  
 break;  
 }  
 s=s->link;  
 }  
 }  
 }  
 s->link=t;  
 if(s->link==NULL)  
 t->link=NULL;  
 return f;  
 }  
 /*void itpost(huff *root)  
 {  
 huff *t;  
 stack *top,*nw;  
 top=NULL;  
 t=root;  
 while(t!=NULL||top!=NULL)  
 {  
 if(t!=NULL)  
 {  
 nw=(stack*)malloc(sizeof(stack));  
 nw->ad=t;  
 nw->next=top;  
 nw->flag=0;  
 top=nw;  
 t=t->lc;  
 }  
 else  
 {  
 t=top->ad;  
 if(top->flag==0)  
 {  
 top->flag=1;  
 t=t->rc;  
 }  
 else  
 {  
 if(t->lc==NULL&&t->rc==NULL)  
 printf("%c",t->info.alpha);  
 else  
 printf("%d",t->info.freq);  
 nw=top;  
 top=top->next;  
 free(nw);  
 t=NULL;  
 }  
 }  
 }  
 getch();  
 } */  
 huff *freepa(huff *t)  
 {  
 huff *q,*n;  
 if(t!=NULL)  
 {  
 q=t->lc;  
 n=t->rc;  
 if(q->lc==NULL&&q->rc==NULL)  
 q=freepa(q);  
 if(n->lc==NULL&&n->rc==NULL)  
 n=freepa(n);  
 }  
 return q;  
 }  
 void leveldis(huff *f)  
 {  
 huff *queue[MAX],*t;  
 int front=-1,rear=-1,i=-1,pl=0,cl=1;  
 t=f;  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t;  
 while(cl!=0||pl!=0)  
 {  
 if(pl==0)  
 {  
 i++;  
 printf("\nELEMENT IN %d LEVEL:",i);  
 pl=cl;  
 cl=0;  
 }  
 t=queue[front];  
 if(front==rear)  
 front=rear=-1;  
 else  
 front=(front+1)%MAX;  
 if(t->info.alpha!='\0')  
 printf("\t%c",t->info.alpha);  
 else  
 printf("\t%d",t->info.freq);  
 if(t->lc!=NULL)  
 {  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t->lc;  
 cl++;  
 }  
 if(t->rc!=NULL)  
 {  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t->rc;  
 cl++;  
 }  
 pl--;  
 }  
 getch();  
 }  
 void prefix(huff *f,int a[],int i)  
 {  
 huff *t;  
 int m;  
 t=f;  
 i++;  
 if(t!=NULL)  
 {  
 if(t->lc==NULL&&t->rc==NULL)  
 {  
 printf("\nPREFIX OF %c:",t->info.alpha);  
 for(m=0;m<i;m++) <br=""> printf("\t%d",a[m]);  
 printf("\tFREQUENCY:%d",t->info.freq);  
 }  
 if(t->lc!=NULL)  
 {  
 a[i]=0;  
 prefix(t->lc,a,i);  
 }  
 if(t->rc!=NULL)  
 {  
 a[i]=1;  
 prefix(t->rc,a,i);  
 }  
 }  
 }

 

 

Other Projects to Try:

  1. Expression Tree using C Language
  2. Breadth First and Depth First Search C Language
  3. Stack Operations using C Language
  4. Dijkstra Algorithm in C Language
  5. BFS AND DFS Algorithm using C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • 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