Extra Practical 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024

Enrollment No: ET22BTCO078 Name: Swayam Parekh

Extra Practical 1

Problem Statement : - Implement Vertical Redundancy Check


(VRC), Longitudinal Redundancy Check (LRC) and
Two-dimensional Parity check - error detection methods.

1) Vertical Redundancy Check (VRC) : -

Theory : -

Vertical Redundancy Check is also known as Parity Check. In this method, a


redundant bit also called parity bit is added to each data unit. This method
includes even parity and odd parity. Even parity means the total number of 1s
in data is to be even and odd parity means the total number of 1s in data is to
be odd. Example – If the source wants to transmit data unit 1100111 using
even parity to the destination. The source will have to pass through Even Parity
Generator.
Parity generator will count the number of 1s in the data unit and will add a
parity bit. In the above example, the number of 1s in the data unit is 5, the
parity generator appends a parity bit 1 to this data unit making the total
number of 1s even i.e 6 which is clear from the above figure. Data along with
parity bits is then transmitted across the network. In this case, 11001111 will
be transmitted. At the destination, This data is passed to the parity checker at
the destination. The number of 1s in data is counted by a parity checker. If the
number of 1s count out to be odd, e.g. 5 or 7 then the destination will come to
know that there is some error in the data. The receiver then rejects such an
erroneous data unit.

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 1
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh

Code:-

#include <stdio.h>
#define ROWS 4
#define COLS 4
int calculateRowParity(int row[], int numCols)
{
int count = 0;
for (int i = 0; i < numCols; i++)
{
if (row[i] == 1)
{
count++;
}
}
return count % 2 == 0 ? 0 : 1; // Return 1 if count of 1's is odd, 0 otherwise
}
void verticalRedundancyCheck(int matrix[][COLS], int numRows, int numCols)
{
int parity[numCols];
for (int j = 0; j < numCols; j++)
{
int count = 0;
for (int i = 0; i < numRows; i++)
{
if (matrix[i][j] == 1)
{
count++;
}
}
parity[j] = count % 2 == 0 ? 0 : 1; // Store the parity for this column
}
printf("Original Matrix:\n");
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

printf("\nParity Bits:\n");
for (int j = 0; j < numCols; j++)
{
printf("%d ", parity[j]);
}

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 2
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh
printf("\n\n");
printf("Error Status:\n");
for (int j = 0; j < numCols; j++)
{
int error = calculateRowParity(matrix[j], numRows);
printf("Column %d: %s\n", j + 1, error == parity[j] ? "No Error" : "Error
Detected");
}
}

int main() {
int matrix[ROWS][COLS] = {
{1, 0, 1, 0},
{1, 1, 1, 1},
{1, 0, 0, 1},
{0, 1, 0, 1}
};
verticalRedundancyCheck(matrix, ROWS, COLS);
return 0;
}

Output : -

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 3
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh

2) Longitudinal Redundancy Check (LRC) : -

Theory : -

Longitudinal Redundancy Check (LRC) is also known as 2-D parity check. In


this method, data which the user wants to send is organised into tables of

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 4
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh
rows and columns. A block of bits is divided into a table or matrix of rows and
columns. In order to detect an error, a redundant bit is added to the whole
block and this block is transmitted to the receiver. The receiver uses this
redundant row to detect error. After checking the data for errors, the receiver
accepts the data and discards the redundant row of bits.

The LRC received by the destination does not match with the newly corrupted
LRC. The destination comes to know that the data is erroneous, so it discards
the data.

Code : -

#include <stdio.h>
#define NUM_ROWS 4
#define NUM_COLS 4
int calculateLRC(int matrix[][NUM_COLS], int numRows, int numCols)
{
int lrc = 0;
for (int j = 0; j < numCols; j++)
{
int columnXOR = 0;
for (int i = 0; i < numRows; i++)
{
columnXOR ^= matrix[i][j];
}
lrc ^= columnXOR;
}

return lrc;
}
int main()
{
int matrix[NUM_ROWS][NUM_COLS] =
{
{1, 0, 1, 0},
{0, 1, 1, 0},
{1, 0, 1, 1},
{0, 1, 0, 1}
};
int lrc = calculateLRC(matrix, NUM_ROWS, NUM_COLS);
printf("Original Matrix:\n");
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 5
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh
}
printf("\nLRC: %d\n", lrc);
return 0;
}

Output : -

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 6
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh

3) Two-dimensional Parity check : -

Theory : -

Error is a condition when the receiver’s information does not match the
sender’s information. During transmission, digital signals suffer from
noise that can introduce errors in the binary bits traveling from sender
to receiver. That means a 0 bit may change to 1 or a 1 bit may change
to 0. Data (Implemented either at the Data link layer or Transport Layer
of the OSI Model) may get scrambled by noise or get corrupted
whenever a message is transmitted. To prevent such errors,
error-detection codes are added as extra data to digital messages.
This helps in detecting any errors that may have occurred during
message transmission.

Code : -

#include <stdio.h>
#define ROWS 4
#define COLS 4
int calculateRowParity(int row[], int numCols)
{
int count = 0;
for (int i = 0; i < numCols; i++) {
if (row[i] == 1) {
count++;
}
}

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 7
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh
return count % 2 == 0 ? 0 : 1;
}
int calculateColumnParity(int matrix[][COLS], int numRows, int colIndex)
{
int count = 0;
for (int i = 0; i < numRows; i++)
{
if (matrix[i][colIndex] == 1)
{
count++;
}
}
return count % 2 == 0 ? 0 : 1; // Return 1 if count of 1's is odd, 0 otherwise
}
void twoDimensionalParityCheck(int matrix[][COLS], int numRows, int numCols)
{
int rowParity[numRows];
int colParity[numCols];
for (int i = 0; i < numRows; i++)
{
rowParity[i] = calculateRowParity(matrix[i], numCols);
}
for (int j = 0; j < numCols; j++)
{
colParity[j] = calculateColumnParity(matrix, numRows, j);
}
printf("Original Matrix:\n");
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
printf("%d ", matrix[i][j]);
}
printf("| %d\n", rowParity[i]);
}
printf("--------\n");
for (int j = 0; j < numCols; j++)
{
printf("%d ", colParity[j]);
}
printf("\n");
int errorFlag = 0;
for (int i = 0; i < numRows; i++)
{
if (rowParity[i] != calculateRowParity(matrix[i], numCols))
{
printf("Error in row %d\n", i + 1);
errorFlag = 1;
}

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 8
Subject code: BTCO13401 Subject Name: Computer Network Date: 08/02/2024
Enrollment No: ET22BTCO078 Name: Swayam Parekh
}
for (int j = 0; j < numCols; j++) {
if (colParity[j] != calculateColumnParity(matrix, numRows, j))
{
printf("Error in column %d\n", j + 1);
errorFlag = 1;
}
}

if (!errorFlag)
{
printf("No errors detected.\n");
}
}
int main() {
int matrix[ROWS][COLS] =
{
{1, 0, 1, 1},
{0, 1, 1, 0},
{1, 1, 0, 1},
{0, 1, 0, 1}
};

twoDimensionalParityCheck(matrix, ROWS, COLS);


return 0;
}

Output : -

SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 9

You might also like