You are on page 1of 25

/1.

Calculation of CRC (Cyclic Redundancy Check)*


{
void
dof
qlil=
cin>>g
cirscr();
#include
#cin>>n;
include<iostream.h>
forli-0;i<
cin>>genU
for(i=0;i<
forli-0;i
cin>>arr[i;
coutcarrlU
arrfn+il=0;
a=n+(g-1);
cout"Enter
for(i-0;ien;it+)
cout<<"Enter
cout<"Enter
main()
cout"\nEnter
cout<<genil;
forj=0;jg:j++)
arrlil;
forlj-0;j<g:ij++)
cout<"\n\tThe
#includestdlib.h>
cout<<"Transmitter
while(gen[o]l=1);
cout<"\n\tThe

conio.h>
j;++i)
a;++i)
n;++i)
size
data:";
no.
of
inti,j.n.8,a,arr[20],gen[20],b[20],q[20],5;
generator:";
of
generator
side:";
appended
data
generator:";
bits:";
matrix:";
matrix

is:";

for(i-0;i< n;+i)

iffarlil==0})

forlj=ij<g+i;++)
arr[il ar|[jl^0;
for(i-0;i<7;i++)
cout<<b[il<<" ";

getch();

OUT PUT
Program to implement Hamming code
Enter the 4 bit data word: 1011
RO-0
R1-0
R2-1
Enter the 7-bit code word: 1001100
s2-1
$1-1
s2-1
Error in transmission
Corruption bit is b1
After correction code word:1011100
else
if(ic4)
break;
coutee"\n

2. Hamming code.
#include<iostream.h>
corrupted
#includecconio.h>
void main()
bit
is
intj,i,a[20],b[20],r0,r1,r2,50,s1,52, c[10);
clrscr(; b"<<3-i;
cout<"\n Enter the 4-bit data word:";
for(i=0;i<4;i++)
cin>>a[il;
rO=(a[3]+a[2]+a[1])%2;
r1-(a[2]+a [1]+a[0])%2;
r2-(a [3]+a[2]+a [O))%2;
cout"n r0="<<ro«<"\n r1="<<r1<<"\n r2="<<r2;
cout"n Enter the 7 bit code word:";
for(i=0;i<7;i++)
cin>>blil;
s0=(b[3]+b[2]+b[1]+b[6)%2;
s1=(b[2]+b[1}+b[0]+b[5]1%2;
s2=(b[3]+b[2]+b[0]+b[4])%2;
cout<"\ns0="<<s0<<"\ns1"<<s1<<"\ns2="<<s2;

i=0;
c[il=s2;
i++;
c[il=s1;
i++
c[il=s0;
int s[7]13]={1, 1,0}, {0, 1, 1}, {1, 1, 1}, 11, 0, 1}, {1, 0, 0}, 10, 1, 0}10, 0, 1}
if(c[o]==0 && c[1]==0 && c[2]==0)
cout<<"NO error in transmission:\n";

{
for(i=0;i<7;i++)
s[i][1)==c[1] && s[i[2]==c[2))
iffslijl0==c[0] &&

else
bit is q"<<6-i;
cout«"\n corrupted
cout"n After corruption:\n";

if(b[lil-=0)
b[il=1;
else
bfil=0;
cout«"\n code word:"
Enter
return
ch=ch;
n=i
printf"
check+=1;printf("64X\n",ch);
Generating
printf("6X\n",
check=check;
printf("-n");
if(ch==0)
any
printf("\n");
printf"0000\n
cout"Checksum
fori-0;i<n;i++)
check;
printf("%X\n",check);
printf("6X\n",check);
cout<<"Checksum is valid";
else
1\n");
thing:123
cout"Checksum
checksum
"); is invalid";
check);
is
unsigned short int checksum(char message|)
generated
unsigned short int tsize];
unsigned short int check=0; at
inti,j.n; it
coutee"Generating checksum"<cendl;
is"<cendl;
for(i-0,j-0;icstrlen(message);i=i+2,j++)
//Divide message into 16-bit field
tjl=message[il:
tl=tllk«8;
til=til+message[i+1];

printf("6X\n",t[i]);
check+=t[il;

3132
3300
000
6432
1
/3. Check sum
#include<iostream.h>

#include<conio.h>

#include<string.h>
#include<stdio.h>
#define size 100
unsigned short int checksum(char [l);
voidvalch(unsigned short intcheck,.char message);
int main()

clrscr();
cout<"Enter any thing:";
char message[size]={0};
cin>>message;
check=checksum(message);
unsigned short int
getch();
valch(check,message);

getch();
return 0;

voidvalch(unsigned short
intcheck,char message I)

t[size],ch=0;
unsigned short int
inti,j,n;
coutecendkcendl;
cout<"Checking checksum"<<endl;

for(i-0,j=0;i<strlen(message);i=i+2,j++)

tpl=message[lil;

tj]=tjl«<8;
til=ti]+message[i+1]

n=
for(i=0;i<n;i++)

printf("%X\n"t[i]);

cht=t[il;

printf("%X\n",check);
ch+=check;
printf"--\n");

printf("%X\n",ch);

printf" 1\n");
printf-====\n");

ch++;
printf("%X\n",ch);
1
0
1
110001014611

/5: Bit stuffing Algorithm


#include<iostream.h>

#include<conio.h>

void main()

int n=0,m=0,a[40],i,k;
cirscr();
ended with -1"<<endl;
coute"Enter the BINARY BIT stream

-1;
do

i++;
cin>>a[il;
mtt

while alil!=-1);
m=m-1;
i=0;
while a[i]!=-1)
while(n!=5)

iffalil==1)
n=ntt;
else
iffa[il==-1)
break;
else
n=0;
i++;
k=i;

if{n==5)
m=m+1;
//=m;
while(m!=k)

aml=a[m-1];
m-
6433
9BCC
Checksum is generated at it is
9BCC
Checking checksum
3232
3300
9BCC

FFFE
1

0
Checksum is valid

/4.Parity check
#include<iostream.h>

#include<conio.h>
void main()

clrscr(
inti,A[20],n=8,rem,m=0;
cout<"Enter the bit information:";
cout<<"Interms of 0 & 1:";
for(i-0;i<n;i++)

cin>>A[i];
if(A[il-=1)
m++;

rem=m%2;

if(rem=-0)
An+1]-0;
else
An+1]=1;
for(i=0;i<n+1;i++)

cout<<A[il;

getch();
)
OUT PUT
information in terms of 0 &1
Enter the bit
Z/6.Charecterstuffing

#include<iostream.h>

#include<conio. h>

# i n c l u d e < s t r i n g . h >

void main()

inti-0,j-0, n;
char a[20], b[20];
clrscr(O; \n";
"enter the string
cout<<

cin>>a5
n=strlen(a);
b[e]="d';
b[1]="l"
b[2]='e"
b[3]='s
b[4]="t'
b[5]="xs
j=6;
while(icn)
[i+1]=='1'&&a[i+2]== 'e')
if(a[ij== 'd '&&a

b[jl='d
b[j+1]='1';
b[j+2]='e';
j-j+3;

b[i]-a[i]1
i++
j+t;
b[jj='d';
b[j+1]="'l';
b[j+2]="e';
b[j+3]= s '
b[j+4]=t;
b[j+5]="x'
b[j+6]="\n'

cout"\n enter hte stuffing \n"


cout<b
getch()

Output
Enter the string
Strdlech
Dlestxstrdledlechdlestx

Enter the string


System
dlestxsystemdlestx
am]=0;
//m=l;
k++
i++;
n-0;

i=0;
cout<"AFTER BIT-STUFFING Mechanism";
while(a[i]!=-1)
cout<<a[i]<<";
i++
getch();

OUT PUT
Enter the binary bit stream ended with -1
111111111111011111101111110-1
After bit stuffing mechanism
1111101111101101111101011111010
Information sent successfully...

Go-back-N ARQ protocol


8. Toimplement
#include<dos.h>

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

voidcal();
void main()

inti,n,f.c,ans=0;
clrscr();
randomize();
abc:
coute"\n Enter the number of frames to send \n;
cin>>n;
f=random(n+2);
coute"\n Fames are going to transmitted \n";
for(i-1;i=n;i++)
coute«"\n Frame "<<ic<" Sending "
call);

for(i=1;i<=n;i++)
ifi-=f)

cout"\n\t Frame is lost "e<fe<" Resend it"


cal();
cout<"\n\t Acknowledgment is not recevied "<<i;
cal();
ans=1;

else

cout«"\n Acknowledgement Received "<<i;


call);
if ans)

ans=0;
cout<<"\n Resend Frames ";
coute<"\n RU Ready to Resend All The Frames Once
int resend Again(1-yes or 0-exit)\n";
cin>>resend;
if(resend==0)
I/7.Stop and wait ARQ protocol
#include<iostream.h>
#include<conio.h>
void main()

clrscr();
inti,j,f;
charch;
cout"Enter the total no of frames you want to send:";
cin>>f;
if(f=0)
cout"No, frames have been requested"'<<endl;
else

i-0;
j=0;
while(i<f)

cout<"Frame"<<i«<"is sent"<<endl;
coutee"is Acknowledgement"<<j<<"Received
? (y/n):";
cin>>ch;
ifch =yI|ch=='n')

i++;
jtt
else

cout<"Again..<<endl;

cout<"Information sent successfully..."<<endl;

getch();

OUT PUT want to send:3


Enter the total no of frames you
Frame0is sent

is Acknowledgement0Received
? (y/n}:y
Framelis sent

is Acknowledgement1Received
? (y/n):n
Frame2is sent
is Acknowledgement2Received ? (y/n):y
to send: ";
Enter the number of frames
cout<<"\n
cin>>n;
f=random(n+2); transmitted ";
Fames a r e going to
cout<<"\n
forli=1;ic=n;i++)
Frame "<<i«<"
Sending ";
cout"n
cal();
for(i-1;i=n;i++)
ifi-=f
is not recevied "<ci;
cout<"\n\t Acknowledgment
call);
ans=1;
else
{
cout"n Acknowledgement Received "<<i;
cal();
iffans)
ans-0;
again:
coute"n RU Ready to Resend The Selected Frames(1-yes or 0-exit)\n";
int resend;
cin>>resend;
if(resend==0)
goto xyz;
cout&"n U need to Send only "<kfe<" Frame only ";
cout««"\n Enter The Frame to be Send:";
intrsend;
cin>>rsend;
cout«"\n Sending the frame";
cal();
if f==rsend)
cout"n ACK "<<fe<" received Successfully....";
else
goto xyz;
forli=1;i<=n;i++)
cout"\n Frame "<<i<<" Sending";
cal();

