大約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