當我嘗試獲取填充亂數的陣列中出現的每個數字的頻率時。當試圖獲得一個輸出時,所有的數字都是垃圾。
#include <iostream>
using namespace std;
int main()
{
int highestNumber, outputLength, result;
cout << "What's the highest number you want to generate?: ";
cin >> highestNumber;
cout << "How long of a number sequence do you want to generate?: ";
cin >> outputLength;
cout << "Okay we will generate " << outputLength<< " number(s) ranging from 1 to " << highestNumber << "!\n";
srand(time(NULL));
int * randomNumbers = new int[outputLength];
for (int i = 1; i <= outputLength; i ) {
result = rand() % highestNumber 1;
cout << result << ", ";
randomNumbers[i] = result;
}
int * numberCounter = new int[highestNumber];
cout << "\nFrequency:\n";
for (int i = 1; i <= highestNumber; i ) {
int temp2 = randomNumbers[i];
numberCounter [temp2 - 1] = numberCounter [temp2 - 1] 1;
}
for (int i = 1; i <= highestNumber; i ) {
int frequency = 0;
frequency = numberCounter [i] / outputLength;
cout << numberCounter [i] << " occurs " << frequency << " of the time\n";
}
}
輸出:
What's the highest number you want to generate?: 5
How long of a number sequence do you want to generate?: 10
Okay we will generate 10 number(s) ranging from 1 to 5!
5, 3, 3, 1, 1, 5, 4, 3, 1, 3,
Frequency:
-842150451 occurs -84215045 of the time
-842150449 occurs -84215044 of the time
-842150451 occurs -84215045 of the time
-842150450 occurs -84215045 of the time
-33686019 occurs -3368601 of the time
uj5u.com熱心網友回復:
該代碼有幾個問題,下面是固定代碼,其中包含對已修復內容的注釋以及對其他可能更改的一些建議。
總結一下問題:
- 回圈運行,從索引 1 到包含大小,而不是從索引 0 到大小不包含。
- 使用 new 分配整數陣列,假設分配初始化為零,則不是 - 但您可以使用 {} 輕松要求它
- 在第二個回圈中回圈錯誤的大小(回圈最高數字而不是輸出長度)。
- 將兩個 int 相除而不進行強制轉換為 double 并將結果回傳到 int。
- 要求其他人除錯您的代碼,而不是使用除錯器 :-)
最后一個問題是最關鍵的。在自己的代碼中發現錯誤的能力至關重要,這是練習編碼的原因之一。確實,對于小程式,您可能會得到社區的幫助,但是一旦您撰寫了更大規模的代碼,如果您還沒有獲得一些除錯技巧,您就會發現自己陷入困境。
這是固定代碼:
#include <iostream>
// better to avoid using entire namespace
using namespace std;
int main()
{
int highestNumber, outputLength, result;
cout << "What's the highest number you want to generate?: ";
cin >> highestNumber;
cout << "How long of a number sequence do you want to generate?: ";
cin >> outputLength;
cout << "Okay we will generate " << outputLength<< " number(s) ranging from 1 to " << highestNumber << "!\n";
srand(time(NULL));
int * randomNumbers = new int[outputLength];
// changed loop to run on [0, outputLength)
for (int i = 0; i < outputLength; i ) {
result = rand() % highestNumber 1;
cout << result << ", ";
randomNumbers[i] = result;
}
std::cout << std::endl; // added
int * numberCounter = new int[highestNumber]{};
cout << "\nFrequency:\n";
// changed the loop below, need to loop on outputLength
for (int i = 0; i < outputLength; i ) {
int temp2 = randomNumbers[i]; // changed i to i-1
numberCounter [temp2 - 1] = 1; // changed to =1, shorter
}
for (int i = 1; i <= highestNumber; i ) {
double frequency = 0; // changed from int to double
frequency = numberCounter [i-1] / (double)outputLength; // added casting to double
// changed the printout below
cout << "number: " << i << " occurs " << numberCounter [i] << " times, with frequency = " << frequency << "\n";
}
}
uj5u.com熱心網友回復:
您的示例更改為更多 C 編程風格:如果您需要數字 [1,n],則所有數字現在都將在 rang [0,n-1] 中以匹配陣列索引,只需添加一個,但在使用數字作為陣列索引時要小心仍然會從 [0,n-1] 運行
#include <iostream>
#include <vector>
#include <random> // c 's random generation, srand is a "C" left over
// no : using namespace std; avoid using this line, specialy in larger problems
// unless you like nameclashes (https://www.learncpp.com/cpp-tutorial/2-9-naming-collisions-and-an-introduction-to-namespaces/)
int main()
{
//int highestNumber, outputLength, result; teach yourself to initialize values. (though not strictly necessary in this case)
int highestNumber = 0;
int outputLength = 0;
// int result = 0; result is not used in your code
std::cout << "What's the highest number you want to generate?: ";
std::cin >> highestNumber;
std::cout << "How long of a number sequence do you want to generate?: ";
std::cin >> outputLength;
std::cout << "Okay we will generate " << outputLength << " number(s) ranging from 1 to " << highestNumber << "!\n";
// TODO : CHECK USER INPUT, never trust it.
// for example what will hapen if user inputs outputLength <= 0?
// "C" style random generation switch to type from #include <random>
// https://en.cppreference.com/w/cpp/header/random
// srand(time(NULL));
// Use C 's default random engine
// the random device generates a random seed (like time(NULL), but really random )
std::random_device seed;
std::default_random_engine generator(seed());
// a uniform distribution will give you numbers with equal distribution
// something rand() % highestNumber will not achieve
// nice thing is you can specify the range of numbers here and you don't
// have to do any calculations later.
std::uniform_int_distribution distribution(0, highestNumber-1);
// In modern C new/delete are hardly ever needed (unless you need to optimize memory managment)
// the datatype of choice for dynamically sized arrays, arrays for which the length is only
// know at runtime) std::vector is the datatype of choice.
// int* randomNumbers = new int[outputLength];
// initialize a vector with outputLength values and initialize them to 0
std::vector<int> randomNumbers(outputLength, 0);
// In C there are range based for loops, they can not pass beyond the
// end of the array, avoiding the troubles in this bit of code.
// for (int i = 1; i <= outputLength; i ) {
// result = rand() % highestNumber 1;
// cout << result << ", ";
// randomNumbers[i] = result;
// }
// loop over the random numbers, use a reference to values in the array https://www.learncpp.com/cpp-tutorial/for-each-loops/
// so we can change the value (which was initialized to 0)
// https://www.learncpp.com/cpp-tutorial/references/
for (int& value : randomNumbers)
{
value = distribution(generator);
std::cout << value << ", ";
}
// int* numberCounter = new int[highestNumber];
std::vector<int> numberCounter(highestNumber, 0);
std::cout << "\nFrequency:\n";
/*
for (int i = 1; i <= highestNumber; i ) {
int temp2 = randomNumbers[i];
numberCounter[temp2 - 1] = numberCounter[temp2 - 1] 1;
}
*/
// count frequencies no need to do intermediate counts of offsets in you
for (const int value : randomNumbers)
{
numberCounter[value] ;
}
/*
for (int i = 1; i <= highestNumber; i ) {
int frequency = 0;
frequency = numberCounter[i] / outputLength;
cout << numberCounter[i] << " occurs " << frequency << " of the time\n";
}
*/
// when you're going to divide numbers to show fractions
// you will need a floating point type.
// in this case we do use indices since we need values from one array
// to look into values of another array.
double size = static_cast<double>(outputLength);
for (std::size_t n = 0; n < highestNumber; n)
{
double frequency = static_cast<double>(numberCounter[n]) / size;
std::cout << n << " has a chance of " << frequency << " of occuring\n";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/321519.html
上一篇:我正在嘗試將輸入資料列(NumpyArray型別)轉換為不同型別(float和U30)
下一篇:char*vschar[]等等
