# Multidimensional array

**URL:** <https://discourse.processing.org/t/multidimensional-array/37212>\
**Category:** Coding Questions\
**Created:** [May 29, 2022, 9:53pm UTC](https://discourse.processing.org/t/multidimensional-array/37212 "2022-05-29T21:53:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvb](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mvb](https://discourse.processing.org/u/mvb)\
**Post date:** [May 29, 2022, 9:53pm UTC](https://discourse.processing.org/t/multidimensional-array/37212/1 "2022-05-29T21:53:18Z")

</div>

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.)

```auto

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);
    }
  }
}

```

 ![Captura de Pantalla 2022-05-29 a les 23.51.49](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/8/7864d6a25c11a241ef58cd11ac38c9dc2bb0ff55.png)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 29, 2022, 10:29pm UTC](https://discourse.processing.org/t/multidimensional-array/37212/2 "2022-05-29T22:29:53Z")

</div>

Hi  
This link closer idea

> [@How to text array](https://discourse.processing.org/t/how-to-text-array/30192/12):
>
> void setup () { size ( 800 , 800 ); } void draw () { fill(0); int[] x = { 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 676, 61, 83, 69, 71, 50, 29, 31, 17, 39 }; fill(0); int row = 0; float spacing = 25; float xx = 0; for (int i = 0; i \< x.length;…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 30, 2022, 7:45am UTC](https://discourse.processing.org/t/multidimensional-array/37212/3 "2022-05-30T07:45:57Z")

</div>

> [@mvb](#):
>
> ```auto
> if(numbers[i][j] !=0){
> num= + 1;
> }
> 
> ```

Replace this with

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 30, 2022, 11:21am UTC](https://discourse.processing.org/t/multidimensional-array/37212/4 "2022-05-30T11:21:02Z")

</div>

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:

```auto
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:

```auto
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.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/6/46eceebdbd23c8d82d6096515edae8fb38805b30.png)

`:)`
