Ok, so this is an addendum to my question here, which was answered.
Not wanting to cheapen the answer for the previous question, I decided this should have its own question in fairness to JB Nizet who answered my first question, and received credit for such.
I am implementing a 2-dimensional data structure for a class.The approach used is an "NxN" Array of objects.
So,
Cell[][] dataStructure = new Cell[N][N];
Each cell relies on the output of the cell to the left of it, and to the cell above it, to create its own output. The 2-d structure will be tested for a range of inputs, 000 to 111.
Example of 2-D structure and how the outputs flow into the next cells

Example:
Assuming standard X,Y directions, I attempt to get the output of the bottom right cell using the following method:
/**
* Recursive method that returns the output of a given cell
* @param row: the row the cell is in (its yPos)
* @param inputs:
* @param column: the column the cell is in (its xPos)
*/
private int[] getOutput(int[] inputs,int yPos, int xPos){
if (yPos==-1){
int[] out = new int[2];
out[0] = 0; // yPos
out[1] = inputs[xPos]; //xPos
return out;
}
else if (xPos==-1){
int[] out = new int[2];
out[0] = inputs[yPos]; //yPos
out[1] = 0; //xPos
return out;
}
int[] leftOutput = getOutput(inputs, yPos, xPos-1);
int[] topOutput = getOutput(inputs, yPos-1, xPos);
return currentStructure[yPos][xPos].getResult(leftOutput[1], topOutput[0]);
}
To simplify things, I now have a single getResult method that for a cell in the 2-d structure, performs logic on the specified inputs. The result is an int[] of two outputs, one for each direction.
The getResult method, as it currently stands is written as such:
public int[] getResult(int left, int top)
{
int[] resultOut = new int[2];
if (xDirStr.equals("00")){ // AND
resultOut[0]= left * top;
}
if (xDirStr.equals("01")){ // OR
if (left ==1 || top ==1)
resultOut[0]= 1;
else
resultOut[0] =0;;
}
if (xDirStr.equals("10")){ // NOT, USES ONLY NOT X
if (left ==0)
resultOut[0]= 1;
else
resultOut[0]= 0;
}
if (xDirStr.equals("11")){ // XOR
if ( (left==1 && top==0) || (left==0 && top==1))
resultOut[0]= 1;
else
resultOut[0]= 0;
}
if (yDirStr.equals("00")){ // AND
resultOut[1]= left * top;
}
if (yDirStr.equals("01")){ // OR
if (left ==1 || top ==1)
resultOut[1]= 1;
else
resultOut[1]= 0;
}
if (yDirStr.equals("10")) { // NOT, USES ONLY NOT X
if (left ==0)
resultOut[1]= 1;
else
resultOut[1]= 0;
}
if (yDirStr.equals("11")) { // XOR
if ( (left==1 && top==0) || (left==0 && top==1))
resultOut[1]= 1;
else
resultOut[1]= 0;
}
return resultOut;
}
I've debugged step-by-step but can't catch my issue. The output does not match what I recreate by hand to verify it, the outputs are always 0. Any helpful hints would be appreciated!
My specific question, is why does getOutput always return 0? From what I see debugging, the issue is not in my logic application, which I've not included here.
Thanks again.
=======UPDATE======== A sample 3x3, at the request of BevynQ.
The inputs, along the left and top edges change during each test, this example is for the base case 000. **The NOT function ALWAYS returns the logical NOT of the value entering from the left.* The "Output" I'm attempting to check in my methods is circled in red.

Eureka, it was so simple that I overlooked it and didn't catch it. The problem is mostly related to my poor choice of variable names. I tinkered with maybe having the wrong array outputs, but the solution was to change the return line to:
return currentStructure[xPos][yPos].getResult(leftOutput[1], topOutput[0]);
See the difference? I swapped xPos and yPos. I understood for a brief shining moment while debugging, but the explanation of it is lost in my brain again. It's related to accessing the structure, which requires the yPosition first, then the xPosition... which is the opposite of the standard (x.y) notation we all know and love.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With