# Array with for loop exercise in Nyhoff book

**URL:** <https://discourse.processing.org/t/array-with-for-loop-exercise-in-nyhoff-book/19277>\
**Category:** Coding Questions\
**Created:** [April 1, 2020, 5:18am UTC](https://discourse.processing.org/t/array-with-for-loop-exercise-in-nyhoff-book/19277 "2020-04-01T05:18:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 1, 2020, 5:18am UTC](https://discourse.processing.org/t/array-with-for-loop-exercise-in-nyhoff-book/19277/1 "2020-04-01T05:18:54Z")

</div>

This is an exercise in the Nyhoff book. I am trying to figure out **how to output to the consul** the column position and the row position 5 times. So that it reads like this:

column: random number, row: random number  
column: random number, row: random number  
column: random number, row: random number  
column: random number, row: random number  
column: random number, row: random number

Instead of getting the random number in the array, I am getting the array location. How do I access the individual random numbers?

```auto
size (150, 200);

int [] columns = new int[5];
int [] rows = new int[5];

for (int i = 0; i < columns.length; i++)
{
  columns [i] = int (random(0, width));
  printArray (columns[i]);
}

for (int j = 0; j < rows.length; j++)
{
  rows [j] = int (random(0, height));
  printArray (rows[j]);
}

for (int k = 0; k < rows.length; k++)
{
  println ("columns: " + columns + "," + "rows: " + rows);
}

```

---

<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:** [April 1, 2020, 8:59am UTC](https://discourse.processing.org/t/array-with-for-loop-exercise-in-nyhoff-book/19277/2 "2020-04-01T08:59:08Z")

</div>

It sounds like it should be a 2D array (grid) and not two 1D array (list) - see tutorials

see [https://www.processing.org/tutorials/arrays/](https://www.processing.org/tutorials/arrays/)

see [https://www.processing.org/tutorials/2darray/](https://www.processing.org/tutorials/2darray/)

---

<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:** [April 1, 2020, 9:06am UTC](https://discourse.processing.org/t/array-with-for-loop-exercise-in-nyhoff-book/19277/3 "2020-04-01T09:06:23Z")

</div>

but as a 1D array Sketch it works like this

- printArray (columns); should be outside the for loops. And it can receive the entire array as a parameter ( `columns`), not only one slot (`columns[i]`)

- In the 3rd for-loop: to address one slot within an array, use: `columns [k]`

Chrisir

```auto

size (150, 200);

int [] columns = new int[5];
int [] rows = new int[5];

// define columns  
for (int i = 0; i < columns.length; i++)
{
  columns [i] = int (random(0, width));
}
println("-----------------");
printArray (columns);

// define rows 
for (int j = 0; j < rows.length; j++)
{
  rows [j] = int (random(0, height));
}
println("-----------------");
printArray (rows);

// ------------------------------------------------------------------

// show content
println("-----------------");
for (int k = 0; k < rows.length; k++)
{
  println ("columns: " 
    + columns [k] 
    + ","
    + "rows: "
    + rows [k]);
}
//

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 1, 2020, 5:44pm UTC](https://discourse.processing.org/t/array-with-for-loop-exercise-in-nyhoff-book/19277/4 "2020-04-01T17:44:45Z")

</div>

Thank you @Chrisir!!

Yes, at one point I thought to try solving this using 2D array approach. However, because this exercise follows their (the Nyhoffs’) chapter on 1D arrays I wanted to make sure I could do that via 1D as theoretically it’s a simpler structure… (though still it kicked my butt…) 🙂🤓

Thank you again!!
