mercredi 18 mars 2020

How do you include the first element in a sequence to find the mean?

In my code I was trying to find the mean/average for a sequence. I used the debugger tool to find out the first element/input was not included in the mean and is throwing the mean off. I was wondering what solution I should used to fix the error. I tried different approaches such as setting my index to 0 and I even tried to do getNumbers()[0] + sum / getNumbers().size();

Here is my code

{
  int index = 0;
  int sum = 0;
  int mean = 0;
  while (index < getNumbers().size())
  {
    sum = sum + getNumbers()[index];
    index = index + 1;
  }
  {
    mean =  (sum) / getNumbers().size();
  }
  return mean;
}

Aucun commentaire:

Enregistrer un commentaire