Loading

Archive for 10/01/2012 - 11/01/2012

C Program to Remove Comments from C Program File

C - Program to Remove Comments (Single and MultiLine)  from a C - Program File




#include<stdio.h>
#include<conio.h>

void main()
{
  FILE *p;
  char *c,s;
  int i=0,j,k;
  p=fopen("filename.c","r");
  if(p==NULL)
  {
    printf("\Cannot Open Input File");
  }

  s=fgetc(p);
  while(!feof(p))
  {

    if(s=='/')
    {
      s=fgetc(p);
      if(s=='/')
    while((s=fgetc(p))!='\n')
    ;
      else if(s=='*')
    while((s=fgetc(p))!='/')
    ;
      else
      ;

    }
    else
    {
    c[i]=s;
    i++;

    }
    s=fgetc(p);
  }

  c[i]='\0';

  fclose(p);
  p=fopen("filename.c","w");
  i=0;
  while(c[i]!='\0')
  {
    fputc(c[i],p);
    i++;
  }
  fclose(p);

}

Posted in | Leave a comment

C - Program for Shaker Sort


C - Program for Shaker Sort




#include<stdio.h>
#include<conio.h>
 
void main()
{
          int arr[10]={100,25,88,13,76,31,45,68,94,53};
          int current,sorted,walker1,walker2,temp,i;
          clrscr();
          current=0;
          sorted=0;
          printf("The original array is:\n----------------------\n");
          for(i=0;i<10;i++)
          printf("%d ",arr[i]);
          while(current<3 && sorted==0)
          {
                   walker1=9;
                   walker2=current;
                   sorted=1;
                   while(walker1>current)
                   {
                             if(arr[walker1]<arr[walker1-1])
                             //Least value bubbles to the beginning of the array
                             {
                                      sorted=0;
                                      temp=arr[walker1];
                                      arr[walker1]=arr[walker1-1];
                                      arr[walker1-1]=temp;
                             }
                             if(arr[walker2]>arr[walker2+1])
                             //Greatest value bubble to the end of the array
                             {
                                      sorted=0;
                                      temp=arr[walker2];
                                      arr[walker2]=arr[walker2+1];
                                      arr[walker2+1]=temp;
                             }
                             walker2++;
                             walker1--;
                   }
                   current++;
                   printf("\n\n\n\nAfter %d pass of Shaker Sort:\n\n",current);
                   for(i=0;i<10;i++)
                             printf("%d ",arr[i]);
          }
          getch();
}

Posted in | 1 Comment

C - Program to Reverse Words in a Sentence

C - Program to Reverse Words in a Sentence


#include<stdio.h>
#include<conio.h>

void main()
{
  int i,j,k;
  char s[100],c;
  clrscr();

  printf("\n\nEnter a Sentence :: ");
  gets(s);

  i=0;
  while(s[i]!='\0')
  {
    j=k=i;
    if(s[i]==32)
    {
    i++;
    continue;
    }
    while(s[i]!=32)
    {
    i++;
    if(s[i]=='\0')
    break;
    }
    j=i-1;
    while(k<=j)
    {
    c=s[k];
    s[k]=s[j];
    s[j]=c;
    k++;
    j--;

    }
  }
  j=0;
  i--;
  while(j<=i)
  {
    c=s[j];
    s[j]=s[i];
    s[i]=c;
    j++;
    i--;
  }

  printf("\n\nReverse :: %s",s);
  getch();
}

Posted in | 1 Comment

C - Program cprintf Example


cprintf Example


#include <conio.h>

int main(void)
{
   /* clear the screen */
   clrscr();

   /* create a text window */
   window(10, 10, 80, 25);

   /* output some text in the window */
   cprintf("Hello world\r\n");

   /* wait for a key */
   getch();
   return 0;
}

Posted in | Leave a comment

C - Program Putch Example


Putch Example



#include <stdio.h>
#include <conio.h>

int main(void)
{
   char ch = 0;

   printf("Input a string:");
   while ((ch != '\r'))
   {
      ch = getch();
      putch(ch);
   }
   return 0;
}

Posted in | Leave a comment

C - Program - Getch Example

Getch Example




#include <conio.h>
#include <stdio.h>

int main(void)
{
  int c;
  int extended = 0;
  c = getch();
  if (!c)
    extended = getch();
  if (extended)
    printf("The character is extended\n");
  else
    printf("The character isn't extended\n");

  return 0;
}

Posted in | Leave a comment

C - Program - Getche Example

Getche Example



#include <stdio.h>
#include <conio.h>

int main(void)
{
   char ch;

   printf("Input a character:");
   ch = getche();
   printf("\nYou input a '%c'\n", ch);
   return 0;
}

Posted in | Leave a comment

C- Program - Putc Example


Putc Example


#include <stdio.h>

int main(void)
{
   char msg[] = "Hello world\n";
   int i = 0;

   while (msg[i])
      putc(msg[i++], stdout);
   return 0;
}

Posted in | Leave a comment

C - Program - Getc Example

Getc Example


#include <stdio.h>

