数组函数输出错误

时间:2012-12-10 14:41:02

标签: c++ arrays function

我想要数组元素的平方根,但输出是如此错误!  你能帮我纠正我的错误??? 如何将数组更改为100?

 #include <math.h>
    #include <iostream>
    using namespace std;

    // function declaration:
    double sqrootx(int arr[], int size);

    int main ()
    {
       // an int array with 5 elements.
       double balance[5] = {1000, 2, 3, 17, 50};
       double sqr;

       // pass pointer to the array as an argument.
       sqr= pow( balance, 5 ) ;

       // output the returned value 
       cout << " result" << sqr << endl; 

       return 0;
    }

3 个答案:

答案 0 :(得分:2)

为了创建一个大小为100的数组。只需更改 double balance[5];double balance[100];

这就是说,很难手动输入100个数字。 因此,您需要运行LOOP。

根据给出的代码判断,列出代码中缺少的内容。

  1. 循环,用数字填充数组中的每个框。
  2. 战俘功能本身。 (因为您已声明math.h,请使用 sqrt
  3. 另一个循环(迭代)或递归,用平方根值替换数组中的原始数字。
  4. 一个循环或函数,以显示具有所有值平方根的数组。
  5. 某些预定义条件,例如“负值”或某些代码绝对
  6. 希望这有帮助。

答案 1 :(得分:1)

看起来像是一个错字:

sqr= sqrootx( balance, 5 ) ;

答案 2 :(得分:0)

你将数字提高到5的幂。平方根是0.5的幂。

for (int i = 0; i < 5; i++) {
    sqr = pow(balance[i], .5);
    cout << " result" << sqr << endl;
}