Tuesday, March 25, 2008

#include
#include
#include

struct edge
{
int destin;
struct edge * edgeptr;
};

struct graph
{
int reach[6];
int nodeno[6];
char data[6];
int dfn[6] ;
struct edge *listptr[6];
};

void main()
{
struct graph g1;
void dfs(struct graph *,int,int *);
int i,j;
static int adj[6][4]={{2,4,-1,-1},{2,5,-1,-1},{0,1,3,5},
{2,4,-1,-1},{0,3,-1,-1},{1,2,-1,-1}};
struct edge *p,*q;
for(i=0;i<6;i++)
{
g1.reach[i]=0;
g1.nodeno[i]=i;
g1.data[i]='A'+i;
}

for(i=0;i<6;i++)
for(j=0;j<4;j++)
if (adj [i] [j] !=-1)
{
q=(struct edge *)malloc(sizeof(struct edge));
q->destin=adj[i][j];
q->edgeptr=NULL;
if (j==0)
g1.listptr[i]=p=q;
else
{
p->edgeptr=q;
p=q;
}
}
dfs(&g1,0,0);
for(i=0;i<6;i++)
printf ("%c\t%d\n",g1.data[i] ,g1.dfn[i]);
getch();
}

void dfs(struct graph *g,int index,int *count)
{
struct edge *link;

*count=*count+1;
g->dfn[index]=*count;
g->reach[index]=1;
link=g->listptr[index];
while(link!=NULL)
{
if(!(g->reach[link->destin]))
dfs(g,link->destin,count);
link=link->edgeptr;
}
}
#include
#include
#include

struct edge
{
int destin;
struct edge * edgeptr;
};

struct graph
{
int reach[6];
int nodeno[6];
char data[6];
int dfn[6] ;
struct edge *listptr[6];
};

void main()
{
struct graph g1;
void dfs(struct graph *,int,int *);
int i,j;
static int adj[6][4]={{2,4,-1,-1},{2,5,-1,-1},{0,1,3,5},
{2,4,-1,-1},{0,3,-1,-1},{1,2,-1,-1}};
struct edge *p,*q;
for(i=0;i<6;i++)
{
g1.reach[i]=0;
g1.nodeno[i]=i;
g1.data[i]='A'+i;
}

for(i=0;i<6;i++)
for(j=0;j<4;j++)
if (adj [i] [j] !=-1)
{
q=(struct edge *)malloc(sizeof(struct edge));
q->destin=adj[i][j];
q->edgeptr=NULL;
if (j==0)
g1.listptr[i]=p=q;
else
{
p->edgeptr=q;
p=q;
}
}
dfs(&g1,0,0);
for(i=0;i<6;i++)
printf ("%c\t%d\n",g1.data[i] ,g1.dfn[i]);
getch();
}

void dfs(struct graph *g,int index,int *count)
{
struct edge *link;

*count=*count+1;
g->dfn[index]=*count;
g->reach[index]=1;
link=g->listptr[index];
while(link!=NULL)
{
if(!(g->reach[link->destin]))
dfs(g,link->destin,count);
link=link->edgeptr;
}
}
#include
#include

