# How to calculate two different mean values with the same function

**URL:** <https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647>\
**Category:** Coding Questions\
**Created:** [January 19, 2019, 3:06pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647 "2019-01-19T15:06:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thatone](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thatone](https://discourse.processing.org/u/thatone)\
**Post date:** [January 19, 2019, 3:06pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/1 "2019-01-19T15:06:40Z")

</div>

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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 19, 2019, 4:50pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/2 "2019-01-19T16:50:05Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![thatone](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thatone](https://discourse.processing.org/u/thatone)\
**Post date:** [January 20, 2019, 1:07pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/3 "2019-01-20T13:07:53Z")

</div>

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 😕

Usually it has been something like this:

```auto
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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 20, 2019, 1:34pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/4 "2019-01-20T13:34:11Z")

</div>

a function can return a array

```auto
// 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

*/

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 20, 2019, 3:15pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/5 "2019-01-20T15:15:04Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![thatone](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thatone](https://discourse.processing.org/u/thatone)\
**Post date:** [January 20, 2019, 4:09pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/6 "2019-01-20T16:09:13Z")

</div>

Thank you so much @kll and @Architector_4

---

<div class="post-metadata">

**Author:** ![thatone](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thatone](https://discourse.processing.org/u/thatone)\
**Post date:** [January 21, 2019, 4:19pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/7 "2019-01-21T16:19:38Z")

</div>

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 😕

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 21, 2019, 4:31pm UTC](https://discourse.processing.org/t/how-to-calculate-two-different-mean-values-with-the-same-function/7647/8 "2019-01-21T16:31:49Z")

</div>

```auto
  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;`