int main(void)
{
   char ch;

   printf("Input a character:");
/* read a character from the standard input stream */
   ch = getc(stdin);
   printf("The character input was: '%c'\n", ch);
   return 0;
}

Posted in | Leave a comment

C - Program to Implement Sequential File - File Handling

C - Program to Implement Sequential File (File Handling)


#include <stdio.h>
#include <conio.h>

typedef struct student
{
    int rollno;
    char name[20];
    float marks;
    int status;
}student;

void create();
void read();
void insert(student rec1);
int Delete(int rollno);
int search(int rollno);
void pack();
void update();

void main()
 {
    FILE *master;
    int rollno,op,recno;
    student rec1;
    create();
    do
     {
        printf("\n\n1)Read(Display)\n2)Insert\n3)Delete\n4)Update");
        printf("\n5)Search\n6)Pack\n7)Quit");
        printf("\nEnter Your Choice:");
        scanf("%d",&op);
        switch(op)
          {
            case 1: read();break;
            case 2: printf("\nEnter a record to be inserted(roll no,name,marks) : ");
                scanf("%d%s%f",&rec1.rollno,rec1.name,&rec1.marks);
                insert(rec1);
                break;
            case 3: printf("\nEnter the roll no.:");
                scanf("%d",&rollno);
                Delete(rollno);
                break;
            case 4: update(); break;
            case 5: printf("\nEnter a roll no. : ");
                scanf("%d",&rollno);
                recno=search(rollno);
                if(recno>=0)
                  {
                    printf("\n Record No.: %d",recno);
                    master=fopen("master.txt","rb");
                    fseek(master,recno*sizeof(student),0);
                    fread(&rec1,sizeof(student),1,master);
                    printf("\n%d  %s   %5.2f",rec1.rollno,rec1.name,rec1.marks);
                  }
                else
                    printf("\nRecord Not Found ");
                break;
            case 6: pack();break;
          }
      }while(op!=7);
 }


void create()
 {
    FILE *master;
    if(!(master=fopen("master.txt","rb")))
    master=fopen("master.txt","wb");
    close(master);
 }
void read()
{
    FILE *master;
    student crec;
    int i=1,n;
    master=fopen("master.txt","r+b");
    fseek(master,0,2);/*go to the end of file */
    n=ftell(master)/sizeof(student);//No. of records
    rewind(master);
    for(i=1;i<=n;i++)
       {
        fread(&crec,sizeof(student),1,master);
        if(crec.status==0)//if the record is not logically deleted
            printf("\n%d) %d\t%s\t%7.2f",i,crec.rollno,crec.name,crec.marks);
        else
            printf("\n%d)      ****** deleted  *********",i);
       }
}

void insert(student rec1)
{
    FILE *master;
    student crec;
    int n,i,k;
    master=fopen("master.txt","r+b");
    rec1.status=0;
    fseek(master,0,2);/*go to the end of file */
    n=ftell(master)/sizeof(student);//No. of records
    if(n==0)//empty file
      {
        fwrite(&rec1,sizeof(student),1,master);
        fclose(master);
        return;
      }
 /* Shift records until the point of insertion */
    i=n-1;
    while(i>=0)
       {
        fseek(master,i*sizeof(student),0);
        flushall();
        fread(&crec,sizeof(student),1,master);
        if(crec.rollno>rec1.rollno)
           {
            fseek(master,(i+1)*sizeof(student),0);
            fwrite(&crec,sizeof(student),1,master);
           }
        else
            break;

        i--;
        }
    flushall();
/*insert the record at (i+1)th position */
    i++;
    printf("\ni=%d",i);
    fseek(master,i*sizeof(student),0);
    fwrite(&rec1,sizeof(student),1,master);
    fclose(master);
}

int Delete(int rollno)
{
    FILE *master;
    student crec;
    int i,n;
    master=fopen("master.txt","r+b");
    fseek(master,0,2);/*go to the end of file */
    n=ftell(master)/sizeof(student);
    rewind(master);
    for(i=0;i<n;i++)
       {
        fread(&crec,sizeof(student),1,master);
        if(crec.status==0)
           {
            if(crec.rollno>rollno)
              {
                printf("\nRecord does not exist ...");
                close(master);
                return(0);
              }
            if(crec.rollno==rollno)
              {
                crec.status=1;
                fseek(master,i*sizeof(student),0);
                fwrite(&crec,sizeof(student),1,master);
                fclose(master);
                return(1);
              }
           }
       }
  return(0);
}

int search(int rollno)
{
    FILE *master;
    student crec;
    int i,n;
    master=fopen("master.txt","r+b");
    fseek(master,0,2);/*go to the end of file */
    n=ftell(master)/sizeof(student);
    rewind(master);
    for(i=0;i<n;i++)
       {
        fread(&crec,sizeof(student),1,master);
        if(crec.status==0)
           {
            if(crec.rollno>rollno)
               {
                fclose(master);
                return(-1);
               }
            if(crec.rollno==rollno)
               {
                fclose(master);
                return(i);
               }
           }
       }
  return(-1);
}

