Results 1 to 7 of 7
Thread: Decoding
 10282011, 04:45 PM #1Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Decoding
First of all i am in the process of building an error correcting code program
The program is far from finished but it runs to a certain extent.
The problem i am having is i think to do with the modulus or % operator.
I quote below from my specification that if the value 8899880747 is entered as the 10 digit codeword. Then variables P, Q and R should equal 10, 7 & 10 respectively.
i.e. (from spec.) :
"P = (7*72*3) mod 11 = 43 mod 11 = 10
Q = (2*37*3) mod 11 = 15 mod 11 = 4 mod 11 = 7
R = (3*37*3) mod 11 =  12 mod 11 = 1 mod 11 = 10"
now if you run my program, i have just ran a small test to see if my variables are bieng calculated properly. By just printing out the values of P, Q and R.
I am actually getting 10, 4, and 1 respectively for P, Q and R . This is kind of strange as if you look above at the quoted spec Q and R are actually 4 and 1, then the modulus is carried out to get 7 & 10.
Why are calculations giving 4 and 1 instead of 7 and 10?
Here is my program so far. It is far from finished but i cant really move on until all my variables are bieng calculated properly.
Java Code:public static void main(String[] args) { int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10; // the 10 digits from user input int s1, s2, s3, s4; // if all = 0 then there are no errors int s1Squared, s2Squared, s3Squared, s4Squared; int P, Q, R; // if all = 0, then there is a single error int qSquared; // the Product of Q * Q int a, b; // the magnitude of the errors int i, j; // the positions of the errors String input; // the 10 digits input by the user Scanner scan = new Scanner(System.in); System.out.println("Enter a potential 10 digit codeword: "); input = scan.nextLine(); d1 = Integer.parseInt(String.valueOf(input.charAt(0))); d2 = Integer.parseInt(String.valueOf(input.charAt(1))); d3 = Integer.parseInt(String.valueOf(input.charAt(2))); d4 = Integer.parseInt(String.valueOf(input.charAt(3))); d5 = Integer.parseInt(String.valueOf(input.charAt(4))); d6 = Integer.parseInt(String.valueOf(input.charAt(5))); d7 = Integer.parseInt(String.valueOf(input.charAt(6))); d8 = Integer.parseInt(String.valueOf(input.charAt(7))); d9 = Integer.parseInt(String.valueOf(input.charAt(8))); d10 = Integer.parseInt(String.valueOf(input.charAt(9))); s1 = (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10) % 11; s2 = (d1 + 2 * d2 + 3 * d3 + 4 * d4 + 5 * d5 + 6 * d6 + 7 * d7 + 8 * d8 + 9 * d9 + 10 * d10) % 11; s3 = (d1 + 4 * d2 + 9 * d3 + 5 * d4 + 3 * d5 + 3 * d6 + 5 * d7 + 9 * d8 + 4 * d9 + d10) % 11; s4 = (d1 + 8 * d2 + 5 * d3 + 9 * d4 + 4 * d5 + 7 * d6 + 2 * d7 + 6 * d8 + 3 * d9 + 10 * d10) % 11; s1Squared = (int) Math.pow(s1, 2); s2Squared = (int) Math.pow(s2, 2); s3Squared = (int) Math.pow(s3, 2); s4Squared = (int) Math.pow(s4, 2); P = (s2Squared  (s1 * s3)) % 11; Q = ((s1 * s4)  (s2 * s3)) % 11; R = ((s3Squared)  (s2 * s4)) % 11; qSquared = (int) Math.pow(Q, 2); i = (int) ((Math.sqrt(qSquared  4 * P * R)  Q) / (2 * P)) % 11; j = (int) (Q  (Math.sqrt(qSquared  4 * P * R)) / (2 * P)) % 11; b = ((i * s1  s2) / (i  j)) % 11; a = s1  b; System.out.println(P); System.out.println(Q); System.out.println(R); if (s1 == 0 && s2 == 0 && s3 == 0 && s4 == 0) { System.out.println("There are no errors!"); } if (P == 0 && Q == 0 && R == 0 && s1 != 0 && s2 != 0 && s3 != 0 && s4 != 0) { System.out.println("There is 1 error"); } } }
 10282011, 05:47 PM #2Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 7
Re: Decoding
Did you not appreciate the help you were given over here?
 10282011, 05:52 PM #3Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Decoding
Im grateful of any help.
 10282011, 05:55 PM #4Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: Decoding
The way the Java modulus operator works is that it enforces the property that, for any ints a and b, the following will be true:
b*(a/b)+a%b==a
In mathematics, b*(a/b) would just be equal to a, so the only way that b*(a/b)+a%b would be equal to a is if a%b were equal to zero. In Java (and almost all modern programming languages), however, rounding occurs when you divide two integer values, meaning that b*(a/b) is not always equal to a. You could think of the modulus operator as a way of asking Java how much rounding would occur when dividing two numbers.
For example, suppose a and b are 10 and 3, respectively. In this case, a/b would be 3 (since everything after the decimal point gets truncated), so b*(a/b) would be 9, rather than 10. Therefore, 10%3 is equal to 1, because you need to add 1 to b*(a/b) (9, in this case) to get a (10, in this case).
Now, let's consider what happens when a is negative. Let's use 10 and 3 for a and b, this time. 10/3 is 3, and 3*3 is 9. The number we have to add to 9 to get 10 is 1, so 10%3 is equal to 1.
Based on what you said, it sounds like you want to calculate a modulus where the result is always positive, regardless of the signs of the two operands. Mathematically speaking, what this would involve is using a different method of rounding. What Java does is it rounds toward zero. For example, a positive number like 3.47 is higher than zero, so it gets rounded down toward zero, becoming 3. A negative number like 2.65 is lower than zero, so it gets rounded up toward zero, becoming 2. What you want is for it to always round down, regardless of the sign, so 2.65 would be rounded down to 3.
Now, in terms of actual coding, what I just said isn't very helpful, because (afaik) Java doesn't have an integer divide function which works like you want it to. You could cast the two ints to doubles, divide them, and use Math.floor(), but that would be really clunky/inelegant. Instead, you can simply take a%b, and, if the result is negative, add b.
 10282011, 06:06 PM #5Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Decoding
Thanks DiamondSoul.
Ive got it sorted.
The figures i were getting are 4 and 1 , in this case if 11 are added to these i get the figures i want in 7 and 10. as I am using % 11.
 10282011, 06:06 PM #6Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Decoding
Would you say this way okay for testing if it is negative and if so then add 11 to it:
Java Code:P = (s2Squared  (s1 * s3)) % 11; if (P < 0){ P = P + 11;
Last edited by ljd0991; 10282011 at 06:18 PM.
 10282011, 06:43 PM #7Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Similar Threads

What classes can I use to perform BER decoding of ldap communications?
By guest_user in forum New To JavaReplies: 2Last Post: 07172011, 02:00 PM 
Huffman encoding/decoding
By warlock in forum New To JavaReplies: 4Last Post: 04052011, 06:59 AM 
Gif decoding/LZW troubles
By hellochar in forum Advanced JavaReplies: 2Last Post: 07142009, 11:26 PM 
Translation/Decoding Program
By wyldstyle in forum New To JavaReplies: 14Last Post: 05052009, 09:17 PM
Bookmarks