WELCOME TO EXAMHELPER20
CREATED BY "NIKHIL DOPPANI"
DO FOLLOW examhelper20
DNS server Installation
BIT STUFFING & DESTUFFING
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
int main()
{
char *p,*q;
char in[MAXSIZE];
char stuff[MAXSIZE];
char destuff[MAXSIZE];
int count=0;
printf("Enter the input character string(0's,1's only):\n");
scanf("%s",in);
p=in;
q=stuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
*q='0';
q++;
}
count=0;
}
}
*q='\0';
printf("\n The stuffed character string is");
printf("\n %s",stuff);
p=stuff;
q=destuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
p++;
}
count=0;
}
}
*q='\0';
printf("\n The destuffed character string is ");
printf("\n %s",destuff);
return 0;
}
OUTPUT:
CHARACTER STUFFING:
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[30],b[30],ch;
printf("enter the string\n ");
scanf("%s",&a);
n=strlen(a);
printf("enter the position \n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position,enter agian");
scanf("%d ",&pos);
}
printf("enter the character ");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='ch';
b[j+4]='d';
b[j+5]='T';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d'&&a[i+1]=='1'&&a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("after char stuffing:\n");
printf("%s",b);
}
OUTPUT:
BROADCAST ROUTING ALGORITHAM
#include<stdio.h>
int a[10][10],n;
main()
{
int i,j,root;
printf("enter the no of nodes:");
scanf("%d",&n);
printf("enter the adjacent matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("enter connecting of %d->%d:: ",i,j);
scanf("%d",&a[i][j]);
}
printf("enter the root node ");
scanf("%d",&root);
adj(root);
}
adj(int k)
{
int i,j;
printf("adjacent node of root node ::\n");
printf("%d \n\n",k);
for(j=1;j<=n;j++)
{
if(a[k][j]==1||a[j][k]==1)
printf("%d\t",j);
}
printf("\n");
for(i=1;i<=n;i++)
{
if((a[k][j]==0)&&(a[i][k]==0)&&(i!=k))
printf("%d",i);
}
}
OUTPUT:
DIJKSTRAS ALGORITHAM
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter the no of vertices :");
scanf("%d",&n);
printf("\n Enter the adjacency matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\n Enter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance && !visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance + cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\n Distance of node %d=%d",i,distance[i]);
printf("\n path=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}
while(j!=startnode);
}
}
OUTPUT:
CRC ERROR DETECTION
#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[28],cs[28],g[]="10001000000100001";
int a,e,c;
void xor(){
for(c=1;c<N;c++)
cs[c]=((cs[c]==g[c])?'0':'1');
}
void crc(){
for(e=0;e<N;e++)
cs[e]=t[e];
do{
if(cs[0]=='1')
xor();
for(c=0;c<N-1;c++)
cs[c]=cs[c+1];
cs[c]=t[e++];
}
while(e<=a+N-1);
}
int main()
{
printf("\n Enter data:");
scanf("%s",t);
printf("\n---------------------------------");
printf("\n Generating polynomial:%s",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\n----------------------------------");
printf("\n Modified data is:%s",t);
printf("\n----------------------------------");
crc();
printf("\n Check sum is:%s",cs);
for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\n----------------------------------");
printf("\n Final lodeword is:%s",t);
printf("\n----------------------------------");
printf("\n Test error detection 0(YES) 1(NO)?:");
scanf("%d",&e);
if(e==0)
{
do{
printf("\n Enter the position where error is to be inserted:");
scanf("%d",&e);
}
while(e==0||e>a+N-1);
t[e-1]=(t[e-1]=='0')?'1':'0';
printf("\n--------------------------------");
printf("\n Erroreous data:%s\n",t);
crc();
for(e=0;(e<N-1)&&(cs[e]!='1');e++);
if(e<N-1)
printf("\n Error detected \n\n");
else
printf("\n No error detected \n\n");
printf("\n---------------------------------");
return 0;
}
}
OUTPUT:
DISTANCE VECTOR ROUTING:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance%d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}
OUTPUT:
#include <stdio.h>
#include <string.h>
int main()
{
int count,src_router,i,j,k,w,v,min;
int cost_matrix[100][100],dist[100],last[100];
int flag[100];
printf("\n Enter the no of routers");
scanf("%d",&count);
printf("\n Enter the cost matrix values:");
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
printf("\n%d->%d:",i,j);
scanf("%d",&cost_matrix[i][j]);
if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;
}
}
printf("\n Enter the source router:");
scanf("%d",&src_router);
for(v=0;v<count;v++)
{
flag[v]=0;
last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
}
flag[src_router]=1;
for(i=0;i<count;i++)
{
min=1000;
for(w=0;w<count;w++)
{
if(!flag[w])
if(dist[w]<min)
{
v=w;
min=dist[w];
}
}
flag[v]=1;
for(w=0;w<count;w++)
{
if(!flag[w])
if(min+cost_matrix[v][w]<dist[w])
{
dist[w]=min+cost_matrix[v][w];
last[w]=v;
}
}
}
for(i=0;i<count;i++)
{
printf("\n%d==>%d:Path taken:%d",src_router,i,i);
w=i;
while(w!=src_router)
{
printf("\n<--%d",last[w]);w=last[w];
}
printf("\n Shortest path cost:%d",dist[i]);
}
}
OUTPUT:
Comments
Post a Comment