I have a problem to ask

Goodmorning everyone,
I have a problem with a code I’m working on but I don’t understand what’s wrong, someone who can help me out?

int randomNum;
//boolean contenitore []; 
boolean[] contenitoreVario = new boolean [5];
boolean Positivo = true; 
boolean Negativo = false; 

void setup() {
  size(400, 400);
  background(127);
  frameRate(1);
}
}
void draw() {
  randomNum = random(100);
}


for (int i = 0; i < 1; i++) { 
  if (randomNum <50); 
  contenitore[] = positivo;
 else if (randomNum >50);
contenitore[] = negativo; 
}


println(contenitore); 

thank!

1 Like

Add your code inside draw:

void draw() {
  for (int i = 0; i < 50; i++) {
    contenitore = random(100);
  }
  printArray(contenitore);
}

I suggest you explore the examples available in the main website.

Kf

1 Like

In addition, you can format the code in the forum by selecting your code and hitting the formatting code button which looks like this: </>. Formatting code is a good practice to ensure your code is not modified by the Forum markdown processor when it is published plus it makes your code easier to read.

image

Kf

3 Likes

thanks, but unfortunately it gives me error in any case :frowning:

You have declared the array contenitore twice in the first two lines. So delete the first line.

The ‘=’ sign is the assignment operator, it means evaluate what is on the right-hand-side and store it in the variable on the left-hand-side. The data types for both sides must be the same. So

won’t work because contenitore is an array of booleans and the RHS returns a float

replacing it with contenitore[i] = random(100); won’t work because contenitore[i] is a boolean and random(100) returns a float.

As @kfrager points out you can’t have code outside of a function.

I suggest you look at the examples and simple tutorials for Processing so you understand how code is supposed to be organised. In particular look at the tutorial on setup() and draw().

This is your first time here so I will give you a hint on how to get better help.

Statements like

and

are useless because they don’t say what is wrong or what the error is. You need to give much more information on what you expected to happen, what actually happened and details of any error messages.

BTW welcome to the forum :smile:

1 Like

Hello,

Welcome to the Processing forum.

Please read the Processing FAQ and Guidelines:
1 . https://discourse.processing.org/faq
2 . Guidelines - Tips on Asking Questions
3 . Guidelines - How to Answer Questions

Please format your code:
https://discourse.processing.org/faq#format-your-code

Lots of resources on the Processing website to start you on your journey:
https://processing.org/

:)

1 Like

:thinking:
ok thanks very kind, thanks for the “welcome” possibly today is the first day!
Anyway I didn’t understand much :frowning:
What should I do to get the correct code?

Thank you!

What are you trying to achieve?

1 Like

sorry if I ask you but I need :), I should initialize a boolean type array containing five elements, then assign to each value of the array if it is false or true by establishing it with a ‘for loop’ and at each cycle generate a random from zero to one hundred and if the number was less than 50 then it would be TRUE, otherwise FALSE.
This is what I’m trying to figure out how to do it.
you are very kind, thank you :smiling_face_with_three_hearts:

Is this school work?

I am going to assume that this is school work about arrays so I am not going to give you a fully worked answer but help you do it for yourself.

Since there is no graphics we don’t need a draw() method.

The for loop needs to be in a function in this case setup() so the basic code is

// Global variables

// setup function which is executed once when the sketch starts
void setup(){
  // All your code will go here
}

In terms of global variables you only need the boolean array and an int for the random number which you have already created so lets add that and some pseudocode for the your code

// Global variables
int randomNum;
boolean[] contenitoreVario = new boolean [5];

// setup function which is executed once when the sketch starts
void setup(){
  // pseudocode V V V V V V V V V 
  // for each element in our array
  //    get a random number in the range >=0 and < 100 and store it in our variable
  //   if the number is less than 50
  //      store TRUE in the current array element
  //    else
  //      store FALSE in the current array element
  //     end if 
  //  end for
  // print array
}
2 Likes

Inside your for loop you have

This will not work for two reasons

  1. negativo is undefined because the global variable is Negativo and in Java variable names are case sensitive so these are two different variables.
  2. contenitore[] represent the whole array you need contenitore[i] to represent the current array element

I think it is time for you to try again. Please post your sketch below instead of editing the original post.

2 Likes

no, I don’t go to school anymore, she is a friend of mine who needs, I had done something when I went to school but some time has passed :cold_sweat:

thank you, very kind :blush: