Loading

Archive for 2012

Client - Server Program in C - Socket Programming

Server File - 


#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#define MAX 30

int AddDigits(int number);
int Factorial(int number);
int StringLength(char str[]);
int StringCompare(char str[],char str1[]);
char* StrConcat(char str1[],char str2[]);
char* SubsString(char str1[],int start,int end);

int ssfd;
int ccfd,res;
int serverlen,clientlen;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
char result[MAX];

typedef struct message
{
char msg[MAX][MAX];
char msgoutput[MAX];
int input;
int ch;
int output;
}message;


void main()
{

int msgLen;
char *ch;
ssfd=socket(AF_INET,SOCK_STREAM,0);
message *msg;
message *reply;

msg=(message*)malloc(sizeof(message));
reply=(message*)malloc(sizeof(message));

printf("\nSSFD Value:%d",ssfd);

msgLen=sizeof(message);


serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");

//serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
//serveraddr.sin_port = htons(9734);

serveraddr.sin_port=9734;
serverlen=sizeof(serveraddr);
bind(ssfd,(struct sockaddr*)&serveraddr,serverlen);
printf("\nConnected to socket");
listen(ssfd,5);

ccfd=accept(ssfd,(struct sockaddr*)&clientaddr,&clientlen);
printf("\nConnection accepted successfully");
while(1)
{
read(ccfd,reply,msgLen);
switch(reply->ch)
{
case 1:
reply->output=AddDigits((int)reply->input);
write(ccfd,reply,msgLen);
break;
case 2:
reply->output=Factorial((int)reply->input);
write(ccfd,reply,msgLen);

break;
case 3:
reply->output=StringLength(reply->msg[0]);
write(ccfd,reply,msgLen);

break;
case 4:
reply->output=StringCompare(reply->msg[0],reply->msg[1]);
write(ccfd,reply,msgLen);
break;

case 5:
strcpy(reply->msgoutput,StrConcat(reply->msg[0],reply->msg[1]));
write(ccfd,reply,msgLen);
break;
case 6:
reply->output=Palindrome(reply->msg[0]);
write(ccfd,reply,msgLen);
break;

case 7:
strcpy(reply->msgoutput,SubsString(reply->msg[0],reply->input,reply->output));
write(ccfd,reply,msgLen);
break;
case 8:
system("xeyes");
break;
}

}

}
int AddDigits(int number)
{
int result=0;
while(number/10!=0)
{

result+=number%10;
number=number/10;
}
result+=number%10;
return result;
}

int Factorial(int number){
int result=1,i;
for(i=1;i<=number;i++){
result*=i;
}
return result;
}

int StringLength(char str[]){
int result=0;
while(str[result]!='\0'){
result++;
}
return result;
}


//0    ===> Strings are equal....
//1    ===> Strings are not equal....
int StringCompare(char str[],char str1[])
{
int result;
int i,j;
for(i=0;i<StringLength(str);i++){
if(str[i]!=str1[i])return 1;
}
return 0;
}

//0    ===> Palindrome...
//1    ===> Not Palindrome...
int Palindrome(char str[])
{
int result;
int i,j;
for(i=0,j=strlen(str)-1;i<StringLength(str);j--,i++){
if(str[i]!=str[j])return 1;
}
return 0;
}

char* SubsString(char str1[],int start,int end){
int i,j=0;
//strcpy(result,str1);
for(i=start;i<=end;i++){
result[j++]=str1[i];
}
//result[j]='\0';
return (char*)result;

}

char* StrConcat(char str1[],char str2[])
{
int i=0,j=0;
strcpy(result,str1);
strcat(result,str2);
//printf("Result %s",result);
return (char*)result;
}


Client File -




#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>

#define MAX 30

typedef struct message
{
char msg[MAX][MAX];
char msgoutput[MAX];
sint input;
int ch;
int output;
}message;


void main()
{
int ssfd;
int len;
int msgLen;
struct sockaddr_in address;
int result;
FILE *fp;

char ch='A';

message *msg;
message *reply;

msg=(message*)malloc(sizeof(message));
reply=(message*)malloc(sizeof(message));
msgLen=sizeof(message);
//strcpy(msg->msg,"Aniket");

ssfd=socket(AF_INET,SOCK_STREAM,0);

address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=9734;
len=sizeof(address);

result=connect(ssfd,(struct sockaddr*)&address,len);
//fp=fdopen(ssfd,"rw");
if(result==-1){
perror("Server not found");
printf("\nError Result:%d",result);
//exit(1);
}

printf("\nConnection successful");

do{
printf("\n\nSELECT Appropriate Option:\n");
printf("\n1.\tAddition of Digits");
printf("\n2.\tFactorial of a Number");
printf("\n3.\tLength of string");
printf("\n4.\tString Comparison");
printf("\n5.\tString Concatenation");
printf("\n6.\tCheck For Palindrome");
printf("\n7.\tFind Substring.");
printf("\n8.\tRun Command");

printf("\n9.\tDisconnect And Exit");
scanf("%d",&msg->ch);
printf("Your Choice:%d",msg->ch);
//write(ssfd,msg,msgLen);
switch(msg->ch){
case 1:
printf("\nEnter Number:");
scanf("%d",&msg->input);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
break;
case 2:
printf("\nEnter Number:");
scanf("%d",&msg->input);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);

break;

case 3:
printf("\nEnter any string:");
scanf("%s",msg->msg[0]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
break;

case 4:
printf("\nEnter any First String:");
scanf("%s",msg->msg[0]);
printf("\nEnter any Second String:");
scanf("%s",msg->msg[1]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
if(reply->output==0)
printf("\nStrings are equal...");
else
printf("\nStrings are not equal...");

break;

case 5:
printf("\nEnter any First String:");
scanf("%s",msg->msg[0]);
printf("\nEnter any Second String:");
scanf("%s",msg->msg[1]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%s",reply->msgoutput);


break;

case 6:
printf("\nEnter any string:");
scanf("%s",msg->msg[0]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
if(reply->output==0)
printf("\nString is a palindrome");
else
printf("\nStrings is not palindrome");

break;
case 7:
printf("\nEnter any string:");
scanf("%s",msg->msg[0]);
printf("\nEnter Start Index:");
scanf("%d",&msg->input);
printf("\nEnter End Index:");
scanf("%d",&msg->output);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%s",reply->msgoutput);
break;
case 8:
printf("\nEnter String-\"xeyes\" or other valid command:");
scanf("%s",msg->msg[0]);

write(ssfd,msg,msgLen);
system(msg->msg[0]);
read(ssfd,reply,msgLen);
break;
default:
printf("Invalid Choice....Select Another");
break;
}
}while(msg->ch!=9);

close(ssfd);
//exit(0);
}



/*
OUTPUT

desktop ~ $ gcc server.c
desktop ~ $ ./a.out

SSFD Value:3
Connected to socket


desktop ~ $ gcc -o client.out client.c
desktop ~ $ ./client.out

Connection successful

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit1
Your Choice:1
Enter Number:1234
Output from server:10

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit2
Your Choice:2
Enter Number:5
Output from server:120

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit3
Your Choice:3
Enter any string:abc
Output from server:3

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit4
Your Choice:4
Enter any First String:xyz  

Enter any Second String:xyx
Output from server:1
Strings are not equal...

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit5
Your Choice:5
Enter any First String:Abc

Enter any Second String:Xyz
Output from server:AbcXyz

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit6
Your Choice:6
Enter any string:malyalam
Output from server:1
Strings is not palindrome

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit7
Your Choice:7
Enter any string:intelcoreigen

Enter Start Index:2

Enter End Index:5
Output from server:telc

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit9

*/

Leave a comment

C Program for Hashing Linear Probing with chaining without Replacement

C - Program to Implement Hashing , handle collision  using linear probing with chaining  without 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
    array chain[]- for chaining of synonyms */
    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*/
    /*locate the beginning of the chain*/
    while(flag[start] && i<SIZE)
       {
        if(data[start]%SIZE==x%SIZE)
            break;
        i++;
        start=(start+1)%SIZE;
       }
    if(i==SIZE)
       {
        printf("\n***hash table is full****");
        return;
       }
    /*go to the end of chain */
    while(chain[start]!=-1)
        start=chain[start];
    /*locate an empty place for the current data */
    j=start;
    while(flag[j] && i<SIZE)
      {
        j=(j+1)%SIZE;
        i=i+1;
      }
    if(i==SIZE)
      {
        printf("\n***hash table is full****");
        return;
      }
    data[j]=x;   /*store the data */
    flag[j]=TRUE;
    if(j!=start)
        chain[start]=j; /*set the chain */
}

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 , , , | 1 Comment

C Program for Depth First Search(DFS) Breadth First Search(BFS)

C - Program to Implement Depth First Search(DFS) and Breadth First Search(BFS)


#include<stdio.h>
#include<conio.h>
#define MAX 30


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

typedef struct q
{
  int r,f;
  int data[MAX];
}q;


node *g[20];                                     //heads of linked list
int n;                                           //no. of nodes
int visited[MAX];

void readgraph();                //create adjacency list
void insert(int vi,int vj);                    //insert edge(vi,vj) in graph
void bfs(int i);
void dfs(int i);
void enquee(q *,int);
int dequee(q *);
int empty(q *);
int full(q *);

void main()
{
  int i,k;
  clrscr();
  do
  {
    printf("\n\n1.CREATE A GRAPH\n2.DFS\n3.BFS\n4.QUIT.");
    printf("\nEnter your choice::");
    scanf("%d",&k);
    switch(k)
    {
      case 1:
            readgraph();
            break;
      case 2:
            for(i=0;i<n;i++)
                visited[i]=0;
            printf("\nEnter the start node::");
            scanf("%d",&i);
            dfs(i);
            break;
      case 3:
            printf("\nEnter the start node::");
            scanf("%d",&i);
            bfs(i);
            break;

    }
   }while(k!=4);
}

void readgraph()
{
  int i,j,vi,vj,no;


  printf("\nEnter the no. of vertices::");
  scanf("%d",&n);
  for(i=0;i<n;i++)                //initialise g to NULL
    g[i]=NULL;

  printf("\nEnter the no. of edges::");         //insert edges to graph g
  scanf("%d",&no);
  for(i=0;i<no;i++)
  {
    printf("\nEnter the edge(u,v)::");
    scanf("%d%d",&vi,&vj);
    insert(vi,vj);
    insert(vj,vi);

  }

}

void insert(int vi,int vj)
{
  node *p,*q;
  q=(node*)malloc(sizeof(node));         //acquire memory for new node
  q->vertex=vj;
  q->next=NULL;


  if(g[vi]==NULL)
    g[vi]=q;                               //insert node in linked list
  else
  {
    p=g[vi];
    while(p->next!=NULL)                       //go to end of linked list
    p=p->next;
    p->next=q;
  }
}

void dfs(int i)
{
 node *p;
 p=g[i];
 visited[i]=1;
 printf("\nVisit->%d",i);
 while(p!=NULL)
 {
   i=p->vertex;
   if(!visited[i])
    dfs(i);
   p=p->next;
  }
}

void bfs(int v)
{
  int i,w;
  q x;
  node *p;
  x.r=x.f=-1;                        //initialise quee
  for(i=0;i<n;i++)
    visited[i]=0;

  enquee(&x,v);
  printf("\nVisit->%d",v);
  visited[v]=1;
  while(!empty(&x))
  {
    v=dequee(&x);
    //insert all unvisited vertices of v into quee
    for(p=g[v];p!=NULL;p=p->next)
    {
    w=p->vertex;
    if(visited[w]==0)
    {
        enquee(&x,w);
        visited[w]=1;
        printf("\nVisit->%d",w);
    }
     }
  }
}


int empty(q *p)
{
  if(p->r==-1 &&p->f==-1)
    return(1);
  return(0);
}

int full(q *p)
{
  if(p->r==MAX-1)
    return(1);
  return(0);

}

void enquee(q *p,int x)
{

  if(p->r==-1)
  {
    p->r=p->f=1;
  }
  else
    p->r=p->r+1;
  p->data[p->r]=x;
}

int dequee(q *p)
{
  int x;
  x=p->data[p->f];
  if(p->f==p->r)
    p->r=p->f=-1;
  else
    p->f=p->f+1;
  return(x);
}

Click Here to Download Source Code with Executable Program.

Posted in , | 1 Comment

C - Program to Implement Singly Linked List (SLL)

C - Program to Implement Singly Linked List (SLL)

 

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
   { int data;
     struct node *next;
   }node;

node *create();
node *insert_b(node *head,int x);
node *insert_e(node *head,int x);
node *insert_in(node *head,int x);
node *delete_b(node *head);
node *delete_e(node *head);
node *delete_in(node *head);
node *reverse(node *head);
void printreverse(node *head);
void print(node *head);

void main()
{ int op,op1,x;
  node *head=NULL;
  clrscr();
  do
    {
      printf("\n\n1)create\n2)Insert\n3)Delete\n4)Display Reverse ");
      printf("\n5)Revert the SLL\n6)Display\n7)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 : ");
        scanf("%d",&x);
        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:printreverse(head);break;
     case 5:head=reverse(head);
        print(head);
        break;
     case 6:print(head);break;
       }
    }while(op!=7);
}

void printreverse(node *head)
  {
    if(head !=NULL)
       {
      printreverse(head->next);
      printf("%d   ",head->data);
       }
  }
node *create()
{ node *head,*p;
  int i,n;
  head=NULL;
  printf("\n Enter no of data:");
  scanf("%d",&n);
  printf("\nEnter the data:");
  for(i=0;i<n;i++)
   {
     if(head==NULL)
     p=head=(node*)malloc(sizeof(node));
     else
       {
     p->next=(node*)malloc(sizeof(node));
     p=p->next;
       }
       p->next=NULL;
       scanf("%d",&(p->data));
   }
 return(head);
}

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

node *insert_e(node *head,int x)
{   node *p,*q;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=NULL;
    if(head==NULL)
       return(p);
    //locate the last node
    for(q=head;q->next!=NULL;q=q->next)
    ;
    q->next=p;
    return(head);
}

node *insert_in(node *head,int x)
{   node *p,*q;
    int y;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=NULL;
    printf("\Insert after which number ? : ");
    scanf("%d",&y);
    //locate the lthe data 'y'
    for(q=head ; q != NULL && q->data != y ; q=q->next)
    ;
    if(q!=NULL)
      {
    p->next=q->next;
    q->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);
     }
  p=head;
  head=head->next;
  free(p);
  return(head);

}
node *delete_e(node *head)
{
  node *p,*q;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  p=head;
  if(head->next==NULL)
     { // Delete the only element
       head=NULL;
       free(p);
       return(head);
     }
//Locate the last but one node
   for(q=head;q->next->next !=NULL;q=q->next)
   ;
   p=q->next;
   q->next=NULL;
   free(p);
   return(head);
}
node *delete_in(node *head)
{
  node *p,*q;
  int x,i;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  printf("\nEnter the data to be deleted : ");
  scanf("%d",&x);
  if(head->data==x)
     { // Delete the first element
       p=head;
       head=head->next;
       free(p);
       return(head);
     }
//Locate the node previous to one to be deleted
   for(q=head;q->next->data!=x && q->next !=NULL;q=q->next )
   ;
   if(q->next==NULL)
     {
       printf("\nUnderflow.....data not found");
       return(head);
     }
   p=q->next;
   q->next=q->next->next;
   free(p);
   return(head);
}

void print(node *head)
{ node *p;
 printf("\n\n");
 for(p=head;p!=NULL;p=p->next)
  printf("%d  ",p->data);
}

node *reverse(node *head)
  { node *p,*q,*r;
    p=NULL;
    q=head;
    r=q->next;
    while(q!=NULL)
      {
    q->next=p;
    p=q;
    q=r;
    if(q!=NULL)
       r=q->next;
      }
   return(p);
  }


 Click Here to Download Source Code with Executable Program

Posted in | Leave a comment

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