Hi!
It's me again and this is a decoder!
The goal:
And I'm trying to make a program that solves logical problems related to the decoder device. For that I need a loop that write true/false into a boolean[][] based on the rules.
Say we have a decoder with 3 inputs. That means that it has 8 outputs (2^(numberOfInputs) = 8).
I need a method to write this (example of 3 inputs):
|0|0|0|
|0|0|1|
|0|1|0|
|0|1|1|
|1|0|0|
|1|0|1|
|1|1|0|
|1|1|1|
Notice the third column (actually called column 0, the middle one would be 1, the most left one 2).
2^0 (0 being the column "name") is 1, so the rule is: from top to bottomw write one 0, then one 1, again one 0 and one 1).
In column 1, 2^1 = 2, so write two 0s, then two 1s and repeat.
In column 2, 2^2 = 4, so write four 0s, then four 1s.
Column 3 would be 2^3 = 8, so eight 0s, then eight 1s.
The code:
package Decoder;
import java.util.Scanner;
public class Decoder {
public static void main(String[] args) {
Decoder M = new Decoder();
int inputs, outputs;
Scanner S = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
inputs = Integer.parseInt(S.nextLine());
outputs = (int) Math.pow(2, inputs); // derived from rule
boolean[][] table = new boolean[outputs][inputs];
System.out.println("Decoder: " + inputs + "/" + outputs);
table = M.defaultTable(outputs, inputs);
M.printTable(table, outputs, inputs);
}
public boolean[][] defaultTable(int inputs, int outputs) {
boolean[][] Y = new boolean[inputs][outputs];
int counter = 0;
int someNumber = 0;
for (int i = outputs - 1; i > -1; i--) {
int step = (int) Math.pow(2, counter);
for (int j = 0; j < inputs; j++) {
if (step == someNumber) { // THIS
Y[j][i] = true; // IS
someNumber = 0; // THE PROBLEM AREA
}
someNumber++;
}
counter++;
}
return Y;
}
public void printTable(boolean[][] Y, int izlazi, int ulazi) { // Standard method for printing a 2D array.
for (int i = 0; i < izlazi; i++) {
for (int j = 0; j < ulazi; j++) {
if (Y[i][j] == true){
System.out.print("|1");
}else {
System.out.print("|0");
}
}
System.out.println("|");
}
}
}
The problem:
I need method "defaultTable" (lines 23-40) to respect the complement of two (1, 2, 4, 8, 16 etc).
I already got it to move in a correct sequence (start at top left cell, work downwards), but I can't figure out how to asign values into the boolean[][] while moving in the "complement of two"-way.
Just a hint! No direct solutions please! :D
Thanks,
Pob