forli=1;i=n;i++)
coute<"\n Acknowledgement Received"<<i«<" After
cal(); Resending"

else
cout"\n Ur Data successfully sended ";
cout<"\n Do U Want Another Fames (0-exit and 1-continue...)? ";
cin>>c;
if(c=1)
gotoabc;
else
goto xyz;
xyz:
cout"n Thanku";
cal;
getch();

voidcal()

for(inti-0;i<3;i++)

sleepi);
cout<""

9.Toimplement Selective-Repeat ARQ protoco


#includecdos.h>
#include<iostream.h>
#includecconio.h>
#include<stdlib.h>
voidcal();
void main()

inti,n,f.c,ans=0;
clrscr();
randomize();
abc:
{
k=j;
min=dist
if
path
siv]=1;
distiv]=0;
if(disti]<min
min=31999;
cin>>i>>j>>c;

forli-1;j<=n;j++)
cout"Enter
costlil0]=c;
++p]=v;
for(i=2;i=n-1;i++)
cost[i]bl==0)
for(j-1;j<=n;j++)
for(i=1;i<=n;i++)
cost[ilG]=31999;

&&
initial
s[i]l=1)
vertes:";
cin>>v;
cout<<v<endl;
shortest(v,n);

int shortest(intv, int n)

int min;
for(i=1;i<=n;i++)
{
slil=0;
dist[il=cost[v][il;

k=-1;

ifcostiv][k]K=dist[k))
p-1;
path ++p]=k;
forlj-1;j<=p:j++)
cout<<pathj]<<endl;

