簡體   English   中英

C++中的隨機數組值?

[英]Random array value in c++?

您好,我可能在這里犯了一個愚蠢的錯誤,但我想知道是否有人可以幫助我! 我應該使用給定的算法將值存儲在大小為 20 的數組中,但是在我的第 8 位數字之后,我不斷得到一些對我來說似乎沒有意義的大數字......

for(loopCounter = 0; loopCounter < MAX_SIZE; loopCounter++)
{
    //Fun array is computed using the algorithm " fun[i] = 7 * i^2 - 4 * i - 500 ".
    fun[loopCounter] = (7 * (loopCounter * loopCounter)) - (4 * loopCounter) - 500;

    //Output

    cout << fun[loopCounter];

}

(MAX_SIZE 聲明為 20)

在 Visual Studio 中運行您的代碼后,我注意到了一些事情。 您的代碼如您所願完美運行。 這里唯一的問題是這個偽隨機數生成器總是從數組的索引 8 生成正數。 所以它看起來像一個大數字。 所以如果你添加 << endl; 在 cout 行的末尾,您將完美地看到結果。

for (int loopCounter = 0; loopCounter < MAX_SIZE; loopCounter++){

    fun[loopCounter] = (7 * (loopCounter * loopCounter)) - (4 * loopCounter) - 500;
    std::cout << fun[loopCounter] << std::endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM