0% found this document useful (0 votes)
79 views19 pages

Practical No: 8: Code

The document describes an implementation of the shortest path algorithm. It defines a struct to store node state including predecessor, length, and label. It takes user input for the number of nodes and edges to build the relation and distance matrices. It initializes all nodes as tentative with infinity length except the target node. It then iterates to update distances and predecessors until it finds the shortest path from the source to target node.

Uploaded by

Amar Trapped
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views19 pages

Practical No: 8: Code

The document describes an implementation of the shortest path algorithm. It defines a struct to store node state including predecessor, length, and label. It takes user input for the number of nodes and edges to build the relation and distance matrices. It initializes all nodes as tentative with infinity length except the target node. It then iterates to update distances and predecessors until it finds the shortest path from the source to target node.

Uploaded by

Amar Trapped
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 19

Practical No: 8

/* Program to implement Bit stuffing and destuffing */

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:

Output 1 : ( Sender Side)

========Menu========
1.Sender Side ( Stuffing )
2.Receiver Side ( Destuffing )
3.Exit

Enter Your Choice : 1

Enter message to be sent : 101111110101

Message Sent After Stuffing :

1011111010101

Output 2 (Receiver Side)

========Menu========
1.Sender Side ( Stuffing )
2.Receiver Side ( Destuffing )
3.Exit

Enter Your Choice : 2

Enter recieved message : 1011111010101

Message Originally Sent By Sender ( After Destuffing) :

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;
}

bool hrecv(char str[],int skip,int index,int read)


{
int len;
int noof1s=0;
int charatpos='2';
char newstr[20];
//reverse(str);
int i,j;
int k;
for(i=index,j=0; i < 11;i=i+skip+1)
{
for(k=0;k<read;k++)
{
if(i+k<11)
{
newstr[j++]=str[i+k];
}
}
}
newstr[j]='\0';
len=j;

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++;
}
}

void copybits ( char str[],char sentstr[],int m,int r)


{
int i=0;
int j=0;
int raise=0;
for(i=0,j=0;j<m+r;)
{ if(j==pow(2,raise)-1)
{
raise++;
sentstr[j]='h';
j++;
continue;
}
else
{
sentstr[j]=str[i];
}
i++;
j++;
}
}
void reverse(char arr[])
{
int n,i,j;
n=strlen(arr);
char temp;
for(i=0,j=n-1;i<n/2,j>=n/2;i++,j--)
{ temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
arr[n]='\0';
}

void hamming(char sentstr[],int skip,int index,int read)


{
int i=0,j,countones=0;
int flag=0;
if(index==0)
i=2;
else
i=index+1;
for(;i<11;i=i+skip+1)
{
for(j=0;j<read;)
{ if(sentstr[i+j]=='1')
{ countones++;
}
j++;
if(index!=0 &&flag!=1 && j==read-1)
{
flag=1;
break;
}
}
i=i+j-1;
}

if(countones%2==1)
{
sentstr[index]='1';
}
else
{
sentstr[index]='0';
}
}
Output :

Output 1: (Sender Side, Encoding)

1. to send bits
2. to recive bits
3. exit
Your choice : 1

Enter the bits of the hamming code to be transmitted


1001101

Hamming Codeword 10011100101

Output 2 : (Reciever Side Without Error)

1. to send bits
2. to recive bits .
3. exit
Your choice : 2

Enter the bits of the hamming codeword recieved


10011100101

No Error in data

Original Data sent by sender : 1001101

Output 3 : (Receiver Side With Error )

1. to send bits
2. to recive bits .
3. exit
Your choice : 2

Enter the bits of the hamming codeword recieved


10010100101

ERROR in recieved data,(elements corresponding index 0 )


Practical No: 10
/* To implement Walsh Matrix Algorithm*/

Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>

int walsh[8][8]={1};
int walshnxt[8][8]={0};

void copy(int cnt)


{
int i, j, k;

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


{
for(j=0; j<cnt/2; j++)
{
if(i < cnt/2)
{
walshnxt[i][j]=walsh[i][j];
walshnxt[i][j+(cnt/2)]=walsh[i][j];
}
else
{
k=i-(cnt/2);
walshnxt[i][j]=walsh[k][j];
if(walsh[k][j] == 1)
walshnxt[i][j+(cnt/2)]=-1;
else
walshnxt[i][j+(cnt/2)]=1;
}
}
}

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


{
for(j=0; j<7; j++)
{
walsh[i][j]=walshnxt[i][j];
}
}

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;

for(i=2; i <= pow(2, num);i=pow(2, cntr))


{
copy(i);
cntr++;
}

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++)
{

printf("\t %d", walshnxt[i][j]);


}
printf("\n");
}
getch();
}

