Professional Documents
Culture Documents
Extra Practical 1
Extra Practical 1
Extra Practical 1
Extra Practical 1
Theory : -
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
Theory : -
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
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}
};
Output : -
SCET/CO/2023-24/Even/BTech-II/Sem-IV/Div-II 9