void pack()
{
    FILE *master,*temp;
    student crec;
    int i,n;
    master=fopen("master.txt","rb");
    temp=fopen("temp.txt","wb");//temporary file to copy the remaining records
    fseek(master,0,2);/*go to the end of file */
    n=ftell(master)/sizeof(student);
    rewind(master);
    for(i=0;i<n;i++)
       {
        fread(&crec,sizeof(student),1,master);
        if(crec.status==0)
            fwrite(&crec,sizeof(student),1,temp);
       }
    fclose(master);
    fclose(temp);
    //copy records back from temp file to master
    temp=fopen("temp.txt","rb");
    master=fopen("master.txt","wb");
    fseek(temp,0,2);/*go to the end of file */
    n=ftell(temp)/sizeof(student);
    rewind(temp);
    for(i=0;i<n;i++)
       {
        fread(&crec,sizeof(student),1,temp);
        fwrite(&crec,sizeof(student),1,master);
       }
    fclose(master);
    fclose(temp);
}
void update()
{
    int rollno;
    student rec1;
    printf("\n Enter the rollno of the record to be updated : ");
    scanf("%d",&rollno);
    printf("\nEnter a new record(roll no. name marks) : ");
    scanf("%d%s%f",&rec1.rollno,rec1.name,&rec1.marks);
    if(Delete(rollno))
        insert(rec1);
    else
        printf("\n Record not found :");
 }

Click Here to Download Source Code with Executable Program

Posted in , | 1 Comment

C - Program for Expression Conversion infix to postfix prefix

C - Program for Expression Conversion

 

/* C - Program for conversion of :
             1. infix to its postfix form
             2. infix to its prefix form
             3. Evaluation of postfix expression
   operators supported '+,-,*,/,%,^,(,)
   operands supported -- all single character operands
*/

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 50

typedef struct node
 {
    int data;
    struct node *next;
 }node;

int  precedence(char);
void init(node **);
int  empty(node *);
int  pop(node **);
void push(node **,int );
int  top(node *); //value of the top element
void infix_to_prefix(char infix[],char prefix[]);
void infix_to_postfix(char infix[],char postfix[]);
void eval_postfix(char postfix[]);
int  evaluate(char x,int op1,int op2);

void main()
 {
    char infix[30],postfix[30],prefix[30];
    clrscr();
    printf("\nEnter an infix expression : ");
    gets(infix);
    infix_to_postfix(infix,postfix);
    infix_to_prefix(infix,prefix);
    printf("\nPostfix : %s\nprefix: %s  ",postfix,prefix);
    printf("\nPostfix evaluation : ");
    eval_postfix(postfix);
    getch();
 }

void infix_to_prefix(char infix[],char prefix[])
  {
    int i,j;
    char temp,in1[30];
    // reverse the infix expression  and store it in in1[]
    for(i=strlen(infix)-1,j=0;i>=0;i--,j++)
        in1[j]=infix[i];
    in1[j]='\0';
    // reverse the direction of brackets
    for(i=0;in1[i]!='\0';i++)
       {
        if(in1[i]=='(')
            in1[i]=')';
        else
            if(in1[i]==')')
                in1[i]='(';
       }
    // convert from infix to postfix
    infix_to_postfix(in1,prefix);
    //reverse the final expression
    for(i=0,j=strlen(prefix)-1;i<j;i++,j--)
      {
        temp=prefix[i];
        prefix[i]=prefix[j];
        prefix[j]=temp;
      }
 }
void infix_to_postfix(char infix[],char postfix[])
{
    node *head;
    char x;
    int i,j;//i-index for infix[],j-index for postfix
    char token;
    init(&head);
    j=0;
    for(i=0;infix[i]!='\0';i++)
      {
        token=infix[i];
        if(isalnum(token))
            postfix[j++]=token;
        else
            if(token == '(')
                push(&head,'(');
            else
                if(token == ')')
                    while((x=pop(&head))!='(')
                        postfix[j++]=x;
                else
                  {
                    while(precedence(token)<=precedence(top(head)) && !empty(head))
                      {
                        x=pop(&head);
                        postfix[j++]=x;
                      }
                    push(&head,token);
                  }
    }
    while(!empty(head))
      {
        x=pop(&head);
        postfix[j++]=x;
       }
    postfix[j]='\0';
}
void eval_postfix(char postfix[])
 {
    node *head;
    char x;
    int op1,op2,val,i;
    init(&head);
    for(i=0;postfix[i]!='\0';i++)
     {      x=postfix[i];
        if(isalpha(x))
             {  printf("\nEnter the value of %c : ",x);
            scanf("%d",&val);
            push(&head,val);
             }
        else
        {       //pop two operands and evaluate
            op2=pop(&head);
            op1=pop(&head);
            val=evaluate(x,op1,op2);
            push(&head,val);
           }
    }
    val=pop(&head);
    printf("\nvalue of expression = %d",val);

}

int evaluate(char x,int op1,int op2)
{
    if(x=='+')  return(op1+op2);
    if(x=='-')  return(op1-op2);
    if(x=='*')  return(op1*op2);
    if(x=='/')  return(op1/op2);
    if(x=='%')  return(op1%op2);

}




