大约3周前,我在2年的java开始学习C++。它看起来很不同,但即时到达那里。我的讲师是一个可爱的家伙,但任何时候我都会问一个问题,为什么这样或者那样。他只是回应“因为它”。我有很多关于C++的问题,让我很困惑
Theres在下面的代码中有很多注释,有一些随机问题,但主要问题是即时获取两个构建错误,一个说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;
}
是否会编译代码?它似乎没有。先编写一段可编译的代码以更好地理解 – 2010-10-13 13:31:15
请检查一下您的文章。我认为你需要格式化文本。我可以看到#include #include #include。头文件名称丢失。 – 2010-10-13 13:33:58
这就是问题的一部分。我不知道为什么它不会建立。 – OVERTONE 2010-10-13 13:34:19