Output:

Enter the power of two that you want to display : 2

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;
}

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 ");
}

for(p=&st[0]; p<&st[node]; p++)


{ p->predecessor=-1;
p->length=INFINITY;
p->label=tentative;
}
fflush(stdin);
printf("\n\nEnter the start and destination node: ");
scanf("%d %d", &s, &t);
s--; t--;
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\nShortest Path: ");
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]);
}getch();
return 0;
}

Output:

Enter total no of nodes and edges: 4 5

Enter source, destination and distance for edge 1: 1 2 2

Enter source, destination and distance for edge 2: 2 3 10

Enter source, destination and distance for edge 3: 3 4 2

Enter source, destination and distance for edge 4: 4 1 8

Enter source, destination and distance for edge 5: 4 2 7


Relation Matrix:

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

Enter the start and destination node: 1 3

Shortest Path: 1 ---8---> 4 ---2---> 3


Practical No: 12

/* Implementation of Sink Tree*/

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:

Enter total no of nodes and edges: 4 5


Enter source, destination and distance for edge 1: 1 2 10
Enter source, destination and distance for edge 2: 2 3 8
Enter source, destination and distance for edge 3: 3 4 3
Enter source, destination and distance for edge 4: 4 1 4
Enter source, destination and distance for edge 5: 1 3 20
Relation Matrix:

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

Enter the destination node: 3


:- Sink Tree for Router 3 :-

From Router 1: 1 ---4---> 4 ---3---> 3

From Router 2: 2 ---8---> 3

From Router 4: 4 ---3---> 3


Practical No: 13
/*To implement RSA Algorithm to encrypt text and decrypt it.*/

Code:

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

long double mod(long double dividend,long double divisor)


{ for(long double i = 1; i <= ((dividend / 2) + 1); i++)
{
if((divisor * i) > dividend)
break;
}
return dividend - (divisor * (i - 1));
}
int main(int argc,char *argv[])
{
char text[255]; // Array for input;
clrscr();
if(argc > 2)
{ cout<<"Improper input";
cout<<endl<<argv[2];
return 1;
}
else if(argc == 2)
{ strcpy(text,argv[2]);
}
else
{ cout<<"Enter the Capital Alphabets string without space"
<<endl<<"-------------------------------------------------"
<<endl;
cin>>text;
}

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);

for(int i = 3; i < n;i += 2)


{ if((long)t % (long)i != 0)
{
e = i;
break;
}
}
if(e == 0)
{ cout<<"Improper Termination Illegal Process\n";
return 2;
}
d = 0;
for(int x = 1; x < e - 1; x++)
{
if(((1 + (x * (long)t)) % (long)e) == 0)
{
d = (1 + (x * t)) / e;
break;
}
}

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


{
cipher[i] = (long)pow((int)text[i] - 64 ,(long)e) % (long)n;
}
cipher[i] = '\0';
cout<<endl<<"The Cipher code so formed is"<<endl;
for(i = 0; i < l; i++)
cout<<cipher[i]<<" ";
cout<<"\nDecrypting the cipher text we get"<<endl;
strcpy(text," ");
long double p;
for(i = 0; i < l; ++i)
{ p = (long double)(pow(cipher[i],d));
text[i] = mod((long double)p,(long double)n) + 64;
}
text[i] = '\0';
cout<<text;
getch();
return 0;
}

Output:

Enter the Capital Alphabets string without space


----------------------------------------------------
RDXISCOOL
1
The Cipher code so formed is
39 60 69 37 68 31 71 71 12
Decrypting the cipher text we get
RDXISCOOL
Practical No.14
/* To implement a server client application.*/

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:

Hello Server I am Client

Hello Client I am Server

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy