Java error correcting, testing for a square issue i think
Hi all, i am having an issue with my code.
First of all this is a program that reads in 10 digits, and there are 4 possible cases.
No error occured
single error occured - correct the error and print out correct 10 digits
double error occured - correct the errors and print out correct 10 digits
More than two errors occured - print out a message saying more than 2 errors
The progra actually works well to a certain extent
Now i think the issue with this program is with the following condition: (quoted from task spec)
4. If (Q2-4PR) is not a non-zero square (i.e. √(Q2-4PR) hasn’t got a root in GF(11)) or if position value i or j is zero there are at least three errors have occurred.
The reason i think there is a problem here is because if i type in 5545644889 as the 10 digits for example. then i get the following output:
Please enter a 10 digits number to decode:
5545644889
QQ - 4PR = 1
(QQ - 4PR) is not a square. More than 2 errors
BUILD SUCCESSFUL (total time: 2 seconds)
Now 1 is obviously a square number (1x1) so im not sure if im calculating this condition correctly.
Any ideas?
I will try and find another 10 digits example because once 4 was the value QQ - 4PR, and it still had this form of output, when obviously 4 is a square.
Here is the program anyway.
Code :
public static void decode(String code) {
// If input is less than 10 digits then warn the user of an invalid input
if (code.length() != 10) {
System.out.println("Invalid input! Input must be 10 digits");
return;
}
// Make an Array out of the String code
int d[] = new int[]{0,
code.charAt(0) - '0',
code.charAt(1) - '0',
code.charAt(2) - '0',
code.charAt(3) - '0',
code.charAt(4) - '0',
code.charAt(5) - '0',
code.charAt(6) - '0',
code.charAt(7) - '0',
code.charAt(8) - '0',
code.charAt(9) - '0'
};
// Generating the Syndrome Vector
int s1 = (d[1] + d[2] + d[3] + d[4] + d[5] + d[6] + d[7] + d[8] + d[9] + d[10]) % 11;
int s2 = (d[1] + 2 * d[2] + 3 * d[3] + 4 * d[4] + 5 * d[5] + 6 * d[6] + 7 * d[7] + 8 * d[8] + 9 * d[9] + 10 * d[10]) % 11;
int s3 = (d[1] + 4 * d[2] + 9 * d[3] + 5 * d[4] + 3 * d[5] + 3 * d[6] + 5 * d[7] + 9 * d[8] + 4 * d[9] + d[10]) % 11;
int s4 = (d[1] + 8 * d[2] + 5 * d[3] + 9 * d[4] + 4 * d[5] + 7 * d[6] + 2 * d[7] + 6 * d[8] + 3 * d[9] + 10 * d[10]) % 11;
// Test to see if s1, s2, s3, s4 = 0, 0, 0, 0
if (s1 == s2 && s2 == s3 && s3 == s4 && s4 == 0) {
System.out.println("No error!");
return;
}
//Calculate P & if P is negative, add 11 to obtain positive modulus result
int P = (s2 * s2 - s1 * s3) % 11;
if (P < 0) {
P += 11;
}
//Calculate Q & if Q is negative, add 11 to obtain positive modulus result
int Q = (s1 * s4 - s2 * s3) % 11;
if (Q < 0) {
Q += 11;
}
//Calculate R & if R is negative, add 11 to obtain positive modulus result
int R = (s3 * s3 - s2 * s4) % 11;
if (R < 0) {
R += 11;
}
// If P, Q & R are all equal to 0 then there is a single error
if (P == Q && Q == R && R == 0) {
d[s2 / s1] -= s1;
if (d[s2 / s1] < 0) {
d[s2 / s1] += 11;
}
System.out.println("Single error!");
System.out.println("At position " + s2 / s1 + " by magnitude " + s1);
String correct = "";
for (int i = 1; i <= 10; i++) {
correct += "" + d[i];
}
// Print out the correct codeword
System.out.println("The correct code: " + correct);
return;
}
// Calculate (QQ - 4PR)%11
int delta = (Q * Q - 4 * P * R) % 11;
if (delta < 0) {
delta += 11;
}
if (delta > 0) {
int de = 2 * P % 11;
int mapping[] = new int[]{0, 1, 6, 4, 3, 9, 2, 8, 7, 5, 10};
int deinv = mapping[de];
// Calculate i (position of error)
int i = ((int) (-Q + Math.sqrt(delta)) * deinv) % 11;
if (i < 0) {
i += 11;
}
//Calculate j (position of error)
int j = ((int) (-Q - Math.sqrt(delta)) * deinv) % 11;
if (j < 0) {
j += 11;
}
// Test to see if (QQ - 4PR)%11 is a non-zero square
int x = (Q * Q - 4 * P * R) % 11;
if (x < 0) {
x += 11;
}
if (x != 1 || x != 4 || x != 9) {
//(QQ - 4PR)%11 is not a square
System.out.println("(QQ - 4PR)%11 = " + x);
System.out.println("(QQ - 4PR)%11 is not a square. More than 2 errors");
return;
}
// Test to see if i or j are equal to 0
if (i == 0 || j == 0) {
System.out.println("i or j is equal to 0. More than two errors!");
return;
}
// Calculate b (Magnitude of error)
int b = ((i * s1 - s2) / (i - j)) % 11;
if (b < 0) {
b += 11;
// Calculate a (Magnitude of error)
}
int a = s1 - b;
System.out.println("a " + a + " b " + b);
d[j] -= b;
if (d[j] < 0) {
d[j] += 11;
}
d[i] -= a;
if (d[i] < 0) {
d[i] += 11;
}
System.out.println("Two errors!");
String correct = "";
for (int k = 1; k <= 10; k++) {
correct += "" + d[k];
}
// Print out the corrected codeword
System.out.println("The correct code: " + correct);
return;
}
System.out.println("More than two errors!");
}
// Main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a 10 digits number to decode: ");
String number = scanner.nextLine();
decode(number);
}
}
Re: Java error correcting, testing for a square issue i think
Updated:
Code has been slightly modified the condition im trying to test for is actually:
if (QQ - 4PR)%11 is not a valid square i.e. if it is not equal to 1, 4 or 9 then print out a message.
obviously with %11 it cant be higher than 11 so the only squares it could be is 1, 4 and 9.
My if statement doesnt seem to like the conditions ive put in it thought.
even when x is one, it still prints out for some reason.
I am using this:
if (x != 1 || x != 4 || x != 9) {
//(QQ - 4PR)%11 is not a square
System.out.println("(QQ - 4PR)%11 = " + x);
System.out.println("(QQ - 4PR)%11 is not a square. More than 2 errors");
return;
}
That basically means if x is not equal to 1, 4 or 9 then print out the messages.
Well even when x is 1 it still prints out that message for some reason?!?
Why?
Re: Java error correcting, testing for a square issue i think
In an OR statement only one condition has to be true.
When x == 1, x != 4 is true
When x == 4, x != 1 is true
When x == 9, x != 1 is true
Your if statement will always be true no matter what value x has.
Re: Java error correcting, testing for a square issue i think
Do i need && instead of || then?
I dont think that would work either would it?
Re: Java error correcting, testing for a square issue i think
What happened when you tried it? You did try it instead of wasting time asking us, didn't you?
Re: Java error correcting, testing for a square issue i think
What's the difference between || and &&?
|| (Or Operator, binary) Checks for all the conditions. If all are False, the result will be false and if any of the one is True, the result will be True.
&& (And Operator, binary) Checks for the very first condition, if it's True, it will go on the next and so on, but if the very first condition is False, all others will be skipped and ultimately the result will be False.
So, in || case, all statements must be False, in order to get the final result as False.
And in && case, all statements must be True, in order to get the final result as True.
As Junky has given you the best possible hint in your case.
Hope this will help too, for your understanding.
Good Luck.