C ++ std偏差函数?

时间:2014-11-25 00:32:15

标签: c++ function for-loop

好的,所有传递给这个函数的数字作为参数,都返回正确的值,我无法弄清楚为什么在我运行之后我得到一些荒谬的数字作为标准偏差。即我得到的数字是(4.23947e10)或沿着那些线的东西,显示功能正在跳过每隔一行。

  float stdDev(int arrayList [], int count, float average)
     {
         int deviation;
         int sum2 = 0;
         for (int i = 0; i <= count; i++)
         {
             sum2 += pow((arrayList[i] - average), 2);

         }

         deviation = sqrt(sum2 / (count - 1));
         return deviation;

     }
void displayList(int heightlist [], int weightlist[], int count)
//displays list of integers based on lists
{

        cout << "\tHeight(s)" << " " << "Weight(s)" << endl;
        for (int i = 0; i <= count; i++)
        {
            cout << "\t[" << heightlist[i] << "]" << " " << "["<< weightlist[i] <<"]" << '\n';
            i++;
        }
}

1 个答案:

答案 0 :(得分:6)

我认为你走的太远了:

for (int i = 0; i <= count; i++)
//                ^^

可能意味着i < count

此外:

int deviation;
int sum2 = 0;

你的意思是float deviation, sum2