luke luke - 1 month ago 18
Java Question

XOR Matrix computation

I am writing one program which take two values m number of columns and n number of rows. I am supposed to perform the xor operation on rows.

Like let's say I have 4 rows and 2 columns. The row input I got from the user is counted in 2 columns means I have to perform only one operation.

For e.g.,

m = 4 and n = 2

6 7 1 3 (1st column) After performing xor operation on rows element like

6 xor 7 = 1

7 xor 1 = 6

1 xor 3 = 2

3 xor 6 = 5

So 1 6 2 5 are the second column and should be printed as the final answer.
But till the 2nd column my program is printing correct output but If I enter my input like m = 4 and n = 3 the output should be 7 4 7 4 but my program prints 7 4 7 2. I am unable to rectify the problem.

If anybody could solve this that will be a great help.

Thank you

Here is my code


import java.util.Scanner;
class XorMatrixMain{
public static void Xor_Array(int[] xor, int n){
int num = xor[0];
boolean bool = false;
for(int j=0;j<n-1;j++){
for(int i=0 ; i<xor.length ; i++){
int last = xor[0];
if(i<xor.length-1){
xor[i] = xor[i]^xor[i+1];
}
if(i==xor.length-1){
if(bool){
xor[i] = xor[i]^last;
}
else{
xor[i] = xor[i]^num;
bool = true;
}
}
}
}
for(int i=0;i<xor.length;i++){
System.out.print(xor[i]+" ");
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
int[] xor = new int[m];
for(int i=0;i<m;i++){
xor[i] = scan.nextInt();
}
Xor_Array(xor,n);
}
}

Answer

@luke
1). The for loop which you are using iterates from last value to first value. And that can make a difference when you perform your xor operation. And affect your output in a different way
2). ArrayOutOfBoundException because you are trying to access an array with illegal index.
Here are some changes which I have made to you code

import java.util.Scanner;
import java.math.*;
class BigForLoop{
    public static void Xor_Array(BigInteger[] x, BigInteger m , BigInteger n){
        BigInteger num = x[0];
        boolean bool = false;
        BigInteger last = new BigInteger("0");
        for(BigInteger j = BigInteger.valueOf(0); j.compareTo(n.subtract(BigInteger.valueOf(1))) < 0; j = j.add(BigInteger.ONE)){
            for(BigInteger i = BigInteger.valueOf(0); i.compareTo(m.subtract(BigInteger.valueOf(1))) <= 0; i = i.add(BigInteger.ONE)){
                if(!(i.equals(BigInteger.valueOf(m.intValue()-1)))){
                    x[i.intValue()] = x[i.intValue()].xor(x[(i.intValue())+1]);
                }
                if(i.equals(BigInteger.valueOf(m.intValue()-1))){
                    if(bool){
                        x[i.intValue()] = x[i.intValue()].xor(last);
                    }
                    else{
                        x[i.intValue()] = x[i.intValue()].xor(num);
                        bool = true;
                    }
                }
            }
            last = x[0];
        }
        for(BigInteger i = BigInteger.valueOf(0); i.compareTo(m.subtract(BigInteger.valueOf(1))) <= 0; i = i.add(BigInteger.ONE))
            System.out.print(x[i.intValue()]+" ");
    }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        BigInteger m = scan.nextBigInteger();
        BigInteger n = scan.nextBigInteger();
        BigInteger[] xor = new BigInteger[m.intValue()];
        for(BigInteger i = BigInteger.valueOf(0); i.compareTo(m.subtract(BigInteger.valueOf(1))) <= 0; i = i.add(BigInteger.ONE))
            xor[i.intValue()] = scan.nextBigInteger();
        Xor_Array(xor,m,n);
    }
}