s[k]=1;
for(j=1;j<=n:j++)
distjl>=dist|k]+cost[k]li] && si]!=1)
if(cost[k]jl!=31999 &&
distl=dist[k]+cost[k]il:

getch();
cout"\n Ur Sended Frame Is Rejected Resend It";
cal(;
goto again;

else
cout"\n Ur Data successfully sended ";
cout<"\n Do U Want Another Fames (0-exit and 1-continue..)? ";
cin>>c
if(c=1)
gotoabc;
else
goto xyz;
xyz:
cout<"\n Thanku";
cal();
getch();

voidcal()

forlinti=0;i<3;i++)

sleep(i);
Couts<"."

/10. programfor Dijkstra's


//10 Dijkshatra's algorithm
#include<iostream.h>

#include<conio.h>

#include<stdio.h>

//using namespace std;


int shortest (int,int);
totcost,path[20],p;
intcost[10]|[10],dist[20],ijn,k.m.s[20],v,
void main)

int c;
cout<"Enter the no fo vertices:";

cin>>n;
cout<"Enter the no of edges:";
cin>>m;
cout«"\n Enter\nEdge cost"<<endl;

for(k-1;k<=m;k++)
int start;
int distance[MAX];
int predecessor[MAX];
public:
voidread graph();
void initialize();
void update();
void check();
void algorithm();

voidbell_ford::read_graph()
cout<"Enter the no. of nodes in the graph:";
cin>>n;
cout"Enter the adjacency matrix for the
for(inti-1;is=n;i++) graph ::\n"
for(int j=1;j<=n;j++)
cin>>graph[i]Gl;
cout<<"Enter the start vertex:";
cin>>start;

voidbell_ford::initialize()
for(inti=1;i<=n;it+)
distanceli]=INFINITY;
predecessor[il=0;
distance[start]=0;
voidbell_ford::update0
forlinti=1;is=n-1;i++)
for(int u=1;u<=n;u++)

for(int v=1;v<=n;v++)

if(graphfu]lv]!=0)

if(distancelv)>distance[u]+graphlu]lv))
distance/v]=distance[u]+graph[u]lvl
predecessorlv]=u;
OUT PUT
vertices:6
no fo
Enter the ofedges:11
the n o
Enter

Enter

Edge cost

2 50
1
3 45
4 10
1
2 3 10
4 15
5 30
4 1 10
4 5 15
5 2 20
5 3 35
6 5 3
Enter initial vertes:1

4
1
4

1
4

5
2

/11. Bellman-Ford algorithm

#include<iostream.h>

#include<stdlib.h>

#define MAX 20
#define INFINITY 9999

classbell_ford

private:
intr
int graph[MAX][MAX];
/* OUT PUT
:4
Enter the no. of nodes in the graph
matrix for the graph::
Enter the adjacency

1 2

2 4

5
2 3
2 6
3 1
1 1
Enter the start vertex ::2

and
There is no negative weight cycle ******

The final paths and the distacnes


******
are

path for node 1 is


1350->1
distance is 2

path for node 2 is:


2->2
distance is 0

path for node 3 is ::


1350->3
distance is 1

path for node 4 is


1350->4
distance is 5
voidbell_ford:check)
{ bell_fordobj;
for(intobj.algorithm();
u=1;u<=n;u++)

for(int v=1;v«=n;v++)

if(graphlullv]!=0)

if(distancefv)>distancefu]+graph{u][v))

Cout<<"does not exist's ";


return;

and\n'";
cout<<"\n\nThere is negative weight cycle
no
are ******\n\n";
the distacnes
cout"****** The final paths and
for(inti-1;ik=n;i++)

"<<i«<" is :\n";
cout<"path for node
intarf[MAX], k=1;
int j=i;
while(predecessor[i] l=0)

arrlkl=predecessorlil;

k++;
j=predecessorlil:

for(-k;k>0;k)|
coutecarrl[k]<<"->";

coutieendl;
c o u t e e " d i s t a n c e i s " e < d i s t a n c e [ i ] < < e n d k < e n d k < e n d l ;

v o i d b e l l _ f o r d : : a l g o r i t h m ( )

read graph();
initialize();
update();
check();

void main0
/ 13.implement Public-Key Encryption
#includesiostream.h>
#include<math.h>
#include<conio.h>
#include stdlib.h>
#include<string.h>
void main()

longintp.q,fn,e,s=0,d=1,n;
char m[100];
unsigned long int C=0,c;
clrscr(;
cout<"\n Enter the value of p & q \n ";
cin>>p>>q;
fn=p-1)* (q-1);
cout"\n Enter the encryption key \n";
cin>>e;
do

s=(d*e)%fn;
d++;

while(sl=1)
d=d-1;
cout"\n public key"<<e<<0<<")";
cout<<"\n private key("<<d<<0<<")";
cout<"\n Enter message \n";
cin>>m;
randomizel);
C-random(100000);
cout<<C;
cout"\n Enter Clipertext \n";
cin>>c;
coute"\n plane text \n";
if(C-=c)
cout<<m;
getch();

OUT PUT
Enterthe value of p &q
515
Enter the encryption key

public key(50)
private key(450)
/12. Implementencription algorithm
#include<iostream.h>
#include<conio.h>

#include<stdio.h>

class enc

intij,len;
char s[10],r[10],ch;
public:void get();
voiddisp();

void enc::get()

cout"Enter the string\n";


ch-getchar();
for(j-0;ch!=\n';j++)

sil-ch;
ch-getchar();

voidenc::disp()

len=j;
cout"After encryption\n";
forli-0;iklen;i++)

{
rlil=slij+4;
cout<r[il

void main()

clrscr();
encob;
ob.get();
ob.displ);
getch();

*OUT PUT
Enter the string
abdf
After encryption
efhj
for(i=0;i<e;i++)
m=(m*pt)%n;
m=m%n;
cout<<"\n\n Cipher Text::"<<m;
getch();
k=1;
for(i=0;i<sk;i++)
k=(k*m)%n;
k=k%n;
cout<<"\n\n Decrypted Text:"<<k;
getch()
return 0;

/*OUTPUT
Enter Value of p:: 3

Enter Value of q :: 6

Value of (P,q) :: 3 6
Value of n,fn : 18 10

Select Public Keye (1 < e< 10)::3

Private Key ::7

Enter Plain Text::4

Cipher Text::10
Decrypted Text ::10
Enter m e s s a g e

anu

4 2 9 4 9 5 1 7 8 1

Enter Clipertext
4294951781

plane text

anu

LL14 RSA cryptography algorithm


#include<iostream.h>
#include<conio.h>
#include<math.h>

int main(0

inti,j,k,p,9,flag.e,x,d;
clrscr();
cout<<"\n Enter Value of p:: ";
Cin>>P;
cout<<"\n Enter Value of q:: ";
cin>>g
int n=p*q;
intfn=(p-1)*(9-1);

(p,q) "<<p<<" "<<q;


cout<<"\n Value of
:

n,fn :: "<<n<<" "<<fn;


cout<<"\n Value of < e < "<<fn<<") ::";
cout<<"\n\n Select
Public Key e (1
cin>>e
intsk;
for(i=1;i>0;i++)

X=i*6

d=x%fn;
if(d==1)
cout<<"\n\n
Private Key::"<<i;
sk=i;
break;

intpt; .

cout<<"\n\n
Enter Plain Text:*;
cin> >pt;
int m=1;
int t iArray[jl;
iArrayil = iArraylj+ 1];

iArrayli+1] =t;

cnt 0;
for (int z =0; z <ilen; Z++)

for (intx = 0; x <= ilen; x++)

if ((iArraylz] + ilen * x) <= istrLen)

strENCR[cnt++] = strOriginallput[(iArray[z] + iLen * x) - 11:

strENCR[istrLen] ="\0;
//cout<<strENCR<< "\n\n"
/* ** ****** ************
Out put
*/
***** *********
****
intnl 1;
for(i 0; ikilen; i++)
cout<<outpass[i] <<" ";
cout< "\n----
cout "\n";
for (i = 0; i<istrLen; it+)

