/
prgassign3_danielyan.c
125 lines (110 loc) · 2.71 KB
/
prgassign3_danielyan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Georgiy Danielyan
* Comp 222 - Computer Organization
* Program for error correction
* Hamming Code Error Detection and Correction
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/**************************
Gloable Variables
**************************/
char *hamCode = NULL;
int maxLengthHamCode;
int parity;
char ESC = 27; //Used for bold printing of messages
/**************************
Function Prototypes
**************************/
void displayMenu();
void enterParameters();
void checkHammingCode();
/**************************
Main Method
**************************/
int main(){
int choice;
do{
choice = 0;
displayMenu();
scanf("%d", &choice);
switch(choice){
case 1:
enterParameters();
break;
case 2:
checkHammingCode();
break;
case 3:
free(hamCode);
exit(0);
break;
default:
printf("Invalid selection\n\n");
//Clear the input stream in case of extraneous inputs.
while ((choice = getchar()) != '\n' && choice != EOF);
break;
}
}while(choice != 4);
return 0;
}
/**************************
Function Definitions
**************************/
void displayMenu(){
printf("\nGeorgiy Danielyan\n");
printf("Error detection/correction:\n");
printf("---------------------------\n");
printf("1) Enter Parameters\n");
printf("2) Check Hamming Code\n");
printf("3) Quit Program\n\n");
printf("Enter selection: ");
}
void enterParameters(){
printf("\nEnter the maximum hamLength: ");
scanf("%d", &maxLengthHamCode);
printf("\nEnter the parity (0 = even, 1 = odd): ");
scanf("%d", &parity);
hamCode = (char *)malloc(sizeof(char)*maxLengthHamCode);
}
void checkHammingCode(){
// /*- turn on bold */
printf("%c[1m",ESC);
/* Declare local variables */
int pBit;
int error = 0;
int hamLength;
printf("\nEnter the Hamming code: ");
scanf( "%s", hamCode );
hamLength = strlen(hamCode);
if( hamLength > maxLengthHamCode ){
printf("\n*** Invalid Entry - Exceeds Maximum Code Length of %d\n\n", maxLengthHamCode);
}
else{
/* Checking Hamming code for errors */
int a, b, c, count = 0;
/* Check for each parity bit */
for( a = 1; a < hamLength; a = a*2){
pBit = parity;
for( b = a; b <= hamLength ; b = c + a ){
for( c = b; count != a && c <= hamLength ; c++ ){
pBit = pBit^(hamCode[hamLength-c]-'0');
count++;
} // c loop
count = 0;
} // b loop
error += (pBit * a);
} // a loop
if( error == 0 )
printf("\n***There is no bit error.\n");
else{
printf("\n***There was an error in bit: %d", error);
hamCode[hamLength-error] = ( hamCode[hamLength-error] == '0' ? '1' : '0' );
printf("\n***The corrected Hamming code is: %s\n", hamCode);
}
}
/* turn off bold */
printf("%c[0m",ESC);
}