将数组传递给函数时出错

Error when passing array to function

本文关键字:函数 出错 数组      更新时间:2023-10-16

我对C++非常陌生,正在编写一个程序,该程序应该可以执行以下操作:

  1. 根据用户定义的常量填充一个整数数组
  2. 将步骤#1中的数组传递给计算数组中整数平均值的函数
  3. 将步骤#2中的数组传递给计算数组中整数的标准偏差的函数

这是我的代码:

#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);

上面写着:"语法错误:']'

为什么?

您的程序中有很多问题。然而,我确实修复了它们,它编译时没有出现错误。

  • 首先,您不需要将数组传递到名称后带有[]的函数中;只需键入变量名即可。

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

  • 第三个,您从未初始化的main()传递了变量i。我想你是想通过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);
}

错误是由于函数调用参数中的方括号引起的。只需删除它们,如:

mean(custArray, i)