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

Edited 3 Years Ago by Pobunjenik: typo

You can go row-by-row down the rows, starting with all false, then for each row after that you go from right-to-left along the row filling the entries based on the previous row's entries. Until you find a false in the previous row, make each entry false, then where there is a false in the previous row make the entry true, and every entry after that is a copy of the previous row's entry.

I don't quite understandy our logic, so I've decided to follow my teacher's advice: anything can be done in Java.

So, I'm actually getting real close to solving this problem my way. :D
I'll post the solution if people want me to. :)

This question has already been answered. Start a new discussion instead.