Looks like your mixing static and active modes

I cannot get processing three to put text on the screen. I get "looks like your mixing static and active modes. Can any one help? Thanks!
Ed

Code:

int rainfall[] = {2, 3, 5, 1, 4, 6, 7, 2, 1};

float average(int[] data) {
   int sum1 = 0;
   for(int i = 0; i < data.length; i++) {
      sum1 += data[i]; 
   }
   
   return (float)(sum1 / data.length);
}

float avgRain = average(rainfall);

textSize(12);
fill(0);
text(avgRain, 10, 20);
1 Like

The problem is that you have some code that is not inside a function.

Either all your code needs to be inside functions (like setup()) and draw()), or you should not use functions at all (in which case your code is just a list of instructions).

Because you have a function (average()), you should put your draw code into draw():

int rainfall[] = {2, 3, 5, 1, 4, 6, 7, 2, 1};
float avgRain;

void setup(){
  size(400,400);
  avgRain = average(rainfall);
}

void draw(){
  background(255);
  textSize(12);
  fill(0);
  text(avgRain, 10, 20);
}

float average(int[] data) {
  int sum1 = 0;
  for(int i = 0; i < data.length; i++) {
    sum1 += data[i];
  }
  return (float)(sum1 / data.length);
}
3 Likes

Thank you TfGuy44! That works great! I did not understand that all code had to be in functions or not in any functions. I missed that somehow. Also thanks for the quick response. Now I understand.
er45

1 Like