Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. Question. Lots of Maths!

Hi.
First of all i am in the process of building an error correcting code program for my Cryptography and Coding Systems Module.

The program is not 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*7-2*3) mod 11 = 43 mod 11 = 10
Q = (2*3-7*3) mod 11 = -15 mod 11 = -4 mod 11 = 7
R = (3*3-7*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, before they get to 7 and 10.

Why is this?

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.

```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");
}

}
}```

2. Re: Question. Lots of Maths!

Do you have understanding of Operators precedence?

As a hint,
* has higher precedence than +. So, it will be calculated before +.

3. Re: Question. Lots of Maths!

There are no + operations in the calculations of P, Q & R

4. Re: Question. Lots of Maths!

for the record, s1, s2, s3, & s4 are bieng calculated correctly. I realise the multiplication is done before addition.

5. Re: Question. Lots of Maths!

Try placing System.out.println after every statement and see where exactly the problem is or if you are good to debug this, try debugging it.

6. Re: Question. Lots of Maths!

What do you mean by debugging it mate

Thanks

7. Re: Question. Lots of Maths!

Originally Posted by djl1990
What do you mean by debugging it mate

8. Re: Question. Lots of Maths!

I have tried that, i have printed out the values of s1, s2, s3, s4 and they are all bieng calculated correctly, therefore ive nailed it down to the problem bieng in the calculation of P, Q, & R

9. Re: Question. Lots of Maths!

This thread has been cross posted here: