将数组传递给函数时出错

时间:2014-07-01 18:43:39

标签: c++ arrays

我对C ++很陌生,正在编写支持执行以下操作的程序:

  1. 根据用户定义的常量填充整数数组。
  2. 将步骤#1中的数组传递给计算数组中整数均值的函数。
  3. 将步骤#2中的数组传递给计算数组中整数标准差的函数。
  4. 这是我的代码:

    #include "stdafx.h"
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    const int SIZE_OF_ARRAY = 100;
    int custArray[];
    
    void fillArray(int SIZE_OF_ARRAY);
    double standardDeviation(double[], int);
    double mean(double[], int);
    double arithmeticAverage;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int i;
        fillArray(SIZE_OF_ARRAY);
        cout << "\n";
        cout << "The mean is: " << mean(custArray[], i);
        cout << endl;
        cout << "The standard deviation is: " << standardDeviation(custArray[], i);
    
        return 0;
    }
    
    void fillArray(int SIZE_OF_ARRAY)
    {
        int i = 0;
        custArray[0] = { 1 };
        for (int i = 0; i < SIZE_OF_ARRAY; i++)
         custArray[i] = i + 1;
    
        return;
    }
    
    double mean(double custArray[], int SIZE_OF_ARRAY)
    {
        double sumOfElements = 0;
        int i;
        for (i = 0; i < SIZE_OF_ARRAY; i++)
        {
        sumOfElements += custArray[i];
        }
        arithmeticAverage = sumOfElements / i;
        return (arithmeticAverage);
     }
    
    double standardDeviation(double custArray[], int SIZE_OF_ARRAY)
    {
        double standardDeviation;
        double tempSum = 0;
    
        for (int i = 0; i < SIZE_OF_ARRAY; i++)
        {
        tempSum += pow((custArray[i] - arithmeticAverage), 2);
        }
        standardDeviation = sqrt(tempSum / (SIZE_OF_ARRAY));
        return (standardDeviation);
    }
    

    我在以下代码行中收到错误:

    cout << "The mean is: " << mean(custArray[], i);
    

    cout << "The standard deviation is: " << standardDeviation(custArray[], i);
    

    其中包含:&#34;语法错误:&#39;]&#39;

    为什么?

2 个答案:

答案 0 :(得分:2)

你的程序中存在很多问题。但是,我确实修复了它并且编译没有错误。

  • 首先,您不会将数组传递到名称后面带[]的功能区;你只需输入变量名称。

  • 第二次,您的mean()standardDeviation()函数都将您的数组声明为double

  • 第三次,您从i传递了未初始化的变量main()。我想你打算通过SIZE_OF_ARRAY

  • 第四,您只需要创建这样的主函数:int main()。您的原始程序具有int _tmain(int argc, _TCHAR* argv[]),这是Microsoft特定的标准。

#include <iostream>
#include <cmath>
using namespace std;

const int SIZE_OF_ARRAY = 100;
int custArray[SIZE_OF_ARRAY];

void fillArray(int SIZE_OF_ARRAY);
double standardDeviation(int[], int);
double mean(int[], int);
double arithmeticAverage;

int main()
{

    //int i; no need for this

    fillArray(SIZE_OF_ARRAY);

    cout << "\n";
    cout << "The mean is: " << mean(custArray, SIZE_OF_ARRAY);
    cout << endl;
    cout << "The standard deviation is: " << standardDeviation(custArray, SIZE_OF_ARRAY);

    return 0;
}


void fillArray(int SIZE_OF_ARRAY)
{
    int i = 0;

    custArray[0] = { 1 };

    for (int i = 0; i < SIZE_OF_ARRAY; i++)
         custArray[i] = i + 1;

    return;
}

double mean(int custArray[], int SIZE_OF_ARRAY)
{
    double sumOfElements = 0;
    int i;

    for (i = 0; i < SIZE_OF_ARRAY; i++)
    {
        sumOfElements += custArray[i];
    }
    arithmeticAverage = sumOfElements / i;
    return (arithmeticAverage);
 }

double standardDeviation(int custArray[], int SIZE_OF_ARRAY)
{
    double standardDeviation;
    double tempSum = 0;

    for (int i = 0; i < SIZE_OF_ARRAY; i++)
    {
        tempSum += pow((custArray[i] - arithmeticAverage), 2);
    }
    standardDeviation = sqrt(tempSum / (SIZE_OF_ARRAY));
    return (standardDeviation);
}

答案 1 :(得分:1)

错误是因为函数调用参数中的括号。只需删除它们:

mean(custArray, i)