将数组传递给函数时出错
Error when passing array to function
我对C++非常陌生,正在编写一个程序,该程序应该可以执行以下操作:
- 根据用户定义的常量填充一个整数数组
- 将步骤#1中的数组传递给计算数组中整数平均值的函数
- 将步骤#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)
相关文章:
- 类中私有结构的构造函数出错
- 字符串验证函数出错
- 我的类默认构造函数出错
- Dev-C++和Code::Blocks中的析构函数出错
- int对的哈希函数出错
- MSVC 编译器错误导致迭代器和友元函数出错
- 复制构造函数出错
- 使用 WinSock2 开发,使用 CreateThread() 函数出错
- 链接列表的Push_back函数出错
- 结构初始化中的隐式复制构造函数出错
- 异步函数出错
- 编译器错误消息错误:令牌之前的预期')' '*'。我的构造函数出错
- c++中使用std::bind和std::函数出错
- 在vector排序中使用c++模板函数出错
- 使用std::for_each lambda函数出错
- 二叉搜索树插入函数出错
- 调用类的函数出错
- c++使用删除函数出错
- 使用已删除的函数出错
- 调用构造函数出错