int precedence(char x)
{
    if(x == '(')                         return(0);
    if(x == '+' || x == '-')             return(1);
    if(x == '*' || x == '/' || x == '%') return(2);
    return(3);
}

void init(node **head)
{
    *head=NULL;
}

int empty(node *head)
{
    if(head==NULL)
        return(1);
    return(0);
}


void push(node **head,int x)
{
    node *p;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=*head;
    *head=p;
}

int pop(node **head)
{
    int x;
    node *p;
    p=*head;
    *head=p->next;
    x=p->data;
    free(p);
    return(x);
}

int top(node  *head)
{
    return(head->data);
}

Click Here to Download Source Code with Executable Program



 

Posted in , , , | Leave a comment

C - Program to Implement Sparse Matrix , Simple Transpose and Fast Transpose

C - Program to Implement Sparse Matrix


/*Represent sparse matrix using array and perform matrix addition,simple transpose and fast transpose */

#include<stdio.h>
#include<conio.h>
#define MAX 20
void printsparse(int[][3]);
void readsparse(int[][3]);
void transpose(int[][3],int[][3]);
void Fast_transpose(int B1[MAX][3],int B2[MAX][3]);
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);
void main()
{
    int b1[MAX][3],b2[MAX][3],m,n,b3[MAX][3],op;
    clrscr();
    do
      {
        printf("\n1)Read the First Sparse Matrix");
        printf("\n2)Read the second sparse matrix");
        printf("\n3)Display the first matrix");
        printf("\n4)Display the second matrix");
        printf("\n5)Addition of two matrices");
        printf("\n6)Simple transpose of the first matrix");
        printf("\n7)Fast transpose of the first matrix");
        printf("\n8)Quit");
        printf("\nEnter your choice : ");
        scanf("%d",&op);
        switch(op)
         {
        case 1: readsparse(b1);break;
        case 2: readsparse(b2);break;
        case 3: printsparse(b1);break;
        case 4: printsparse(b2);break;
        case 5: addsparse(b1,b2,b3);printsparse(b3);break;
        case 6: transpose(b1,b3);printsparse(b3);break;
        case 7: Fast_transpose(b1,b3);printsparse(b3);break;
         }
     }while(op!=8);
}
void readsparse(int b[MAX][3])
{
    int i,t,m,n;

    printf("\n Enter the size of matrix (rows,columns)");
    scanf("%d%d",&m,&n);
    b[0][0]=m;
    b[0][1]=n;

    printf("\nEnter no. of non-zero elements:");
    scanf("%d",&t);
    b[0][2]=t;
    for(i=1;i<=t;i++)
    {
        printf("\n Enter the next triple(row,column,value) :");
        scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
    }
}
void printsparse(int b[MAX][3])
{
    int i,n;
    n=b[0][2];   //no of 3-triples
    printf("\nrows = %d\tcolumns = %d",b[0][0],b[0][1]);
    printf("\n");
    for(i=1;i<=n;i++)
        printf("%d\t%d\t%d\n",b[i][0],b[i][1],b[i][2]);
}
void transpose(int b1[][3],int b2[][3])
{
    int i,j,k,n;
    b2[0][0]=b1[0][1];
    b2[0][1]=b1[0][0];
    b2[0][2]=b1[0][2];
    k=1;
    n=b1[0][2];
    for(i=0;i<b1[0][1];i++)
        for(j=1;j<=n;j++)
    /* if a column number of current triple == i
       then insert the current triple in b2 */
        if(i== b1[j][1])
        {
            b2[k][0]=i;
            b2[k][1]=b1[j][0];
            b2[k][2]=b1[j][2];
            k++;
        }
}

void Fast_transpose(int B1[MAX][3],int B2[MAX][3])
{
    int m,n,t,i,col_num,location;
    int total[MAX],index[MAX];
    m=B1[0][0];n=B1[0][1];t=B1[0][2];
    B2[0][0]=n;B2[0][1]=m;B2[0][2]=t;
    for(i=0;i<n;i++)
        total[i]=0;
    for(i=1;i<=t;i++)
    {
        col_num=B1[i][1];
        total[col_num]++;
    }
    index[0]=1;
    for(i=1;i<n;i++)
        index[i]=index[i-1]+total[i-1];

    for(i=1;i<=t;i++)
    {
        col_num=B1[i][1];
        location=index[col_num];
        index[col_num]++;
        B2[location][0]=B1[i][1];
        B2[location][1]=B1[i][0];
        B2[location][2]=B1[i][2];
    }
}

void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
    int t1,t2,i,j,k;
    t1=b1[0][2];
    t2=b2[0][2];
    i=j=k=0;
    b3[0][0]=b1[0][0];
    b3[0][1]=b1[0][1];
    while(i<=t1 && j<=t2)
    {
        if(b1[i][0] < b2[j][0])
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            k++;
            i++;
            continue;//go to  end of the loop
        }
        if(b2[j][0] < b1[i][0])
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b2[j][1];
            b3[k][2]=b2[j][2];
            k++;
            j++;
            continue;//go to end of the loop
        }
        if(b1[i][1] < b2[j][1])
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            k++;
            i++;
            continue;//go to end of the loop
        }
        if(b2[j][1] < b1[i][1])
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b2[j][1];
            b3[k][2]=b2[j][2];
            k++;
            j++;
            continue; //go to end of the loop
        }