if(i== iLen nl)

cout "\n" <<strOriginallputfi] ««" ";


nl++;

else
cout<strOriginallput[il <«"
cout< "\n\n" << "Encrypted String: <<strENCR; "

**** ********************
***/
* Encryption is over, now going for decryption
************** ******************* k*****

cout< "\n;
charstrtmp[100];
cnt 0;
for (z =0; z <iLen; z++)
for (int x 0; x<= ilen; x++)
=
if (iArraylz] + iLen * x) <= (istrlen))

s t r t m p l i A r r a y l z ] + (iLen * x) - 1] = strENCR[cnt++};

strtmplistrLen] = "\0';

cout< "Decrypted String:" <<strtmp«« "\n\n";

OUT PUT
abcdefghijklmnopqrstuvwxyz
Enter the string without space:
Enter the crypt pass: planetcode
Planetcode

Abcdefghlj
KImnopqrst

UvWxyz
Encrypted string: cmwggiseoy.jtblvdnxhrakufpz

Decrypted string: abcdefghijklmnopqrstuvwxyz

Press any key to continue

/16.Shortest path
#include<iostream.h>

#includeconio.h>

void main()

//clrscr();
intn,c[10][10],d[10][101,p[10][10],ij,dist[10];

cout<<"Enter the no of nodes";

cin>>n; matrix"<<endl;
cout"Enter the cost

for(i=1;i<=n;i++)

forlj=1j<=n;j++t)

cin>>clilul
d[ilGl=c[ilGl:
plilGl=0;
distil=0;
for(int k=1;k<=n;k++)

forfi=1;i<=n;it+)
for(j-1;j=n;jt+)
if((dik}+dik]lül<dliG])

dlilul=di]lk}+d[k]ul;

Pili=k;
matrix is";
final distance
cout<<"n\n The

You might also like