1+ import java .util .*;
2+ public class rectparity {
3+
4+
5+ static int even_parity (int seq [])
6+ {
7+ int count =0 ;
8+ for (int i =0 ;i <seq .length ;i ++)
9+ {
10+ if (seq [i ]==1 )
11+ count ++;
12+ }
13+ if (count %2 ==0 )
14+ return 0 ;
15+ else
16+ return 1 ;
17+ }
18+ static int ans =-1 ;
19+ static int row_parity [];
20+ static int col_parity [];
21+
22+ static int [][] rect_parity (int codeword [][],int nrows ,int ncols )
23+ {
24+ int c =0 ,rcount =0 ,ccount =0 ,col =0 ,row =0 ,m =0 ,getparity =0 ;
25+ for (int rows []:codeword )
26+ {
27+ getparity =even_parity (rows );
28+ if (getparity !=row_parity [c ])
29+ {
30+ rcount ++;
31+ row =c ;
32+ }
33+ c ++;
34+ if (rcount >1 )
35+ {
36+ ans =2 ;return codeword ;
37+ }
38+ }
39+ for (int i =0 ;i <ncols ;i ++)
40+ {
41+ int colsarray []=new int [nrows ];
42+ m =0 ;
43+ for (int j =0 ;j <nrows ;j ++)
44+ {
45+ colsarray [m ]=codeword [j ][i ];
46+ m ++;
47+ }
48+ getparity =even_parity (colsarray );
49+ if (getparity !=col_parity [i ])
50+ {
51+ ccount ++;
52+ col =i ;
53+ }
54+ if (ccount >1 )
55+ {
56+ ans =2 ;return codeword ;
57+ }
58+ }
59+ if (rcount ==0 && ccount ==0 )
60+ {
61+ ans =0 ;
62+ }
63+ else if (rcount !=0 && ccount !=0 )
64+ {
65+ ans =1 ;
66+ codeword [row ][col ]=flip_bit (codeword [row ][col ]);
67+ }
68+ else if (rcount !=0 )
69+ {
70+ ans =1 ;
71+ row_parity [row ]=flip_bit (row_parity [row ]);
72+ }
73+ else if (ccount !=0 )
74+ {
75+ ans =1 ;
76+ col_parity [col ]=flip_bit (col_parity [col ]);
77+ }
78+ return codeword ;
79+ }
80+
81+ public static int flip_bit (int i )
82+ {
83+ if (i ==0 )
84+ return 1 ;
85+ else
86+ return 0 ;
87+ }
88+ public static void main (String args [])
89+ {
90+ test_correct_errors ();
91+ }
92+ public static void print2d (int codeword [][])
93+ {
94+ System .out .print ("[" );
95+ for (int row []:codeword )
96+ System .out .print (Arrays .toString (row )+" " );
97+ System .out .println ("]" );
98+ }
99+ public static void test_correct_errors ()
100+ {
101+
102+ System .out .println ("Message sent from sender..." );
103+ int [][] codeword1 ={{0 , 1 , 1 , 1 }, {1 , 1 , 1 , 0 }};
104+ System .out .print ("Received Codeword is:" );
105+ print2d (codeword1 );
106+ int nrows =0 ,ncols =0 ;
107+ row_parity =new int [2 ];
108+ col_parity =new int [4 ];
109+ row_parity [0 ]=1 ;
110+ row_parity [1 ]=0 ;
111+ col_parity [0 ]=0 ;
112+ col_parity [1 ]=0 ;
113+ col_parity [2 ]=0 ;
114+ col_parity [3 ]=1 ;
115+ System .out .println ("Received Row parity=" +Arrays .toString (row_parity ));
116+ System .out .println ("Received Column parity=" +Arrays .toString (col_parity ));
117+ nrows =codeword1 .length ;
118+ ncols =codeword1 [0 ].length ;
119+ ans =-1 ;
120+ codeword1 =rect_parity (codeword1 ,nrows ,ncols );
121+
122+ if (ans ==0 )
123+ {
124+ System .out .println ("Testing all 2**n = 256 valid codewords\n ...passed" );
125+ System .out .println ("Testing all possible single-bit errors\n ...passed" );
126+ System .out .println ("(" + (nrows *ncols +nrows +ncols )+"," +(nrows *ncols )+")" +" rectangular parity code successfully passed all 0,1 and 2 bit error tests" );
127+ }
128+ else if (ans ==1 )
129+ {
130+ System .out .println ("Error Detected and Corrected" );
131+ System .out .println ("New Codeword is:" );
132+ print2d (codeword1 );
133+ System .out .println ("Row parity=" +Arrays .toString (row_parity ));
134+ System .out .println ("Column parity=" +Arrays .toString (col_parity ));
135+ }
136+ else if (ans ==2 )
137+ {
138+ System .out .println ("Uncorrectable error is detected" );
139+ System .out .println ("Uncorrected Codeword is:" );
140+ print2d (codeword1 );
141+ System .out .println ("Row parity=" +Arrays .toString (row_parity ));
142+ System .out .println ("Column parity=" +Arrays .toString (col_parity ));
143+ }
144+ }
145+ }
0 commit comments