Ask simple questions about arrays~~thinks~~

QUESTION_1:Why does this code work out to be 0000000000,it isn’t 46810… :thinking:

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)

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

:relieved: :relieved:

1 Like

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.

2 Likes

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

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

1 Like

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? :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

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

OR

values[n] = n;

1 Like

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

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:

0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

values[n] = n It is same result :cry:

Is this question 1 or 2?

Post your entire code please

2 Likes

To see something try with <100

1 Like

Ah, swap those 2 lines

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

2 Likes

I’m sorry. I was called away :sweat_smile:

question 1 code:

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:

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

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

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
2 Likes

Question 1 code:

Try values_1[i] = i*2;

1 Like

Question 2 code:

Better:

  values[n] = n;
  println(values[n]);
  n=n+1;
2 Likes

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

Unreachable code

Code to test:

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

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

:)

2 Likes

The reason is here :joy: :joy: :joy: :joy: :joy:

1 Like