Problem with arithmetic mean

So I was trying to read the output a potenciometer gives a hundred times and to get the arithmetic mean of that hundred outputs shown by the serial comm. in my computer, but it doesn't seem to calculate the mean. I can't find the error, please help me.. :cold_sweat:

int x=0;
void setup() {
  Serial.begin(9600);
}
void loop() { 
  int values[x];
  if(x<100) {
    x++;
    values[x]=analogRead(A0);
    delay(10);
  };
  Serial.print("The ");
  Serial.print(x);
  Serial.print(" time this pot has a value of ");
  Serial.println(values[x]);
  int y;
  unsigned long sum=0;
  for(y=0; y<=x; y++) {
    sum=(sum + values[y]);
  };
  long mean=sum / 101;
  if (x == 100) {
    Serial.print("The mean is ");
    Serial.print(mean);
    delay(10000);
    x=0;
  };
}

In the serial monitor it shows this up:

The 1 time this pot has a value of 569
The 2 time this pot has a value of 569
The 3 time this pot has a value of 570
The 4 time this pot has a value of 570
The 5 time this pot has a value of 569
The 6 time this pot has a value of 570
The 7 time this pot has a value of 570
The 8 time this pot has a value of 570
The 9 time this pot has a value of 570
The 10 time this pot has a value of 570
The 11 time this pot has a value of 570
The 12 time this pot has a value of 570
The 13 time this pot has a value of 570
The 14 time this pot has a value of 570
The 15 time this pot has a value of 569
The 16 time this pot has a value of 570
The 17 time this pot has a value of 570
The 18 time this pot has a value of 569
The 19 time this pot has a value of 570
The 20 time this pot has a value of 570
The 21 time this pot has a value of 570
The 22 time this pot has a value of 570
The 23 time this pot has a value of 569
The 24 time this pot has a value of 570
The 25 time this pot has a value of 570
The 26 time this pot has a value of 570
The 27 time this pot has a value of 570
The 28 time this pot has a value of 570
The 29 time this pot has a value of 570
The 30 time this pot has a value of 570
The 31 time this pot has a value of 570
The 32 time this pot has a value of 570
The 33 time this pot has a value of 570
The 34 time this pot has a value of 570
The 35 time this pot has a value of 570
The 36 time this pot has a value of 570
The 37 time this pot has a value of 570
The 38 time this pot has a value of 570
The 39 time this pot has a value of 570
The 40 time this pot has a value of 570
The 41 time this pot has a value of 570
The 42 time this pot has a value of 570
The 43 time this pot has a value of 570
The 44 time this pot has a value of 570
The 45 time this pot has a value of 570
The 46 time this pot has a value of 570
The 47 time this pot has a value of 570
The 48 time this pot has a value of 570
The 49 time this pot has a value of 570
The 50 time this pot has a value of 569
The 51 time this pot has a value of 570
The 52 time this pot has a value of 570
The 53 time this pot has a value of 570
The 54 time this pot has a value of 570
The 55 time this pot has a value of 569
The 56 time this pot has a value of 570
The 57 time this pot has a value of 570
The 58 time this pot has a value of 570
The 59 time this pot has a value of 570
The 60 time this pot has a value of 569
The 61 time this pot has a value of 570
The 62 time this pot has a value of 569
The 63 time this pot has a value of 570
The 64 time this pot has a value of 570
The 65 time this pot has a value of 569
The 66 time this pot has a value of 570
The 67 time this pot has a value of 570
The 68 time this pot has a value of 569
The 69 time this pot has a value of 570
The 70 time this pot has a value of 569
The 71 time this pot has a value of 569
The 72 time this pot has a value of 570
The 73 time this pot has a value of 569
The 74 time this pot has a value of 569
The 75 time this pot has a value of 569
The 76 time this pot has a value of 570
The 77 time this pot has a value of 569
The 78 time this pot has a value of 570
The 79 time this pot has a value of 570
The 80 time this pot has a value of 570
The 81 time this pot has a value of 570
The 82 time this pot has a value of 569
The 83 time this pot has a value of 570
The 84 time this pot has a value of 570
The 85 time this pot has a value of 570
The 86 time this pot has a value of 570
The 87 time this pot has a value of 569
The 88 time this pot has a value of 570
The 89 time this pot has a value of 570
The 90 time this pot has a value of 570
The 91 time this pot has a value of 570
The 92 time this pot has a value of 569
The 93 time this pot has a value of 570
The 94 time this pot has a value of 570
The 95 time this pot has a value of 570
The 96 time this pot has a value of 570
The 97 time this pot has a value of 569
The 98 time this pot has a value of 570
The 99 time this pot has a value of 570
The 100 time this pot has a value of 570
The mean is 0

I don't know why does this happen, maybe you could lend me a hand :smiley:

  int values[x];

How many elements do you think this array will hold? If you said more than 0, you are wrong. In the rest of the code, you are stomping all over memory you don't own.

You calculate the sum but never use it. Then calculate mean =mean / 101 which doesn't look right. Maybe mean= sum/100.

It has to hold 101 element, doesn't it?
Sorry but I am just starting and things get a bit messed up in my head when it comes to memory stuff. Maybe int values[x] must be a long instead of an int? I haven't though on that before :sweat_smile:

Thanks, Groundfungus, I meant to write sum. Also, after I uploaded the "correct" code, the serial monitor said:

The mean is 42510846

It has to hold 101 element, doesn't it?

No. It holds 0. That is the value of x when the array is allocated. You can't "dynamically" allocate an array that way. You need to statically size the array.

  long mean=mean / 101;

Why are creating ANOTHER variable called mean?

There is just so much wrong with that code that it is hard to know where to begin. So, I won't. Get a C book and learn something about how to program before you try this again.

Well, sorry for being a newbie... It is a bit difficult to learn how to program if you only read books and don't have a teacher to ask doubts like this...

void loop() { 
  int values[x];

Whoa! That array is created on entering loop(), and thrown away every time loop
exits.... That's is not what you want.

Try:

int values [100] ;

void loop ()
{

But wait a minute, what are you using an array for - just record the
sum and the count of readings if you only want the mean:

int count = 0 ;
long sum = 0L ;

void loop ()
{
  sum += analogRead (A0) ;
  count ++ ;

  if (count == 100)
  {
    Serial.print ("mean is ") ;
    Serial.println ((float) sum / count) ;
    count = 0 ;
    sum = 0L ;
    delay (1000) ;
  }
}

I was using an array to print out the value it reads each time, I don't want it to output just the mean, I want it to show all the values and then print the mean. Thanks a lot, MarkT

I was using an array to print out the value it reads each time, I don't want it to output just the mean, I want it to show all the values

You don't need an (incorrectly defined) array for that. A simple integer variable to hold the last value read, so it can be printed and then summed is sufficient.

int reading = 0;
int count = 0 ;
long sum = 0L ;

void loop ()
{
  reading = analogRead(A0);
  Serial.println(reading);
  sum += reading ;
  count ++ ;

  if (count == 100)
  {
    Serial.print ("mean is ") ;
    Serial.println ((float) sum / count) ;
    count = 0 ;
    sum = 0L ;
    delay (1000) ;
  }
}