How to calculate two different mean values with the same function

Hello,

I’m a bit in a problem. I’m supposed to get this function to work for the followings array, but I only get it to work it one. Perhaps I’m not thinking right, or it’s easier than I’m trying to it to be. Anyone that has any idea, would really be appreciated…

The code is the following:

void setup() {
  background(0);
  float[] number1= {45, 12, 34, 56.2, 23.3};
  float[] number2= {1.565, 3.3333, 4.9875654321, 3.21};
  float m1= meanvalue(number1);
  text(m1, 20, 30);
  float m2= meanvalue(number2);
  text(m2, 20, 60);
}

float meanvalue(float[] number1) {
  float sum=number1[0]+number1[1]+number1[2]+number1[3];  
   float b=sum/number1.length;
 
  return b;
}

Please, use the little </> icon above the text box to format the code into actual code, so that it would look better in the comments, i.e. like this.

Anyways, the problem in your function is that it only does it for numbers 0, 1, 2 and 3 - and not any more. Shouldn’t it also read number 4, 5, 6, however many of them are there?

The solution is the for loop, which can iterate over all numbers of an array no matter how long or short it is. The syntax for for loop is simple:
for(counter variable declaration; condition to stop the loop; increment of the variable){...

For example:

for(int i=0; i < 5; i++){
  println( i );
}

This will go through all numbers up until 5, and will print 0 1 2 3 4. You can change that 5 to any number, or even a variable that is a number, and have it do its thing as much as you need.

So, in your function, I suggest doing just float sum = 0;, and then using a for loop to add all of numbers from the array to the sum variable.

1 Like

Hello again,

I understand that I should use a for loop, but don’t understand how I will the two arrays, to preform the same meanvalue function and get two return types. I’ve tried many types but never got the hang of it…

I’m sorry that I that I don’t completely understand this, but it is what is is :confused:

Usually it has been something like this:

float meanvalue(float[] number1, float[] number2) {
float sum=number1[0]+number1[1]+number1[2]+number1[3];
float b=sum/number1.length; 
float v=number2[0]+number2[1]+number2[2]+number2[3];
float c=v/number2.length;

return b && c;

or like this:

float meanvalue(float[] number1, float[] number2) {
for( int i=0; i<5, i++){
 float sum=0;
float b=number[i]+number1[i]+number1[i]+number1[i];
float sum=b/number1.length;
}

a function can return a array

// https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/3

float[] as={0, 1, 2, 3};
float[] bs={4, 5, 6};

void setup() {
  println("as: ");
  printArray(as);
  println("bs: ");
  printArray(bs);
  println("mean1, mean2, mean all");
  printArray ( calc_something(as, bs) );
}

float[] calc_something(float[] number1, float[] number2) {
  float[] results={0, 0, 0};
  float sum1 = 0,sum2 = 0;
  for ( int i=0; i < number1.length; i++) sum1 +=number1[i];
  results[0] = sum1/float(number1.length);
  for ( int i=0; i < number2.length; i++) sum2 +=number2[i];
  results[1] = sum2/float(number2.length);
  results[2] = ( sum1+sum2 )/( float(number1.length) + float(number2.length) );

  return results;
}


/*


as: 
[0] 0.0
[1] 1.0
[2] 2.0
[3] 3.0
bs: 
[0] 4.0
[1] 5.0
[2] 6.0
mean1, mean2, mean all
[0] 1.5
[1] 5.0
[2] 3.0

*/

1 Like

I don’t think I understand your question completely, but I will try saying random things that could help.

A way to use the for loop for this function is like this:

float meanvalue(float[] a){
  float sum = 0;
  for(int i=0; i<a.length; i++){
    sum = sum + a[i];
  }
  sum = sum / a.length;
  return sum;
}

This way, every single value inside of any array you give to it gets added, and a mean value is returned. The name for the input array in the function definition does not matter completely, as long as you don’t confuse it with values that are outside of the function.

So, now you can do println(meanvalue(number1)); and println(meanvalue(number2));, and both will work with this function.

1 Like

Thank you so much @kll and @Architector_4

1 Like

Hello again, if anyone has time or a tip to what I’m doing wrong here again. So this code is a follow up to the one above, but in this case the user will decide how many values the array can have and what these values are. However, I only get it to write them out once and the function doesn’t work with with then. (It probably has something to do with the first for loop, but I don’t know how exactly :confused:

import javax.swing.JOptionPane;

String str=JOptionPane.showInputDialog("How many values?");
float c=float(str);

void setup() {
  background(0);
   

for(int i=0; i < c; i++){
   String str1=JOptionPane.showInputDialog(" Write a number " + i);
float d=float(str1);
   
   float[] number1={d};
  float m1= meanvalue(number1);
   text(m1, 20, 30);
}


}

float meanvalue(float[] number1){
  float sum=0;
  for(int i=0; i < number1.length; i++){
     sum=sum+number1[i];
  }
  sum=sum/number1.length; 
  return sum;
}
  float[] number1={d};
  float m1= meanvalue(number1);

this should not be inside the FOR loop.
the array should be declared after you know “c” how long it is

and the mean should be calculated after all data have been entered

and in the FOR loop must assign
number1[i]=d;

1 Like