1. Simulate Even Parity Generator and Checker.
// C program to find parity
// of an integer
# include <stdio.h>
# define bool int
/* Function to get parity of number n. It returns 1
if n has odd parity, and returns 0 if n has even
parity */ bool getParity(unsigned int n)
{
bool parity = 0;
while (n) {
parity = !parity; n
= n & (n - 1);
}
return parity;
}
/* Driver program to test getParity() */
int main() {
unsigned int n = 3;
printf("Parity of no %d = %s", n,
(getParity(n)? "odd": "even"));
getchar();
return 0;
}
OUTPUT:
Parity of no 3 = even
2. Simulate Two dimensional parity generator and
checker.
// C program to find parity
// of an integer
# include <stdio.h>
# define bool int
/* Function to get parity of number n. It returns 1
if n has odd parity, and returns 0 if n has even
parity */ bool getParity(unsigned int n)
{ bool parity =
0; while (n)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
/* Driver program to test getParity() */
int main() { unsigned int n = 7;
printf("Parity of no %d = %s", n,
(getParity(n)? "odd": "even"));
getchar();
return 0;
}
OUTPUT:
Parity of no 7 = odd
3. Simulate checksum generator and checker.
/*
* C program to implement Checksum
*/
#include<stdio.h>
#include<math.h>
int sender(int arr[10],int
n)
{ int checksum,sum=0,i;
printf("\n****SENDER SIDE****\n");
for(i=0;i<n;i++) sum+=arr[i];
printf("SUM IS: %d",sum); checksum=~sum;
//1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
return checksum;
} void receiver(int arr[10],int n,int
sch)
{
int checksum,sum=0,i;
printf("\n\n****RECEIVER SIDE****\n");
for(i=0;i<n;i++) sum+=arr[i];
printf("SUM IS:%d",sum); sum=sum+sch;
checksum=~sum; //1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
} void main() { int n,sch,rch; printf("\nENTER SIZE OF THE
STRING:"); scanf("%d",&n); int arr[n]; printf("ENTER THE
ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:\n"); for(int
i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sch=sender(arr,n);
receiver(arr,n,sch);
}
OUTPUT:
ENTER SIZE OF THE STRING:2
ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:
1011
0111
****SENDER SIDE****
SUM IS: 1122
CHECKSUM IS:-1123
****RECEIVER SIDE****
SUM IS:1122
CHECKSUM IS:0
4. Simulate Hamming Code Method.
#include<stdio.h>
// C program for the above approach
#include <math.h>
#include <stdio.h>
// Store input bits int
input[32];
// Store hamming code int
code[32]; int ham_calc(int,
int); void solve(int input[],
int);
// Function to calculate bit for
// ith position
int ham_calc(int position, int c_l)
{ int count = 0, i,
j; i = position - 1;
// Traverse to store Hamming Code
while (i < c_l) {
for (j = i; j < i + position; j++)
{
// If current boit is 1
if (code[j] == 1)
count++;
}
// Update i i
= i + 2 * position;
} if (count %
2 == 0) return
0; else
return 1;
}
// Function to calculate hamming code
void solve(int input[], int n)
{ int i, p_n = 0, c_l, j,
k; i = 0;
// Find msg bits having set bit //
at x'th position of number while (n >
(int)pow(2, i) - (i + 1)) { p_n++;
i++;
}
c_l = p_n + n;
j = k =
0;
// Traverse the msgBits
for (i = 0; i < c_l; i++) {
// Update the code if (i
== ((int)pow(2, k) - 1)) {
code[i] = 0; k++;
}
// Update the code[i] to the
// input character at index j
else { code[i] =
input[j]; j++;
}
}
// Traverse and update the
// hamming code for (i = 0;
i < p_n; i++) {
// Find current position
int position = (int)pow(2, i);
// Find value at current position
int value = ham_calc(position, c_l);
// Update the code
code[position - 1] = value;
}
// Print the Hamming Code
printf("\nThe generated Code Word is: ");
for (i = 0; i < c_l; i++) {
printf("%d", code[i]);
}
}
// Driver Code void
main()
{
// Given input message Bit
input[0] = 0; input[1] =
1; input[2] = 1;
input[3] = 1;
int N = 4;
// Function Call
solve(input, N);
}
OUTPUT:
The generated Code Word is: 0001111
5. Simulate Cyclic Redundancy Check (CRC) error
detection algorithm for noisy channel.
#include<stdio.h>
char data[20],div[20],temp[4],total[100];
int i,j,datalen,divlen,len,flag=1; void
check(); int main() { printf("Enter the
total bit of data:");
scanf("%d",&datalen);
printf("\nEnter the total bit of divisor");
scanf("%d",&divlen); len=datalen+divlen-1;
printf("\nEnter the data:");
scanf("%s",&data);
printf("\nEnter the divisor");
scanf("%s",div);
for(i=0;i<datalen;i++)
{
total[i]=data[i];
temp[i]=data[i];
} for(i=datalen;i<len;i++) //padded with zeroes
corresponding to divlen total[i]='0'; check();
// check for crc for(i=0;i<divlen;i++) // append crc
output (remainder) at end of temp temp[i+datalen]=data[i];
printf("\ntransmitted Code Word:%s",temp); printf("\n\nEnter the
received code word:"); scanf("%s",total); check();
for(i=0;i<divlen-1;i++) if(data[i]=='1')
{ flag=0; break;
} if(flag==1) printf("\nsuccessful!!");
else printf("\nreceived code word contains
errors...\n");
}
void check()
{
for(j=0;j<divlen;j++)
data[j]=total[j];
while(j<=len)
{ if(data[0]=='1') // in XOR ans remains as it is
except in case of 1 for(i = 1;i <divlen ; i++)
data[i] = (( data[i] == div[i])?'0':'1'); for(i=0;i<divlen-1;i++)
// left shift data word by 1 after div data[i]=data[i+1];
data[i]=total[j++]; // replace empty right by total
}
}
OUTPUT:
Enter the total bit of data:7
Enter the total bit of divisor4
Enter the data:1001010
Enter the divisor1011
transmitted Code Word:1001010111
Enter the received code word:1001010100
received code word contains errors...
6. Simulate and implement stop and wait protocol
for noisy channel.
7. Simulate and implement go back n sliding window
protocol.
#include<stdio.h> int main() {
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1) { for( i = 0;
i < windowsize; i++)
{ printf("Frame %d has been
transmitted.\n",sent); sent++;
if(sent == windowsize) break;
} printf("\nPlease enter the last Acknowledgement
received.\n"); scanf("%d",&ack);
if(ack
== windowsize)
break; else
sent = ack;
} return
0;
}
OUTPUT:
enter window size
Frame 0 has been transmitted.
Frame 1 has been transmitted.
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.
Please enter the last Acknowledgement received.
8. Simulate and implement Selective Repeat Sliding
Window Protocol.
#include<stdio.h>
int main() { int
w,i,f,frames[50];
printf("Enter window size:
"); scanf("%d",&w);
printf("\nEnter number of frames to transmit:
"); scanf("%d",&f);
printf("\nEnter %d frames:
",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in
the following manner (assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for
acknowledgement sent by the receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by
sender\n\n"); } else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by
sender\n");
return 0;
}
OUTPUT:
Enter window size: 3
Enter number of frames to transmit: 5
Enter 5 frames: 12 5 49 4 6
With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
12 5 49
Acknowledgement of above frames sent is received by sender
46
Acknowledgement of above frames sent is received by sender
9. Simulate and implement distance vector routing
algorithm.
#include<stdio.h>
struct node {
unsigned dist[20];
unsigned from[20];
}rt[10]; int main() {
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the
number of nodes : ");
scanf("%d",&n);
printf("\nEnter the
cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j
]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i]
[j];
rt[i].from[j]=j;
} do { count=0;
for(i=0;i<n;i++) for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[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<n;i++)
{ printf("\n\nState value for router %d is
\n",i+1); for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d
Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}
OUTPUT:
Enter the number of nodes : 4
Enter the cost matrix :
0 3 5 99
3 0 99 1
5402
99 1 2 0
State value for router 1 is
node 1 via 1 Distance0 node
2 via 2 Distance3 node 3 via
3 Distance5 node 4 via 2
Distance4
State value for router 2 is
node 1 via 1 Distance3 node
2 via 2 Distance0 node 3 via
4 Distance3 node 4 via 4
Distance1
State value for router 3 is
node 1 via 1 Distance5 node
2 via 4 Distance3 node 3 via
3 Distance0 node 4 via 4
Distance2
State value for router 4 is
node 1 via 2 Distance4 node 2 via 2 Distance1 node 3 via 3 Distance2 node 4 via 4 Distance0