CN Lab prog-BH
CN Lab prog-BH
CN Lab prog-BH
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
void main()
{
char *p,*q;
char temp;
char in[MAXSIZE];
char stuff[MAXSIZE];
char destuff[MAXSIZE];
int count=0;
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("\nthe 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("\nthe destuffed character string is");
printf("\n%s\n",destuff);
getch();
}
#include<stdio.h>
#include<conio.h>
void ins(char,int);
void del(int,int);
char fr[50];
main()
{
int i,ch;
clrscr();
printf("Enter the frame:\t");
gets(fr);
/*Character Stuffing*/
ins('s',0);
ins('e',5);
printf("\n\nStuffed frame is : %s",fr);
getch();
/*Destuffing*/
del(0,6);
del(strlen(fr)-6,6);
printf("\n\nDestuffed bit is \t%s",fr);getch();
return 0;
}
void ins(char in,int p)
{
char dup[50];
int i;
strcpy(dup,fr);
if(in=='s')
{
fr[p]='s';
fr[p+1]='t';
fr[p+2]='x';
fr[p+3]='d';
fr[p+4]='l';
fr[p+5]='e';
for(i=p+6;i<strlen(fr)+6;i++)
{
fr[i]=dup[i-6];
}
}
if(in=='e')
{
strcat(fr,"etxdle");return;
}
}
void del(int q,int n)
{
int i;
for(i=q;i<strlen(fr);i++)
{
fr[i]=fr[i+n];
}
}
3) C Program to find Minimum Distance of Vertices from a given Source in a Graph using Dijkstra’s
Algorithm.
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void main()
{
int G[MAX][MAX],i,j,n,u;
clrscr();
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
getch();
}
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
4)C program to implement Distance Vector Routing using Bellman Ford
Algorithm.
#include<stdio.h>
struct node
{
int dist[20];
int from[20];
}rt[10];
void main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the 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];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct
distance from the node i to k using the cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
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\nDestination %d Next Hop %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}
o/p:
/*
A sample run of the program works as:-
Enter the number of nodes :
3
Enter the cost matrix :
0 2 7
2 0 1
7 1 0
For router 1
Destination 1 Next Hop 1 Distance 0
Destination 2 Next Hop 2 Distance 2
Destination 3 Next Hop 3 Distance 3
For router 2
Destination 1 Next Hop 1 Distance 2
Destination 2 Next Hop 2 Distance 0
Destination 3 Next Hop 3 Distance 1
For router 3
Destination 1 Next Hop 1 Distance 3
Destination 2 Next Hop 2 Distance 1
Destination 3 Next Hop 3 Distance 0
*/
5) write C program to implement BROADCAST ROUTING
#include<stdio.h>
struct ed
{
int v1,v2,w;
}
edj[20],temp;
void main()
{
int i,j,n=0,s,d,par[20],s1,d1;
clrscr();
printf("Enter no of edges");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the node1,node2,weight");
scanf("%d%d%d",&edj[i].v1,&edj[i].v2,&edj[i].w);
par[i]=0;
}
clrscr();
printf("Given Graph is:\n");
for(i=0;i<n;i++)
printf("node %d to node %d wieght %d\n",edj[i].v1,edj[i].v2,edj[i].w);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
if(edj[j].w>edj[i].w)
{
temp=edj[i];
edj[i]=edj[j];
edj[j]=temp;
}
}
printf("\n BROADCAST ROUTING FOR THE GIVEN GRAPH\n");
printf("Node\tNode\tWeight\n");
for(i=0;i<n;i++)
{
s=edj[i].v1;
d=edj[i].v2;
s1=s;
d1=d;
while(par[s1]>0)
s1=par[s1];
while(par[d1]>0)
d1=par[d1];
if(s1!=d1)
{
par[d]=s;
printf("%d\t%d\t%d\n",s,d,edj[i].w);
}
}
getch();
}
/*o/p:
enter node1,node2,weight 1 2 3
enter node1,node2,weight 2 3 2
enter node1,node2,weight 2 4 1
enter node1,node2,weight 3 5 4
enter node1,node2,weight 4 5 5
2 4 1
2 3 2
1 2 3
3 5 4
***********************************************/
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
****************************************************************
7)C Program to detect error using Checksum method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
long int sender(int b[10],int k)
{
int i,sum=0,checksum;
printf("\n****SENDER****\n");
for(i=0;i<k;i++)
sum+=b[i];
printf("SUM IS: %d",sum);
checksum=~sum;
printf("\nSENDER's CHECKSUM IS:%d",checksum);
return checksum;
}