From 4c88e911285ec9fddaaa713f9448ba223273c658 Mon Sep 17 00:00:00 2001 From: array-in-a-matrix Date: Tue, 6 Jul 2021 03:51:50 -0400 Subject: [PATCH] error detection (need 2 isolate error cord & fix) --- index.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 142 insertions(+), 25 deletions(-) diff --git a/index.c b/index.c index f5ddb27..26abaa2 100644 --- a/index.c +++ b/index.c @@ -6,14 +6,21 @@ #define LOWER 0 #define UPPER 3 -void print_matrix(int matrix[GRID_SIZE][GRID_SIZE]); int random_number(int lower, int upper); +void print_matrix(int matrix[GRID_SIZE][GRID_SIZE]); void induce_noise(int matrix[GRID_SIZE][GRID_SIZE]); + void parity_check_1(int matrix[GRID_SIZE][GRID_SIZE]); void parity_check_2(int matrix[GRID_SIZE][GRID_SIZE]); void parity_check_3(int matrix[GRID_SIZE][GRID_SIZE]); void parity_check_4(int matrix[GRID_SIZE][GRID_SIZE]); +void error_check_1(int matrix[GRID_SIZE][GRID_SIZE]); +void error_check_2(int matrix[GRID_SIZE][GRID_SIZE]); +void error_check_3(int matrix[GRID_SIZE][GRID_SIZE]); +void error_check_4(int matrix[GRID_SIZE][GRID_SIZE]); + + int main() { srand(time(0)); @@ -25,26 +32,33 @@ int main() {1, 0, 1, 1}}; print_matrix(message); - printf("\n"); parity_check_1(message); - printf("\n"); - print_matrix(message); - printf("\n"); parity_check_2(message); - printf("\n"); - print_matrix(message); - printf("\n"); parity_check_3(message); - printf("\n"); - print_matrix(message); - printf("\n"); parity_check_4(message); printf("\n"); print_matrix(message); + printf("\n"); + printf("\n"); + printf("Message now corrupt!\n"); + induce_noise(message); + print_matrix(message); + printf("\n"); + printf("\n"); + error_check_1(message); + error_check_2(message); + error_check_3(message); + error_check_4(message); return 0; }; +int random_number(int lower, int upper) +{ + int num = (rand() % (upper - lower + 1)) + lower; + return num; +}; + void print_matrix(int matrix[GRID_SIZE][GRID_SIZE]) { for (int i = 0; i < GRID_SIZE; i++) @@ -57,12 +71,6 @@ void print_matrix(int matrix[GRID_SIZE][GRID_SIZE]) } }; -int random_number(int lower, int upper) -{ - int num = (rand() % (upper - lower + 1)) + lower; - return num; -}; - void induce_noise(int matrix[GRID_SIZE][GRID_SIZE]) { int random_X = random_number(LOWER, UPPER); @@ -81,6 +89,7 @@ void induce_noise(int matrix[GRID_SIZE][GRID_SIZE]) }; }; + void parity_check_1(int matrix[GRID_SIZE][GRID_SIZE]) { int counter = 0; @@ -106,12 +115,10 @@ void parity_check_1(int matrix[GRID_SIZE][GRID_SIZE]) if ((counter % 2) == 0) { - printf("even number of 1s\n"); matrix[0][1] = 0; } else { - printf("odd number of 1s\n"); matrix[0][1] = 1; }; }; @@ -141,12 +148,10 @@ void parity_check_2(int matrix[GRID_SIZE][GRID_SIZE]) if ((counter % 2) == 0) { - printf("even number of 1s\n"); matrix[0][2] = 0; } else { - printf("odd number of 1s\n"); matrix[0][2] = 1; }; }; @@ -176,12 +181,10 @@ void parity_check_3(int matrix[GRID_SIZE][GRID_SIZE]) if ((counter % 2) == 0) { - printf("even number of 1s\n"); matrix[1][0] = 0; } else { - printf("odd number of 1s\n"); matrix[1][0] = 1; }; }; @@ -211,12 +214,126 @@ void parity_check_4(int matrix[GRID_SIZE][GRID_SIZE]) if ((counter % 2) == 0) { - printf("even number of 1s\n"); matrix[2][0] = 0; } else { - printf("odd number of 1s\n"); matrix[2][0] = 1; }; +}; + + +void error_check_1(int matrix[GRID_SIZE][GRID_SIZE]){ + int counter = 0; + int col_1[GRID_SIZE], col_3[GRID_SIZE]; + + for (int i = 0; i < GRID_SIZE; i++) + { + col_1[i] = matrix[i][1]; + col_3[i] = matrix[i][3]; + }; + + for (int i = 0; i < GRID_SIZE; i++) + { + if (col_1[i] == 1) + { + counter++; + }; + if (col_3[i] == 1) + { + counter++; + }; + }; + + if((counter % 2) != 0){ + printf("Error in col 1 or col 3\n"); + }else{ + printf("Message is unharmed.\n"); + }; +}; + +void error_check_2(int matrix[GRID_SIZE][GRID_SIZE]){ + int counter = 0; + int col_2[GRID_SIZE], col_3[GRID_SIZE]; + + for (int i = 0; i < GRID_SIZE; i++) + { + col_2[i] = matrix[i][2]; + col_3[i] = matrix[i][3]; + }; + + for (int i = 0; i < GRID_SIZE; i++) + { + if (col_2[i] == 1) + { + counter++; + }; + if (col_3[i] == 1) + { + counter++; + }; + }; + + if((counter % 2) != 0){ + printf("Error in col 2 or col 3\n"); + }else{ + printf("Message is unharmed.\n"); + }; +}; + +void error_check_3(int matrix[GRID_SIZE][GRID_SIZE]){ + int counter = 0; + int row_1[GRID_SIZE], row_3[GRID_SIZE]; + + for (int i = 0; i < GRID_SIZE; i++) + { + row_1[i] = matrix[1][i]; + row_3[i] = matrix[3][i]; + }; + + for (int i = 0; i < GRID_SIZE; i++) + { + if (row_1[i] == 1) + { + counter++; + }; + if (row_3[i] == 1) + { + counter++; + }; + }; + if((counter % 2) != 0){ + printf("Error in row 1 or row 3\n"); + }else{ + printf("Message is unharmed.\n"); + }; +}; + +void error_check_4(int matrix[GRID_SIZE][GRID_SIZE]){ + int counter = 0; + int row_2[GRID_SIZE], row_3[GRID_SIZE]; + + for (int i = 0; i < GRID_SIZE; i++) + { + row_2[i] = matrix[2][i]; + row_3[i] = matrix[3][i]; + }; + + for (int i = 0; i < GRID_SIZE; i++) + { + if (row_2[i] == 1) + { + counter++; + }; + if (row_3[i] == 1) + { + counter++; + }; + }; + + if((counter % 2) != 0){ + printf("Error in row 2 or row 3\n"); + }else{ + printf("Message is unharmed.\n"); + }; }; \ No newline at end of file