//else add the two tuples
        b3[k][0]=b1[i][0];
        b3[k][1]=b1[i][1];
        b3[k][2]=b1[i][2]+b2[j][2];
        k++;
        i++;
        j++;

     }//end of loop
        while(i<=t1)
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            i++;
            k++;
        }
        while(j<=t2)
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b1[j][1];
            b3[k][2]=b1[j][2];
            j++;
            k++;
        }
        b3[0][2]=k-1;
  }

 Click Here to Download Source Code with Executable Program.

Posted in , , , , | Leave a comment

Java Visual Basic Mini Projects

Visual Basic Mini Projects

Java Mini Projects

Posted in | Leave a comment

C - Program to Implement Various Set Operations - Union, Intesection

C - Program to Implement Various Set Operations

/*  Operations covered :
      1) Create()     : for creating a new set with initial members
                of the set
      2) print()      : diaplays all members of the set
      3) Union()      : finds union of two sets, set1[] and set2[]  and
                stores the result in set3[]
      4) intersection() : finds intersection of two sets, set1[] and set2[]
                and stores the result in set3[]
      5) difference() :finds difference of two sets, set1[] and set2[]
               and stores the result in set3[]
      6) member()     :function returns 1 or 0 ,depending onwhether the
              element  x belongs or not to a  set.

      7) symmdiff()  : Finds Symmetric difference of two sets
Representation of a set
-----------------------
      A set is representrd using an  array of integers.
      It may be declared as:
           int set[30];
      set[0] - gives number of elements in a set.
      set[1] to set[29] are for storing set members.

      Example :
           A set,[2,11,3,5 6],when represebted will appear as:
           [5][2][3][5][6][11][ ][ ][ ] <--- array set[]
            0  1  2  3  4  5   6  7  8  <--- index
*/

#define MAX 30
#include<stdio.h>
#include<conio.h>
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set3[]);
void difference(int set1[],int set2[],int set3[]);
void symmdiff(int set1[],int set2[],int set3[]);
int member(int set[],int x);


void main()
{ int set1[MAX],set2[MAX],set3[MAX];
  int x,op;
  clrscr();
  flushall();
  set1[0]=set2[0]=set3[0]=0;
  do
   { printf("\n1)Create\n2)Print\n3)Union\n4)Intersection\n5)Difference");
     printf("\n6Symmetrec Difference \n7)Quit");
     printf("\nEnter Your Choice:");
     scanf("%d",&op);
     switch(op)
      {
    case 1: printf("\nCreting First Set*******");
        create(set1);
        printf("\nCreating Second Set*****");
        create(set2);
        break;
    case 2: printf("\nFirst Set :\n");
        print(set1);
        printf("\n\nSecond Set :\n");
        print(set2);
        printf("\n\nThird Set :\n");
        print(set3);
        break;
    case 3: Union(set1,set2,set3);print(set3);break;
    case 4: intersection(set1,set2,set3);print(set3);break;
    case 5: difference(set1,set2,set3);print(set3);break;
    case 6: symmdiff(set1,set2,set3);print(set3);break;
     }
  printf("\npress a key............");
  getch();
  }while(op!=7);
 }

 /*creates set[] with initial elements*/

 void create(int set[])
   {   int n,i,x;
       set[0]=0;/*make it a null set*/
       printf("\n No. of elements in the set:");
       scanf("%d",&n);
       printf("\n enter set elements :");
       for(i=1;i<=n;i++)
       scanf("%d",&set[i]);
       set[0]=n; //Number of elements.

   }

 void  print(int set[])
  { int i,n;
    n=set[0];/* number of elements in the set */
    printf("\Members of the set :-->");
    for(i=1;i<=n;i++)
       printf("%d  ",set[i]);
  }

 /* union of  set1[] and set2[] is stored in set3[]*/

void Union(int set1[],int set2[],int set3[])
  { int i,n;
    /* copy set1[] to set3[]*/
    set3[0]=0;/*make set3[] a null set */
    n=set1[0];/* number of elements in the set*/
    //Union of set1,set2= set1 + (set2-set1)
    for(i=0;i<=n;i++)
    set3[i]=set1[i];

    n=set2[0];
    for(i=1;i<=n;i++)
       if(!member(set3,set2[i]))
        set3[++set3[0]]=set2[i];  // insert and increment no. of elements
   }

 /*function returns 1 or 0 depending on whether x belongs
  to set[] or not */

 int member(int set[],int x)
  { int i,n;
    n=set[0]; /* number of elements in the set*/
    for(i=1;i<=n;i++)
      if(x==set[i])
     return(1);

     return(0);
  }

/*intersection of set1[] and set2[] is stored in set3[]*/

void intersection(int set1[],int set2[],int set3[])
     {
    int i,n;
    set3[0]=0; /* make a NULL set*/
    n=set1[0];/* number of elements in the set*/
    for(i=1;i<=n;i++)
      if(member(set2,set1[i])) /* all common elements are inserted in set3[]*/
           set3[++set3[0]]=set1[i];  // insert and increment no. of elements
     }

/*difference of set1[] and set2[] is stored in set3[]*/

void difference(int set1[],int set2[],int set3[])
      { int i,n;
    n=set1[0];/* number of elements in the set*/
    set3[0]=0;/*make it a null set*/
    for(i=1;i<=n;i++)
       if(!member(set2,set1[i]))
         set3[++set3[0]]=set1[i];  // insert and increment no. of elements
      }

 void symmdiff(int set1[],int set2[],int set3[])
      { int i,n;
    n=set1[0];/* number of elements in the set*/
    set3[0]=0;/*make it a null set*/
    //Calculate set1-set2
    for(i=1;i<=n;i++)
       if(!member(set2,set1[i]))
         set3[++set3[0]]=set1[i];  // insert and increment no. of elements
    //Calculate set2-set1
    n=set2[0];
    for(i=1;i<=n;i++)
       if(!member(set1,set2[i]))
         set3[++set3[0]]=set2[i];  // insert and increment no. of elements

      }

 Click Here to Download Source Code with Executable Program.

Posted in , , , | Leave a comment

C - Program to Implement Hashing , handle collision using linear probing with chaining and with replacement

C - Program to Implement Hashing , handle collision  using linear probing with chaining and with replacement

 

#include <stdio.h>
#include <conio.h>
#define SIZE 10              /* size of the hash table*/
#define FALSE 0
#define TRUE 1
#define h(x) x%SIZE         /*hashing function */



void insert( int data[],int flag[],int chain[],int x);
int search(int data[],int flag[],int chain[],int x);
void print(int data[],int flag[],int chain[]);


void main()
 {
    int data[SIZE],flag[SIZE],chain[SIZE],i,j,x,op,loc;
    /* array data[]  - is a hash table
       array flag[]  - if flag[i] is 1 then the ith place of the hash
               table is filled */
    for(i=0;i<SIZE;i++) /* initialize */
       {
        flag[i]=FALSE;
        chain[i]=-1;
       }
   clrscr();
    do
      {
        flushall();
        printf("\n\n1)Insert\n2)Search\n3)Print\n4)Quit");
        printf("\nEnter Your Choice : ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: printf("\n Enter a number to be inserted:");
                scanf("%d",&x);
                insert(data,flag,chain,x);
                break;
            case 2: printf("\n Enter a number to be searched :");
                scanf("%d",&x);
                if((loc=search(data,flag,chain,x))==-1)
                    printf("\n****Element not found****");
                else
                    printf("\n***Found at the location=%d",loc);
                break;
            case 3: print(data,flag,chain);
                break;
           }
    }while(op!=4);
  }

void insert( int data[],int flag[],int chain[],int x)
{
    int i=0,j,start;
    start=h(x); /*hashed location*/
    /*Situation I, hashed location is empty*/
    if(flag[start]==0)
      {
        data[start]=x;
        flag[start]=1;
        return;
      }
 /*Situation II, hashed location does not contain a synonym*/
    if(h(data[start])!=h(x))
      { /* locate an empty location */
        i=0;j=start;
        while(flag[j] && i<SIZE)
           {
            j=(j+1)%SIZE;
            i++;
           }
        if(i==SIZE)
           {
            printf("\nTable is full ...");
            return;
           }
    /*Delete the element by modifying the chain */
        i=data[start]%SIZE;/*beginning of the chain*/
        while(chain[i] != start)
            i=chain[i];
        chain[i]=chain[start]; /*modify */
    /*add the deleted element at the end of the chain */
        while(chain[i]!=-1)
            i=chain[i];
        chain[i]=j;
        data[j]=data[start];
        chain[start]=-1;
        flag[j]=1;
        chain[j]=-1;
    /*insert the current key */
        data[start]=x;
        chain[start]=-1;
        return;
      }
    /*Situation III ,hashed location contains a synonym */
    /* locate an empty location */
    i=0;j=start;
    while(flag[j] && i<SIZE)
       {
        j=(j+1)%SIZE;
        i++;
       }
    if(i==SIZE)
       {
        printf("\nTable is full ...");
        return;
       }
    data[j]=x;
    flag[j]=1;
    chain[j]=-1;
    /*go to the end of chain */
    i=start;/*beginning of the chain*/
    while(chain[i] != -1)
        i=chain[i];
    chain[i]=j;
}

int search(int data[],int flag[],int chain[],int x)
 {
    int i=0,j;
    j=h(x); /*hashed location*/
    /*locate beginning of the chain*/
    while(i<SIZE && flag[j] && data[j]%SIZE !=x%SIZE)
       {
        i++;
        j=(j+1)%SIZE;
       }
    if(!flag[j] || i==SIZE)
        return(-1);
    /*locate the element in the chain */
    while(j!=-1)
       {
        if(data[j]==x)
            return(j);
        j=chain[j];
       }
    return(-1);
}

