Java を 2 年間使用した後、約 3 週間前に C++ の学習を開始しました。とても違うようですが、私はそこに着いています。私の講師は素敵な人ですが、何かがなぜそのようであるか、またはこのようであるかについて質問するときはいつでも. 彼はただ「そうだから」と答えます。
以下のコードには、いくつかのランダムな質問を含む多くのコメントがありますが、主な問題は、2 つのビルド エラーが発生することです。メインで。
誰かがコードを読んで、その中のいくつかのコメントに答えてくれませんか?おそらく全体的な問題はありますか?
#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;
}