Multidimensional array

I have the following code and I want to achieve the final picture. Can anyone help me to complete it?
(this is not homework or anything like that, I am learning at home through examples.)


float width_columns, height_rows;
PFont f;
int[][] numbers = {{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

void setup() {
  size(800, 600);

float num[][]= new float[width_columns][height_columns];

for (int i=0; i<numbers.length; i++){
  for (int j=0; j<numbers[i].length; j++){
    if(numbers[i][j] !=0){
      num=  + 1;
    }
  }
}

void draw() {
  background(100, 0, 100);
  f = createFont("Arial", 28, true);
  textFont(f, 18);

  height_rows = height/numbers.length;
  width_columns = width/numbers[0].length;

  for (int i = 0; i < numbers.length; i++) { 
    for (int j = 0; j < numbers[i].length; j++) {
      text(numbers[i][j], j*width_columns + width_columns/4, i*height_rows + height_rows/2);
    }
  }
}

Hi
This link closer idea

Replace this with

numbers[i][j] = i+j;

1 Like

Hello @mvb,

There are many errors in that example…

  • it is missing a closing bracket in setup()
  • height_columns is not declared. Looks like it should be another name.
  • width_columns and height_rows are floats; you will see error in Processing console once you correct bracket.
  • num array is never used. Do you need it?

I would start over and first make a simple working grid and display row and column co-ordinates . Then… look at the numbers and look for patterns and math that you can use for the outcome you want.

You do not need an array for the simple pattern that you want.

If you do use an array with the first line initialized you could also make that work if that is an initial condition you are working with.

Snippets from code I wrote:

int rows = 10, cols = 20; // number of rows and columns

// 2D array
int[][] numbers = new int [rows][cols]; // Initialized to all zeroes

I added brackets and a comma to text output:

String s1 = "(" + str(col) + "," + str(row) + ")";  // Like (x, y) co-ordinates
//String s2 = "(" + str(row) + "," + str(col) + ")";  // (y, x)
text(s1, col*wc + wc/2, row*hr + hr/2);  // Try s1 or s2 here!

You can just use row and column for text for starters if that is easier for you.

:)

1 Like