void print(int data[],int flag[],int chain[])
 {
    int i;
    for(i=0;i<SIZE;i++)
        if(flag[i])
            printf("\n(%d) %d     %d",i,data[i],chain[i]);
        else
            printf("\n(%d) ---    %d",i,chain[i]);
 }

 Click Here to Download Source Code with Executable Program


Posted in , | 2 Comments

C - Program for Operations on Polynomial using a circular linked list

 C - Program for Operations on Polynomial using a circular linked list



#include <stdio.h>
#include <conio.h>
#include <math.h>

typedef struct  node
 {
    int power;
    float coeff;
    struct node *next;
 }node;

    node *  insert(node *head,int power1,float coeff1);
    node *  create();
    node *  padd(node *p1,node *p2);
    node *  pmul(node *p1,node *p2);
    float eval(node *p1,float x);
    void print(node *head);

node *insert(node *head,int power1,float coeff1)
      {
    node *p,*q;
    /*terms are stored in decreasing  order of power*/
    /*pointer is used to locate the correct place of insertion.
      pointer p is used to store the address of the node created for
     the current term.If a term with same power is found,coefficients
     are added*/
    p=(node*) malloc(sizeof(node));
    p->power=power1;
    p->coeff=coeff1;
    p->next=NULL;
    if(head==NULL) //Empty linked list
       {
        head=p;
        head->next=head;
        return(head);
       }
    if(power1<head->power)//lowest power,inserted at the end
       {
        p->next=head->next;
        head->next=p;
        head=p;
        return(head);
       }
    if(power1==head->power) //add coefficients
       {
        head->coeff=head->coeff+coeff1;
        return(head);
       }

     q=head;
     while(q->next!=head && power1<=q->next->power) //locate the postion for insertion
        q=q->next;
     if(p->power==q->power)
        q->coeff=q->coeff+coeff1;
     else
       {
        p->next=q->next;
        q->next=p;
       }
    return(head);
  }


 node *create()
   {
    int n,i,power1;
    float coeff1;
    node *head=NULL;
    printf("\nEnter No. of Terms:");
    scanf("%d",&n);
    printf("\nenter a term as a tuple of (power,coefficient) : ");
    for(i=1;i<=n;i++)
       {
        scanf("%d%f",&power1,&coeff1);
        head=insert(head,power1,coeff1);
       }
       return(head);
  }

 node * padd(node *p1,node *p2)
 {
    node *p;
    node *head=NULL;
    int power;float coeff;
    p=p1->next;
    do     //insert the first polynomial
       {
        head=insert(head,p->power,p->coeff);
        p=p->next;
       } while(p!=p1->next);
    p=p2->next;
    do  //insert the second polynomial
       {
        head=insert(head,p->power,p->coeff);
        p=p->next;
     } while(p!=p2->next);
    return(head);
  }

 node *pmul(node *p1,node *p2)
 {
    node *head1,*head2;
    node *head=NULL;
    head2=p2->next;
    do   //for every term of the second polynomial
       {
        head1=p1->next;
        do  //multiply with every term of the first polynomial
           {
 //    for(p=head1;p!=NULL;p=p->next)
            head=insert(head,head1->power+head2->power,head1->coeff * head2->coeff);
            head1=head1->next;
            }while(head1!=p1->next);
        head2=head2->next;
       }while(head2!=p2->next);
    return(head);
 }

 float eval(node *head,float x)
 {
    float value=0.00;
    node *p;
    p=head->next;
    do
      {
        value=value+p->coeff * pow(x,p->power);
        p=p->next;
      }while(p!=head->next);
    return(value);
 }

 void print( node *head)
 {
    node *p;
    p=head->next;
    printf("\n");
    do
       {
        printf("%6.2fx^%d   ",p->coeff,p->power);
        p=p->next;
       }while(p!=head->next);
 }


 void main()
 {
    node *p1,*p2,*p3;
    int op;
    float value,x;
    p1=p2=p3=NULL;
    clrscr();
    do
      {
        printf("\n1)Create first polynomial");
        printf("\n2)Create second polynomial");
        printf("\n3)Print first polynomial");
        printf("\n4)Print second polynomial");
        printf("\n5)Add\n6)Multiply\n7)Evaluate First Polynomial\n8)Quit");
        printf("\nEnter Your Choice: ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: p1=create();break;
            case 2: p2=create();break;
            case 3: print(p1);break;
            case 4: print(p2);break;
            case 5: p3=padd(p1,p2);
                print(p3);break;
            case 6: p3=pmul(p1,p2);
                print(p3);break;
            case 7: printf("\nEnter the value of X:");
                scanf("%f",&x);
                value=eval(p1,x);
                printf("\nEvaluated value = %6.2f",value);
                break;
            }
     }while(op!=8);
 }

 Click Here to Download Source Code with Executable Program

Posted in , | Leave a comment

C - Program for Doubly Linked List

C - Program for Implementation Doubly Linked List



