-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparity.cpp
More file actions
187 lines (172 loc) · 3.96 KB
/
parity.cpp
File metadata and controls
187 lines (172 loc) · 3.96 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//============================================================================
// Name : parity.cpp
// Author : SidPro
// Version : 1.0
// Description : Two-dimensional Parity check
/*
=> Parity check bits are calculated for each row, which is equivalent to a
simple parity check bit. Parity check bits are also calculated for all columns,
then both are sent along with the data. At the receiving end these are compared
with the parity bits calculated on the received data.
=> https://www.geeksforgeeks.org/error-detection-in-computer-networks/
*/
//============================================================================
/* input - output format
*
* Enter input stream: 0001110110111100110110100
Enter number of columns: 5
Row parity: 0 1 0 1 0
column parity: 1 0 0 1 0
0001110110111100110110100100100
No Error
*/
#include <iostream>
#include<tuple>
using namespace std;
// 0001110110111100110110100
tuple<string,int> sender();
void receiver(string,int);
int main(){
string s;
int col=0;
tie(s,col) = sender();
if(s!=""&&col!=0)
receiver(s,col);
return 0;
}
tuple<string,int> sender(){
int col,i,j,track=-1,len,count;
bool check = false;
char parity,ch1;
string s;
cout<<"Enter input stream: ";
cin>>s;
len = s.length();
cout<<"Enter number of columns: ";
cin>>col;
char column[col];
char row[len/col];
if(len%col == 0)check = true;
if(check){
//-------------- Row parity --------->
for(i=0;i<len;++i){
if(i%col==0 && i>0){
if(count%2==0)
row[track] = '0';
else
row[track] = '1';
}
if(i%col==0){
++track;
count=0;
}
if(s[i]=='1')count++;
}
if(count%2)row[track] = '1';
else row[track] = '0';
//---------------------column Parity------------------->
track = col;
for(i=0;i<track;++i){
count=0;
if(s[i]=='1')++count;
for(j=i+track;j<len;j=j+track){
if(s[j]=='1')++count;
}
if(count%2==0)column[i]='0';
else column[i]='1';
}
//------------------Parity Bit-------------------->
count=0;
track=0;
for(i=0;i<col;++i){
if(column[i]=='1')++track;
}
for(i=0;i<len/col;++i){
if(row[i]=='1')++count;
}
if(count%2==0)ch1='0';
else ch1='1';
if(track%2==0)parity='0';
else parity='1';
if(ch1==parity)parity=ch1;
else cout<<"message is not clear"<<endl;
//----------------------print everything-------------->
cout<<"Row parity: ";
for(i=0;i<len/col;++i){
cout<<" "<<row[i];
}
cout<<endl;
cout<<"column parity: ";
for(i=0;i<col;++i){
cout<<" "<<column[i];
s = s+column[i];
}
s = s + parity;
cout<<"\n"<<s<<endl;
}
//-------------------------------------------
else
cout<<"Please check your input steam and column inputs Best Regards!!!";
return make_tuple(s,col);
}
void receiver(string s,int col){
string parity = s.substr(s.length()-(col + 1));
string s2 = s.substr(0,(s.length()-(col + 1)));
// cout<<parity<<endl;
// cout<<s2<<endl;
int len = s2.length();
int i,j,track=-1,count;
char column[col],row[len/col],p1,p2;
bool pass1 = true;
track = col;
for(i=0;i<track;++i){
count=0;
if(s[i]=='1')++count;
for(j=i+track;j<len;j=j+track){
if(s[j]=='1')++count;
}
if(count%2==0)column[i]='0';
else column[i]='1';
}
for(i=0;i<col;++i){
if(parity[i]!=column[i])pass1=false;
}
cout<<"\n";
if(pass1){
track=-1;
for(i=0;i<len;++i){
if(i%col==0 && i>0){
if(count%2==0)
row[track] = '0';
else
row[track] = '1';
}
if(i%col==0){
++track;
count=0;
}
if(s[i]=='1')count++;
}
if(count%2)row[track] = '1';
else row[track] = '0';
count=0;
track=0;
for(i=0;i<col;++i){
if(column[i]=='1')++track;
}
for(i=0;i<len/col;++i){
if(row[i]=='1')++count;
}
if(count%2==0)p1='0';
else p1='1';
if(track%2==0)p2='0';
else p2='1';
if(p1==parity[parity.length() - 1] && p2==parity[parity.length() - 1])
cout<<"No Error";
else
cout<<"Error";
}
else{
cout<<"message is courrepeted";
}
}