# Ask simple questions about arrays~~thinks~~

**URL:** <https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675>\
**Category:** Beginners\
**Created:** [March 15, 2020, 10:44am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675 "2020-03-15T10:44:15Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 15, 2020, 10:44am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/1 "2020-03-15T10:44:15Z")

</div>

QUESTION\_1：Why does this code work out to be 0000000000，it isn’t 46810… 🤔

```auto
int [] values_1 = new int [10];
for (int i = 1; i < values_1.length; i++){
  values_1[i] = values_1[i] * 2;
  print(values_1[i]);
}

```

QUESTION\_2：Why did I make an error using the examples in the book (inaccessible code)

```auto
float[] values = new float[1000];
int n = 0;
while (0 < 10) {
  values[n] = random(0, 10);
  n = n + 1;
  print(values[n]);
}

```

😌 😌

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [March 15, 2020, 11:04am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/2 "2020-03-15T11:04:15Z")

</div>

Hello @KEEP,

For you first question, you create an array of 10 slots with only zeros.  
Then you say that the value of each slot is taht value times 2 but 0 x 2 is always 0.

For your second question, you have a typo there: `while (0 < 10) {`  
0 is always lower than 10 so this is an infinite loop. It should be n\<10.

---

<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:** [March 15, 2020, 11:07am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/3 "2020-03-15T11:07:33Z")

</div>

> [@KEEP](#):
>
> Why does this code work out to be 0000000000

Command print() prints into one line without line feed.

The array content is 0 at the beginning and 0 times 2 is always 0

---

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 15, 2020, 11:23am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/4 "2020-03-15T11:23:18Z")

</div>

Thinks! I solved the question\_2 that doesn’t make an error about inaccessible code!  
But how can I make this code output different values according to the progression of n or i？ 🥰 🥰

---

<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:** [March 15, 2020, 11:24am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/5 "2020-03-15T11:24:32Z")

</div>

> [@KEEP](#):
>
> how can I make this code output different values according to the progression of n

> [@KEEP](#):
>
> values[n] = random(0, 10);

values[n] = random(n, n+10);

OR

values[n] = n;

---

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 15, 2020, 11:29am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/6 "2020-03-15T11:29:38Z")

</div>

Thinks! I tried, but the answer was the same 0.0

```auto
float[] values = new float[1000];
int n = 1;
while (n < 10) {
  values[n] = random(n, n+10);
  n=n+1;
  println(values[n]);
}

```

The code run result:

```auto
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

```

---

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 15, 2020, 11:30am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/7 "2020-03-15T11:30:50Z")

</div>

values[n] = n It is same result 😢

---

<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:** [March 15, 2020, 11:37am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/8 "2020-03-15T11:37:18Z")

</div>

Is this question 1 or 2?

Post your entire code please

---

<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:** [March 15, 2020, 11:46am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/9 "2020-03-15T11:46:41Z")

</div>

> [@KEEP](#):
>
> while (n \< 10) {

To see something try with \<100

---

<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:** [March 15, 2020, 11:48am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/10 "2020-03-15T11:48:28Z")

</div>

> [@KEEP](#):
>
> n=n+1;  
> println(values[n]);

Ah, swap those 2 lines

Because you print now the line that has not been altered yet (you print the next line)

---

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 15, 2020, 12:31pm UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/11 "2020-03-15T12:31:37Z")

</div>

I’m sorry. I was called away 😅

question 1 code:

```auto
int [] values_1 = new int [10];
for (int i = 1; i < values_1.length; i++){
  values_1[i] = values_1[i] * 2;
  println(values_1[i]);
}

```

question 2 code:

```auto
float[] values = new float[1000];
int n = 1;
while (n < 100) {
  values[n] = n;
  n=n+1;
  println(values[n]);
}

```

---

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 15, 2020, 12:39pm UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/12 "2020-03-15T12:39:16Z")

</div>

Their answers are all the same value  
The QUESTION\_1 result:  
0  
0  
0  
0  
0  
0  
0  
0  
0  
The QUESTION\_2 result:  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
…

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [March 15, 2020, 1:47pm UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/13 "2020-03-15T13:47:19Z")

</div>

As @Chrisir said, you need to put `n = n + 1;` after your `println()`.

Let’s imagine n = 2. What you are currently doing is:

- Fill values[2] with a random number
- Add 1 to n so n is now 2 + 1 = 3
- Print in the console the value of values[3]

Since you haven’t defined a value for values[3] yet it will display 0.

What you want is swap the last 2 steps:

- Fill values[2] with a random value
- Print values[2] in the console
- Increase the value of n so n =3 now

---

<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:** [March 15, 2020, 1:52pm UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/14 "2020-03-15T13:52:23Z")

</div>

Question 1 code:

> [@KEEP](#):
>
> values\_1[i] = values\_1[i] \* 2; // always 0

Try `values_1[i] = i*2;`

---

<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:** [March 15, 2020, 1:57pm UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/15 "2020-03-15T13:57:04Z")

</div>

Question 2 code:

> [@KEEP](#):
>
> values[n] = n;  
> n=n+1;  
> println(values[n]);

Better:

```auto
  values[n] = n;
  println(values[n]);
  n=n+1;

```

---

<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:** [March 15, 2020, 2:37pm UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/16 "2020-03-15T14:37:27Z")

</div>

> [@KEEP](#):
>
> Why did I make an error using the examples in the book (inaccessible code)

The condition was always true and the code that follows it is:

> Unreachable code

Code to test:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4b9bdbada963847bf4d784d7b5cc2797afc7ed09.png)

This is the code that Processing generates in the background and can be found in the temp file:

```auto
import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class sketch_200315a extends PApplet {
  public void setup() {
while (true) 
  {
  }
 
    noLoop();
  }

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "sketch_200315a" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

```

`:)`

---

<div class="post-metadata">

**Author:** ![KEEP](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keep/32/7910_2.png) [@KEEP](https://discourse.processing.org/u/KEEP)\
**Post date:** [March 22, 2020, 3:09am UTC](https://discourse.processing.org/t/ask-simple-questions-about-arrays-thinks/18675/17 "2020-03-22T03:09:34Z")

</div>

The reason is here 😂 😂 😂 😂 😂