void main()
{
int a[100],n,i,key,p;
int binary_search(int [],int,int);
clrscr();
printf("\nHow many integers=>");
scanf("%d",&n);
printf("\nEnter integers(sorted)=>");
for(i=0;i scanf("%d",&a[i]);
printf("\nEnter the element to be searched=");
scanf("%d",&key);
if((p=binary_search(a,n,key)) == -1)
printf("\n%d is not present\n",key);
else
printf("\n%d is present at position %d\n",key,p+1);
getch();
}

int binary_search(int x[],int n,int key)
{
int l,h,m;
l=0;
h=n-1;
while(l<=h)
{
m=(l+h)/2;
if(key < x[m])
h=m-1;
else if(key > x[m])
l=m+1;
else return(m);
}
return(-1);
}
#include
#include
#include
struct edge
{
int destin;
struct edge *edgeptr;
};

struct graph
{
int reach[6];
int nodeno[6];
char data[6];
int dist[6];
struct edge *listptr[6];
};

void main()
{
struct graph g1;
void bfs(struct graph *,int,int);
int i,j;
static int adj[6][4]={{2,4,-1,-1},{2,5,-1,-1},{0,1,3,5},
{2,4,-1,-1},{0,3,-1,-1},{1,2,-1,-1}};
struct edge *p,*q;
clrscr();
for(i=0;i<6;i++)
{
g1.reach[i]=0;
g1.nodeno[i]=i;
g1.data[i]='A'+i;
}
for(i=0;i<6;i++)
for (j=0;j<4; j++)
if (adj [i] [j] !=-1)
{
q=(struct edge *)malloc(sizeof(struct edge));
q->destin=adj[i][j];
q->edgeptr=NULL;
if (j==0)
g1.listptr[i]=p=q;
else
{
p->edgeptr=q;
p=q;
}
}
bfs(&g1,0,6);
for(i=0;i<6;i++)
printf("%c\t%d\n",g1.data[i],g1.dist[i]);
getch();
}

void bfs(struct graph *g,int index,int n)
{
void qinsert(int *,int *,int *,int,int);
int qdelete(int *,int *,int *);
int *f,*b;
struct edge *link;
int q[6];
g->reach[index]=1;
g->dist[index]=0;
*f=*b=0;
qinsert(q,f,b,n,index);
while(*b !=0 )
{
index=qdelete(q,f,b);
link=g->listptr[index];
while(link!=NULL)
{
if(!(g->reach[link->destin]))
{
g->dist[link->destin]=g->dist[index]+1;
g->reach[link->destin]=1;
qinsert(q,f,b,n,link->destin);
}
link=link->edgeptr;
}
}
}

void qinsert(int *q,int *f,int *b,int n,int index)
{
if (*b==n)
printf( "queue full\n") ;
else
{
*b=*b+1;
q[*b]=index;
if (*f==0)
*f=*f+1;
}
}

int qdelete(int *q,int *f,int *b)
{
int y;
if(*b==0)
{
printf ( "queue empty\n");
return -1;
}
else
{
y=q[*f];
if (*f==*b)
*f=*b=0;
else
*f=*f+1;
return y;
}
}

Sunday, March 16, 2008

booths

#include
#include
#include
#include

int get(int a)
{
char ch='B';
int flag=0;
if(a==1)
ch='A';
do
{
printf("¦ ENTER VALUE OF %c: ",ch);
scanf("%d",&a);
if(a<0)
{
a = a * -1;
flag = 1;
}
if(9<=a)
printf("¦\n\t!INVALID NUMBER.ENTER VALUE (-9 < A < 9)!");
}while(9<=a);
if(flag)
a = a *-1;
return(a);
}

void add(int *a,int *b)
{
int x,i,c=0;
for(i=3;i>=0;i--)
{
x=a[i];
a[i]=c^x^b[i];
if(((c==1)&&(x==1))||((x==1)&&(b[i]==1))||((b[i]==1)&&(c==1)))
c = 1;
else
c = 0;
}
}

void binary(int x,int*arr)
{
int i,p=x,c[4]={0,0,0,1};
for(i=0;i< 4;i++)
arr[i] = 0;
if(x < 0)
x = x *-1;
i = 3;
do
{
arr[i]=x%2;
x = x/2;
i--;
}while(x!=0);
if(p< 0)
{
for(i=0;i< 4;i++)
arr[i]=1-arr[i];
add(arr,c);
}
printf("\n\nTHE BINARY EQUIVALENT OF %d IS : ",p);
for(i=0;i< 4;i++)
printf("%d",arr[i]);
}

void rshift(int x,int *y)
{
int i;
for(i=3;i>0;i--)
y[i] = y[i-1];
y[0] = x;
}

void main()
{
int q=0,i,j,a,b,A[4]={0,0,0,0},C[4]={0,0,0,1},C1[8]={0,0,0,0,0,0,0,1};
int s=0,z=0,Q[4],M[4],temp,temp1[4],ans[8],y,x=0,c=0;
clrscr();
printf("\n¦----------------------------------------------------\n");
a = get(1);
b=get(0);
printf("\n¦---------------------------------------------------\n");
binary(a,M);
binary(b,Q);
printf("\n\n---------------------------------------------------\n");
printf(" OPERATION\t\t A\t Q\tQ'\t M");
printf("\n\n INITIAL\t\t");
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
for(j=0;j< 4;j++)
{
if((Q[3]==0)&&(q==1))
{
printf("\n A:=A+M \t\t");
add(A,M);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
if((Q[3]==1)&&(q==0))
{
printf("\n A:=A-M \t\t");
for(i=0;i< 4;i++)
temp1[i] = 1-M[i];
add(temp1,C);
add(A,temp1);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
printf("\n Shift \t\t\t");
y = A[3];
q = Q[3];
rshift(A[0],A);
rshift(y,Q);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
printf("\n\n---------------------------------------------------\n");
printf("\nTHE ANSWER IN BINARY IS : ");
for(i=0;i< 4;i++)
ans[i]=A[i];
for(i=0;i< 4;i++)
ans[i+4]=Q[i];
if(((a<>0))||((a>0)&&(b< 0)))
{
for(i=0;i< 8;i++)
ans[i]=1-ans[i];
for(i=7;i>=0;i--)
{
x = ans[i];
ans[i]=c^x^C1[i];
if(((c==1)&&(x==1))||((x==1)&&(C1[i]==1))||((C1[i]==1)&&(c==1)))
c=1;
else
c=0;
}
}
for(i=0;i< 8;i++)
printf("%d",ans[i]);
for(i=7;i>=0;i--)
{
s = s + (pow(2,z) * ans[i]);
z = z+1;
}
if(((a<>0))||((a>0)&&(b< 0)))
printf("\nTHE ANSWER IN DECIMAL IS : -%d\n",s);
else
printf("\nTHE ANSWER IN DECIMAL IS : %d\n",s);
getch();
}
unsigned


#include<>
#include<>

#define max 16

void convert1(int *n1,int *n2)
{
int i,z,j,d=0,n=1;
z=(2*max);
for(i=max-1;i>=0;i--)
{
z--;
if(n1[i]==1)
{
n=1;
for(j=1;j< =z;j++)
n = n * 2;
d = d + n;
}
}
n = 1;
for(i=max-1;i>=0;i--)
{
if(n2[i]==1)
{
n = 1;
for(j=1;j< =i;j++)
n = n * 2;
d = d + n;
}
}
printf("\n\n\tIN DECIMAL FORM: %d",d);
}

void convert2(int x,int *arr)
{
int q=0,j=x;
while(j>=0 && q< max)
{
x=j%2;
arr[q++]=x;
j = j/2;
}
}

int addbin(int *n1,int *n2,int *arr3)
{
int i,c=0,g,p,j,arr[max];
for(i=max-1;i>=0;i--)
{
g = n1[i]&n2[i];
p = n1[i]^n2[i];
arr[i] = p^c;
c = g|(p&c);
}
c = 0;
for(i=0;i< max;i++)
{
if(c==0)
{
if(n1[i]==0 && n2[i]==0)
arr3[i]=0;
if(n1[i]==0 && n2[i]==1)
arr3[i]=1;
if(n1[i]==1 && n2[i]==0)
arr3[i]=1;
if(n1[i]==1 && n2[i]==1)
{
arr3[i] = 0;
c = 1;
}
}
else if(c==1)
{
if(n1[i]==0 && n2[i]==0)
{
arr3[i] = 1;
c = 0;
}
if(n1[i]==0 && n2[i]==1)
arr3[i]=0;
if(n1[i]==1 && n2[i]==0)
arr3[i]=0;
if(n1[i]==1 && n2[i]==1)
arr3[i]=1;
}
}
return c;
}

void shift(int *n1,int *n2,int *arr3)
{
int c=0,i,j,k,temp[max];
for(i=0;i< max;i++)
{
if(arr3[0]==1)
{
c = addbin(n1,n2,temp);
for(j=0;j< max;j++)
n1[j] = temp[j];
}
for(j=0;j< max-1;j++)
arr3[j] = arr3[j+1];
arr3[max-1] = n1[0];
for(j=0;j< max-1;j++)
n1[j] = n1[j+1];
n1[max-1] = c;
}
}

void main()
{
int i,j,a[max],b[max],q[max],x,y;
clrscr();
printf("\n _________________________________________________");
printf("\n|\t\tUnsigned Multiplication\n");
printf("|----------------------------------------------------------------------------");
printf("\n|Enter The First No\t: ");
scanf("%d",&x);
printf("|Enter The Second No\t: ");
scanf("%d",&y);
printf("|_________________________________________________");
while(x>0 && y>0)
{
convert2(0,a);
printf("\n\nTHE 16-bit BINARY FORM OF 0\t: ");
for(i=max-1;i>=0;i--)
printf("%d ",a[i]);
convert2(x,b);
printf("\nTHE 16-bit BINARY FORM OF %d\t: ",x);
for(i=max-1;i>=0;i--)
printf("%d ",b[i]);
convert2(y,q);
printf("\nTHE 16-bit BINARY FORM OF %d\t: ",y);
for(i=max-1;i>=0;i--)
printf("%d ",q[i]);
shift(a,b,q);
printf("\n\n\tCalculating %d * %d ",x,y);
printf("In 16-bit BINARY FORM :\n\n\t");
for(i=max-1;i>=0;i--)
printf("%d ",a[i]);
printf("\n\t");
for(j=max-1;j>=0;j--)
printf("%d ",q[j]);
convert1(a,q);
x = -1;
getch();
}
}
nonrestoring


#include
#include
int a[5]={0,0,0,0,0},q[4],b[5],b2c[5];
comp()
{
int i=4;
do
{
b2c[i]=b[i];
i--;
}while(b[i+1]!=1);
while(i>=0)
{
b2c[i]=(b[i]+1)%2;
i--;
}
printf("\n\tB's complement:");
for(i=0;i< 5;i++)
printf("%d",b2c[i]);
printf("\n");
}


nonresdiv()
{
shiftleft();
if(a[0]==0)
a_minus_b();
else
a_plus_b();
q[3]=(a[0]+1)%2;
}

shiftleft()
{
int i;
for(i=0;i< 4;i++)
a[i]=a[i+1];
a[4]=q[0];
for(i=0;i< 3;i++)
q[i]=q[i+1];
}

a_minus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b2c[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}

a_plus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}


void main()
{
int i,j,k;
clrscr();
printf("Enter dividend in binary form\t: ");
for(i=0;i< 4;i++)
scanf("%d",&q[i]);
printf("Enter divisor in binary form\t: ");
for(i=0;i< 5;i++)
scanf("%d",&b[i]);
comp();
printf("\n\t[A]\t[M]\n");
for(i=0;i< 4;i++)
{
nonresdiv();
printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
}
if(a[0]==1)
a_plus_b();printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
printf("\n\tThe Quotient Is\t: ");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n\tThe Remainder Is\t: ");
for(j=0;j< 5;j++)
printf("%d",a[j]);
getch();
}