hamming code
- October 2nd, 2012
- By Lettergram
- Write comment
If you wish to download:
Hamming Code.exe
Hamming Code.c – Windows Version
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 700
//This is the word encoder
void wordEncoder(int uncoded[][8],int ROWS,int COLS){
int i, j, k, s, t;
int J[8][8];
int I[8][8];
int c[SIZE][COLS];
int W[SIZE][2*COLS];
i = 0;
j = 1;
k = 1;
s = 5;
J[0][0] = 0;
//This generates a J matrix
while( i < COLS ){
k = 1;
if(i < 4){
while( j < COLS ){
J[i][j] = k;
if(s == j){
k = 0;
}else if(s > j){
k = 1;
}
j++;
}
j = 0;
s--;
}else{
J[i][6] = 1;
while(j < 6){
J[i][j] = k;
if(s < j){
k = 1;
}else if(s > j){
k = 0;
}
j++;
}
j = 0;
s++;
}
i++;
}
i = j = 0;
k = 0;
//End of generation of J matrix
//This generates an I matrix
while(i < COLS){
while(j < COLS){
if(j != k){
I[i][j] = 0;
}else{
I[i][j] = 1;
}
j++;
}
k++;
j = 0;
i++;
}
//End of generation of I matrix
printf("\nThe generated A matrix [I|J] is : \n\n");
for ( i=0; i < COLS; i++){
printf("[");
for (j=0; j < COLS; j++){
printf("%d", J[i][j]);
}
printf(" ");
for (j=0; j < COLS; j++){
printf("%d", I[i][j]);
}
printf("]\n");
}
//The encoding process AC = w
printf("\n\nThe encoded text (A*C[i]) is: ");
k = i = j = s = t = 0;
while(i < ROWS){
t++;
printf("\n\nInitial value for C[%d]: ", t);
while(k < COLS){
printf("%d", uncoded[i][k]);
k++;
}
printf("\nThe new number is w[%d]: ", t);
//multiplies each row of uncoded (C), by each column of A
while(s < 14){
if(s < 7){
while(j < COLS){
W[i][s] += (uncoded[i][j] * I[j][s]);
j++;
}
}else{
while(j < COLS){
W[i][s] += (uncoded[i][j] * J[j][s-7]);
j++;
}
}
W[i][s] = (W[i][s] % 2);
printf("%d", W[i][s]);
s++;
j = 0;
}
k = 0;
s = 0;
i++;
}
//End of encoding
return;
}
//This is the word decoder
void decoder(){
int i, j, k, s, t;
int COLS = 7;
int ROWS;
int J[8][8];
int I[8][8];
int c[SIZE][2*COLS];
int W[SIZE][2*COLS];
i = 0;
j = 0;
k = 1;
s = 5;
t = 0;
int ch;
char ascii;
printf("\n\n");
printf("Please type 1 if you would like to use the generated J, or 2 to type your own:\n");
scanf("%d", &k);
if(k == 1){
//This generates the J matrix from above
while( i < COLS ){
k = 1;
if(i < 4){
while( j < COLS ){
J[i][j] = k;
if(s == j){
k = 0;
}else if(s > j){
k = 1;
}
j++;
}
j = 0;
s--;
}else{
J[i][6] = 1;
while(j < 6){
J[i][j] = k;
if(s < j){
k = 1;
}else if(s > j){
k = 0;
}
j++;
}
j = 0;
s++;
}
i++;
}
//This is the end of the generation of the J matrix
}else if(k == 2){
printf("\nPlease enter your J matrix (must be a 7x7 matrix)\n");
while(i < 7){
printf("\nJ[%d]: ", i);
while((ch = getchar()) != '\n'){
if(49 == ch){
ch = 1;
}else if(48 == ch){
ch = 0;
}
J[i][j] = ch;
j++;
}
i++;
j = 0;
}
}else{
printf("\nUnkown option, failure!\n");
return;
}
//This generates and I matrix
printf("\nGenerating your I matrix for you!\n");
i = j = k = 0;
while(i < COLS){
while(j < COLS){
if(j != k){
I[i][j] = 0;
}else{
I[i][j] = 1;
}
j++;
}
i++;
j = 0;
k++;
}
//End of I matrix generation
//Prints the A* matrix
printf("\nThe generated A* matrix [J/I] is : \n\n");
for ( i=0; i < COLS; i++){
printf("[");
for (j=0; j < COLS; j++){
printf("%d", J[i][j]);
}
printf("]\n");
}
printf("\n");
for ( i=0; i < COLS; i++){
printf("[");
for (j=0; j < COLS; j++){
printf("%d", I[i][j]);
}
printf("]\n");
}
printf("\n\nPlease enter how many characters you have in your sentance?\n");
printf("Please include puncuation and spaces: ");
scanf("%d", &ROWS);
i = 0;
t = 1;
j = 0;
k = getchar(); //Because I used scanf I have to return processor use to getchar();
//Collects each character in binary
while(i < ROWS){
printf("\nPlease type value for W[%d]: ", t);
while((ch = getchar()) != '\n'){
if(49 == ch){
ch = 1;
}else if(48 == ch){
ch = 0;
}
W[i][j] = ch;
j++;
}
t++;
i++;
j = 0;
}
//Decodes each letter
printf("\n\nThe Decoded Text is: ");
k = i = j = s = t = 0;
while(i < ROWS){
t++;
k = 0;
printf("\n\nInitial value for W[%d]: ", t);
while(k <= 2*COLS){
printf("%d", W[i][k]);
k++;
}
printf("\nThe new number is c[%d]: ", t);
j = 0;
while(j < 7){
printf("%d", W[i][j]);
++j;
}
/*multiplies each row of encoded (W), by each column of A*
I am removing this because it's a computer and doesn't need error checking...
I don't have time to make it purposly add errors.
while(s < 7){
while(j < 2*COLS){
if(j < 7){
c[i][s] += (W[i][j] * J[s][j]);
j++;
}else{
c[i][s] += (W[i][j] * I[s][j]);
j++;
}
}
c[i][s] = (c[i][s] % 2);
printf("%d", c[i][s]);
s++;
j = 0;
}
*/
i++;
}
i = 0;
ch = 0;
printf("\n\nYoure sentence was: ");
//Converts binary to letter!
while(i < ROWS){
if(W[i][0] == 1){
ch += 64;
}if(W[i][1] == 1){
ch += 32;
}if(W[i][2] == 1){
ch += 16;
}if(W[i][3] == 1){
ch += 8;
}if(W[i][4] == 1){
ch += 4;
}if(W[i][5] == 1){
ch += 2;
}if(W[i][6] == 1){
ch += 1;
}
ascii = ch;
printf("%c", ascii);
ch = 0;
i++;
}
//End of conversion!
return;
}
// This is the word collector
void wordCollector(){
char word[SIZE];
char ch;
int i = 0;
int str_len;
int k,num;
int j = 7;
int binary[SIZE][8];
k = getchar();
printf("\n***After you type your sentence, type enter**\n");
printf("Please type the word you wish to encode: ");
while((ch = getchar()) != '\n'){
j = 6;
word[i] = ch;
for(k = 0; k < 8; k++){
binary[i][k] = 0;
}
for(k = 0; k < 1; k++){
num = ch;//num = *(p+i);
printf("\n\nANSI value for '%c' is %d", ch, num);
while(num != 0){
binary[i][j--] = num % 2;
num /= 2;
}
}
printf("\nConverted to Binary: ");
for(k = 0; k < 7; k++){
printf("%d", binary[i][k]);
}
i++;
}
word[i] = '\0';
i = 0;
printf("\n\nYour sentence was: \n");
while (word[i] != '\0'){
putchar(word[i]);
printf(" [");
for(k = 0; k < 7; k++){
printf("%d", binary[i][k]);
}
printf("]\n");
i++;
}
wordEncoder(binary, i, 7);
return;
}
//This is the main function
int main(void){
int option = 1;
while(option != 51){
printf("\n\nAre you encoding(type 1), decoding(type 2) or do you wish to exit(type 3)?: ");
option = getchar();
if(option == 49){
wordCollector();
}else if(option == 50){
decoder();
}else if(option == 51){
}else{
printf("\nInvalid Choice, Choose Another!");
}
}
printf("\n\n");
system("pause");
return 0;
}



