在学习了 2 年的 java 之后,我大约在 3 周前开始学习 c++。看起来很不一样,但我到了那里。我的讲师是一个可爱的人,但任何时候我问一个问题,为什么事情会这样或那样。他只是回答“因为它是”。
下面的代码中有很多注释,带有一些随机问题,但主要问题是我遇到了两个构建错误,一个说 arraytotal 尚未初始化(即使我找到了它的值),另一个说外部引用主要。
有人会介意阅读代码并回答其中的一些评论,也许是我遇到的整体问题?
#include<string>
#include<fstream>
#include<ostream>
using namespace std;
//double decimals[5] ={2,4,6,8,10};
const int arraySize = 5;
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
//double decimals[arraySize];
/*
this array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
*/
// why do you have to write the name of the function up here before you even create it?
double averageN();
int main()
{
averageN();
return 0;
}
// why does the array have to be created here?
double averageN(double decimals[arraySize])
{
double average;
double arrayTotal;
for (int i = 0; i<5;i++)
{
// fills with random numbers from 0 - 10
decimals[i] = (0+(rand()%10));
}
// find the total of all the elements in the array
for (int i = 0; i < arraySize;i++)
{
double currentElement = decimals[i];
arrayTotal = (currentElement+arrayTotal);
//arrayTotal +=decimals[i]) ;
}
// return the average
average = (arrayTotal/arraySize);
return 0.0;
}