/* Accept input as a string and construct a Doubly Linked List for the input
   string with each node contains, as a data one character from the string and
   perform :
   a) Insert b) delete c)Display forward d) Display backward
*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct node
   { char data;
     struct node *next,*prev;
   }node;

node *create();
node *insert_b(node *head,char x);
node *insert_e(node *head,char x);
node *insert_in(node *head,char x);
node *delete_b(node *head);
node *delete_e(node *head);
node *delete_in(node *head);
void displayforward(node *head);
void displaybackward(node *head);
void main()
{ int op,op1;
  char x;
  node *head=NULL;
  clrscr();
  do
    { flushall();
      printf("\n\n1)create\n2)Insert\n3)Delete");
      printf("\n4)Display forward\n5)Display backward\n6)Quit");
      printf("\nEnter your Choice:");
      scanf("%d",&op);
      switch(op)
       { case 1:head=create();break;
     case 2:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");
        printf("\nEnter your choice : ");
        scanf("%d",&op1);
        printf("\nEnter the data to be inserted : ");
        flushall();
        x=getchar();
        switch(op1)
         {  case 1: head=insert_b(head,x);
                break;
            case 2: head=insert_e(head,x);
                break;
            case 3: head=insert_in(head, x);
                break;
          }
        break;
     case 3:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");
        printf("\nEnter your choice : ");
        scanf("%d",&op1);
        switch(op1)
         {  case 1:head=delete_b(head);
               break;
            case 2:head=delete_e(head);
               break;
            case 3:head=delete_in(head);
               break;
          }
         break;
     case 4:displayforward(head);break;
     case 5:displaybackward(head);break;
       }
    }while(op!=6);
}

node *create()
{ node *head,*p;
  char x;
  head=NULL;
  printf("\n Enter a string :");
  flushall();
  while((x=getchar())!='\n')
   {
     if(head==NULL)
    {
          head=p=(node*)malloc(sizeof(node));
          head->next=head->prev=NULL;
    }
     else
       {
          p->next=(node*)malloc(sizeof(node));
          p->next->prev=p;
          p=p->next;
          p->next=NULL;
       }
    p->data=x;
   }
 return(head);
}

node *insert_b(node *head,char  x)
{   node *p;
    p=(node*)malloc(sizeof(node));
    p->data=x;
   if(head==NULL)
    {
          head=p;
          head->next=head->prev=NULL;
    }
   else
       {
          p->next=head;
          head->prev=p;
          p->prev=NULL;
          head=p;
       }
   return(head);
}
node *insert_e(node *head,char  x)
{   node *p,*q;
    p=(node*)malloc(sizeof(node));
    p->data=x;
   if(head==NULL)
    {
          head=p;
          head->next=head->prev=NULL;
    }
   else
       {
          for(q=head; q->next != NULL ; q=q->next)
          ;
          q->next=p;
          p->prev=q;
          p->next=NULL;

       }
   return(head);
}


node *insert_in(node *head,char x)
{   node *p,*q;
    char  y;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    printf("\Insert after which character ? : ");
    flushall();
    y=getchar();
    //locate the lthe data 'y'
    for(q=head ; q != NULL && q->data != y; q=q->next)
    ;
    if(q->data==y)
      {
        p->next=q->next;
        p->prev=q;
        p->next->prev=p;
        p->prev->next=p;
      }
    else
       printf("\nData not found ");
    return(head);
}
node *delete_b(node *head)
{
  node *p,*q;
  if(head==NULL)
     {
      printf("\nUnderflow....Empty Linked List");
      return(head);
     }
  if(head->next==NULL)//delete the only node
    {
     p=head;
     free(p);
     head=NULL;
    }
  else
    {
     p=head;
     head=head->next;
     head->prev=NULL;
     free(p);
    }
  return(head);

}
node *delete_e(node *head)
{
  node *p;
  if(head==NULL)
     {
      printf("\nUnderflow....Empty Linked List");
      return(head);
     }
  if(head->next==NULL)//delete the only node
    {
      p=head;
      free(p);
      head=NULL;
    }
  else
    {
      for(p=head ; p->next != NULL ; p=p->next)
      ;
      p->prev->next=NULL;
      free(p);
    }
  return(head);

}

node *delete_in(node *head)
{
  node *p,*q;
  char x;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  printf("\nEnter the data to be deleted : ");
  flushall();
  x=getchar();
  for(p=head ; p !=NULL && p->data != x ; p=p->next)
  ;

   if(p->data!=x)
     {
      printf("\nUnderflow.....data not found");
      return(head);
     }
 if(p==head)
  {
      head=head->next;
      if(head != NULL)
         head->prev=NULL;
      free(p);
  }
 else
  {
      p->prev->next=p->next;
      p->next->prev=p->prev;
      free(p);
   }
 return(head);
}

void displayforward(node *head)
{ node *p;
  printf("\n");
  if(head != NULL)
     {
     for(p=head ;p!=NULL ; p=p->next)
        printf("%c",p->data);
     }
}
void displaybackward(node *head)
{ node *p;
  printf("\n");
  if(head != NULL)
     {   // go to the last node
     for(p=head ;p->next!=NULL ; p=p->next)
     ;
     for( ;p!=NULL ; p=p->prev)
        printf("%c",p->data);
     }
}

Click Here to Download Source Code with Executable Program.


Posted in | Leave a comment