Assignment 1
Assignment 1
ASSIGNMENT:1
Objective:
To implement parity check for error detection.
Source code:
# include<bits/stdc++.h>
# define bool int
using namespace std;
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
int main()
{
unsigned int n = 12;
cout<<"Parity of no "<<n<<" = "<<(getParity(n)? "odd": "even");
getchar();
return 0;
}
OUTPUT:
Objective:
To implement LRC for error detection.
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int l1,bit[100],count=0,i,choice;
printf("Enter the length of data stream: ");
scanf("%d",&l1);
printf("\nEnter the data stream ");
for(int i=0;i<l1;i++) {
scanf("%d",&bit[i]);
if(bit[i]==1)
count=count+1;
}
printf("Number of 1's are %d",count);
printf("\nEnter the choice to implement parity bit");
printf("\n1-Sender side\n2-Receiver side\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(count%2==0)
bit[l1]=0;
else
bit[l1]=1;
printf("\nThe data stream after adding parity bit is\n");
for(i=0;i<=l1;i++)
printf("%d",bit[i]);
break;
case 2:
if(count%2==0)
printf("There is no error in the received data stream");
else
printf("There is error in the received data stream");
break;
default:
printf("Invalid choice");
break;
}
return 0;
}
OUTPUT :
Objective:
Write a program to implement VRC for error detection.
Source code:
#include<stdio.h>
#include<conio.h>
int binary(int);
void parity(int[]);
int arr[9],arr1[9];
char chr;
int temp,temp1,i;
void parity(int a[])
{
int count;
count=0;
for(i=0;i<8;i++)
{
if(a[i]==1)
count++;
}
if(count%2==0)
a[8]=0;
else
a[8]=1;
count=0;
printf("Receiver Side :\n");
printf("\n\nVRC : \n");
for(i=0;i<9;i++)
{
if(i==8)
printf(" | ");
printf("%d ",a[i]);
}
}
int binary(int x)
{
int rem;
int ctr=0,i=1;
do
{
rem=x%2;
arr[i]=rem;
if(rem==1)
{
ctr++;
}
x=x/2;
i++;
}
while(x!=0);
if(ctr%2==0)
{
arr[0]=0;
}
else
{
arr[0]=1;
}
return(0);
}
void main()
{ char chr1;
printf("Enter Data :");
scanf("%c %c",&chr,&chr1);
temp=chr;
binary(temp);
printf("\nAscii value is : %d\n",temp);
printf("\nBinary Form : ");
for(i=0;i<8;i++)
{
arr1[i]=arr[i];
printf("%d ",arr[i]);
}
printf("\n");
parity(arr);
temp1=chr1;
binary(temp1);
printf("\n\nAscii value is : %d\n",temp1);
printf("\nBinary Form : ");
for(i=0;i<8;i++)
{
printf("%d ",arr[i]);
}
parity(arr);
getch();
}
OUTPUT: