Practical No: 8: Code
Practical No: 8: Code
Code:
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char num[50];
int i,j, k, cnt=0,ch,n,pad;
char dnum[50];
int dispflag=0;
clrscr();
printf("========Menu========\n");
printf("1.Sender Side ( Stuffing ) \n");
printf("2.Receiver Side ( Destuffing ) \n");
printf("3.Exit\n");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter message to be sent : ");
scanf("%s",num);
i=0;
n=strlen(num);
while (i<strlen(num))
{
if(num[i]=='1')
{
i++;
cnt++;
if(cnt==5)
{
for(k=strlen(num)+1;k>i;k--)
num[k]=num[k-1];
num[i]='0';
}
n++;
}
else
{
i++;
cnt=0;
}}
num[n+1]='\0';
printf("\n Message Sent After Stuffing : \n\n");
for(i=0;i<=strlen(num);i++)
{ printf("%c" ,num[i]);
}
break;
case 2:
printf("\nEnter recieved message : ");
fflush(stdin);
scanf("%s",num);
i=0; j=0;
n=strlen(num);
while (i <n)
{
if(dispflag==1)
{
i++;
dispflag=0;
continue;
}
if(num[i]=='1')
{
dnum[j]=num[i];
j++;
i++;
cnt++;
if(cnt==5)
{
cnt=0;
dispflag=1;
continue;
}}
else if(num[i]=='0')
{ dnum[j] =num[i];
i++; j++;
cnt=0;
}}
dnum[j]='\0';
printf("\nMessage Originally Sent By Sender ( After Destuffing) : \n\n" );
for(i=0;i<=strlen(dnum);i++)
{ printf("%c" ,dnum[i]);
}
break;
case 3 : exit(0);
}
getch();
}
Outputs:
========Menu========
1.Sender Side ( Stuffing )
2.Receiver Side ( Destuffing )
3.Exit
1011111010101
========Menu========
1.Sender Side ( Stuffing )
2.Receiver Side ( Destuffing )
3.Exit
101111110101
Practical No: 9
/* Program to implement hamming code */
Code:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void hamming(char[],int,int,int);
bool hrecv(char[],int,int,int);
int detr(int);
void copybits(char[],char[],int,int);
void reverse(char[]);
int main()
{
int m,r;
char str[100];
char sentstr[100];
int n,i,choice;
printf(" 1. to send bits 2. to recive bits . 3. exit \n");
printf(" Your choice : " );
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the bits of the hamming code to be transmitted \n");
scanf("%s",str);
m=strlen(str);
r=detr(m);
n=m+r;
reverse(str);
copybits(str,sentstr,m,r);
hamming(sentstr,1,0,1);
hamming(sentstr,2,1,2);
hamming(sentstr,3,3,3);
hamming(sentstr,4,7,4);
printf("\n\n");
reverse(sentstr);
printf(" Hamming Codeword ");
for(i=0;i<m+r;i++)
{
printf("%c",sentstr[i]);
}
break;
case 2:
printf("\nEnter the bits of the hamming codeword recieved \n");
fflush(stdin);
gets(str);
m=strlen(str);
r=detr(m);
n=m+r;
reverse(str);
if(hrecv(str,1,0,1) && hrecv(str,2,1,2) && hrecv(str,3,3,3) && hrecv(str,4,7,4))
{ printf(" \nNo Error in data " ) ;
}
break;
case 3: exit(0);
}
getch();
return 0;
}
printf("\n\n");
for(j=0;j<len;j++)
printf("%c",newstr[j]);
for(i=1;i<len;i++)
{
if(newstr[i]=='1')
{
noof1s++;
}
}
if(noof1s%2==0)
charatpos='0';
else
charatpos='1';
if(newstr[0]!=charatpos)
{
printf("\nERROR in recieved data,(elements corresponding index %d )",index);
return false;
}
else
{
return true;
}}
int detr(int m)
{
int r=0;
int mr;
while(1)
{ mr=m+r+1;
if(mr <= pow(2,r))
{ return r;
}
r++;
}
}
if(countones%2==1)
{
sentstr[index]='1';
}
else
{
sentstr[index]='0';
}
}
Output :
1. to send bits
2. to recive bits
3. exit
Your choice : 1
1. to send bits
2. to recive bits .
3. exit
Your choice : 2
No Error in data
1. to send bits
2. to recive bits .
3. exit
Your choice : 2
Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int walsh[8][8]={1};
int walshnxt[8][8]={0};
void main()
{
int num, i, j, cntr;
printf("Enter the power of two that you want to display : ");
scanf("%d", &num);
walsh[0][0]=1;
cntr=1;
printf("\n\n\n");
if(num==0)
printf("\t1");
else
for(i=0;i<pow(2,num);i++)
{
for(j=0;j<pow(2,num);j++)
{
Output:
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
Practical No: 11
/* Implementation of Shortest Path Algorithm*/
Code:
#include<stdio.h>
#include<conio.h>
#define INFINITY 10000
struct state
{ int predecessor;
int length;
enum{permanent, tentative} label;
}*p, st[15];
int main()
{ int node, edge, dist[10][10], i, j, s, d, t, m, n;
int relation[10][10], k, min, length, path[10];
clrscr();
printf("Enter total no of nodes and edges: ");
scanf("%d %d", &node, &edge);
for(i=0; i<node; i++)
{ for(j=0; j<node; j++)
{ if(i==j)
{ relation[i][j]=1;
dist[i][j]=0;
continue;
}
relation[i][j]=0;
dist[i][j]=1000;
}
}
for(i=0; i<edge; i++)
{ fflush(stdin);
printf("\n\nEnter source, destination and distance for edge %d: ",i+1);
scanf("%d %d %d", &s, &t, &d);
relation[s-1][t-1]=1;
relation[t-1][s-1]=1;
dist[s-1][t-1]=d;
dist[t-1][s-1]=d;
}
Output:
1 1 0 1
1 1 1 1
0 1 1 1
1 1 1 1
Distance Matrix:
0 2 1000 8
2 0 10 7
1000 10 0 2
8 7 2 0
Code:
#include<stdio.h>
#include<conio.h>
#define INFINITY 10000
struct state
{ int predecessor;
int length;
enum{permanent, tentative} label;
}*p, st[15];
int main()
{ int node, edge, dist[10][10], i, j, s, d, t, m, n;
int relation[10][10], k, min, length, path[10], l;
clrscr();
printf("Enter total no of nodes and edges: ");
scanf("%d %d", &node, &edge);
for(i=0; i<node; i++)
{ for(j=0; j<node; j++)
{ if(i==j)
{ relation[i][j]=1;
dist[i][j]=0;
continue; }
relation[i][j]=0;
dist[i][j]=1000;
}
}for(i=0; i<edge; i++)
{ fflush(stdin);
printf("\n\nEnter source, destination and distance for edge %d: ",i+1);
scanf("%d %d %d", &s, &t, &d);
relation[s-1][t-1]=1;
relation[t-1][s-1]=1;
dist[s-1][t-1]=d;
dist[t-1][s-1]=d;
}
printf("\n\nRelation Matrix: \n\n");
for(i=0; i<node; i++)
{ for(j=0; j<node; j++)
printf(" %3d", relation[i][j]);
printf("\n\n");
}
printf("\n\nDistance Matrix: \n\n ");
for(i=0; i<node; i++)
{ for(j=0; j<node; j++)
printf("%4d\t", dist[i][j]);
printf("\n\n ");
}
fflush(stdin);
printf("\n\nEnter the destination node: ");
scanf("%d", &t);
printf("\n\n\t\t\t:- Sink Tree for Router %d :-", t);
t--;
for(l=0; l<node; l++)
{ s=l; //source node
if(s==t)
continue;
for(p=&st[0]; p<&st[node]; p++)
{ p->predecessor=-1;
p->length=INFINITY;
p->label=tentative;
}
st[t].length=0; st[t].label=permanent;
k=t;
do
{ for(i=0; i<node; i++)
{ if(dist[k][i]!=0 && st[i].label==tentative)
{ if(st[k].length + dist[k][i] < st[i].length)
{ st[i].predecessor = k;
st[i].length = st[k].length + dist[k][i];
}
}
}
/* find tentatively labeled node with the smallest label. */
k=0; min=INFINITY;
for(i=0; i<node; i++)
{ if(st[i].label==tentative && st[i].length<min)
{ min=st[i].length;
k=i;
}
}
st[k].label=permanent;
}while(k!=s);
/* Copy the path into the output array. */
for(i=0; i<15; i++)
path[i]='\0';
i=0; k=s;
do
{ path[i]=k+1; //manipulation since array starts from 0
i++;
k=st[k].predecessor;
}
while(k>=0);
printf("\n\n\nFrom Router %d: ", s+1);
for(i=0; path[i]!=NULL; i++)
{ printf("%d", path[i]);
m=path[i];
n=path[i+1];
if(path[i+1]!=NULL)
printf(" ---%d---> ", dist[--m][--n]);
}
printf("\n\n");
}
getch();
return 0;
}
Output:
1 1 1 1
1 1 1 0
1 1 1 1
1 0 1 1
Distance Matrix:
0 10 20 4
10 0 8 1000
20 8 0 3
4 1000 3 0
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
cout<<argc;
long double e = 0,d,l = strlen(text); // e must be odd
long double cipher[255];
long double a = 7,b = 11;
long double n = (a * b);
long double t = (a - 1)*(b - 1);
Output:
Code(Client.C):
#include<stdio.h>
#include<conio.h>
#include<server.h>
void hello()
{
printf("\nHello Server I am Client");
}
void main()
{
clrscr();
hello();
hellorply();
getch();
}
Code(Server.h):
#include<stdio.h>
#include<conio.h>
void hellorply()
{
printf("\n\nHello Client I am Server\n");
}
Output: