如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
How do I create a 2d array in C++ that contains the counter of the loop in one part and a list of numbers in the other part?
我正在尝试计算随机生成的数字在 3 种不同情况下的出现次数:10、1,000 和 100,000 次迭代。在每次迭代中,我想计算数字 1 到 6 的出现次数,然后比较这些计数。
这是我的实现想法:有一个嵌套的for
循环,其中外部循环将从案例 1 迭代到案例 3,内部for
循环将为每个案例生成一个随机数,计算发生频率并将其放入数组中。
当我退出内部fo
r 循环时,我想获取该数组并将其放入另一个 2d 数组中,以便保存每种情况的值。
这是我到目前为止的C++代码:
extern "C"{
#include <stdlib.h>
#include <time.h>
}
#include <iostream>
using namespace std;
int genRand(){
int min = 1;
int max = 6;
int randNum = min + rand() / (RAND_MAX / (max - min + 1) + 1);
return randNum;
}
int main(){
//Seeds the random number generator(takes the current time and uses it to randomize)
srand((unsigned int)time(NULL));
//Part 1 - Roll a single die 10, 1000, and 100 000 times
//First part holds the number of cases and the second hold the number of occurances
int randArr[3][6] = {0};
int countArr[6] = {0};
int mult = 1; int randNum = 0;
for(int i = 0; i<3; i++){
for(int j = 0; j<10*mult; j++){
randNum = genRand();
for(int k = 1; k<=6; k++){
if(randNum == k) { countArr[k-1]++;}
}
}
mult *= 100;
randArr[i][i] = i, countArr[i];
}
}
我相信我的最后一行是错误的,但我不明白为什么或如何制作一个 2d 数组,使得第一部分是案例编号(在这种情况下是 i(,然后第二个输入将是我之前创建的数组出现频率。
有人可以帮助指出我做错了什么吗?
您可以大大简化代码以降低此问题的复杂性。
实现此目的的最简单成语是使用地图。std::map
使用树结构来保存数据,std::unordered_map
使用哈希表。后者(通常(更快,但前者需要较少的打字。两者都可以很好地解决此问题。
另一个主要变化是,您通常不应使用rand()
或其相关实用程序来生成随机数。在 C++11 及更高版本中使用<random>
库。如果您确实打算使用rand()
,因为您明确测试了它的统计随机性,那很好,但如果这是您需要/期望的,我会包含更改后的代码以"正确"生成随机数。如有必要,将其替换为旧代码。
#include <iostream>
#include <random>
#include <map>
#include<array>
int genRand(){
//Generate Random numbers with an engine
static std::mt19337 engine(std::random_device{}());
//Easy-to-configure distribution algorithm
static std::uniform_int_distribution<int> distribution(1, 6);
//Pass the engine into the distribution and it will correctly give you the result you want
return distribution(engine);
}
int main(){
int mult = 10;
for(int i = 0; i<3; i++){
//Construct the map
std::map<int, int> map;
for(int j = 0; j < mult; j++) {
//Generate the number
int randNum = genRand();
//The brackets operator does a lot of work: if the entry already exists, it
//simply returns a reference to it. If it does not, it creates a new entry,
//value-initializes it (sets it to 0), and then returns a reference.
//We then increment this reference.
++map[randNum];
}
std::cout << "Results for " << mult << " iterations:" << std::endl;
for(auto const& result : map) {
//We can iterate over the results. std::map will keep the results sorted
//automatically, because it uses the natural ordering of the key
std::cout << " " << "Number: " << result.first << "; Occurrences: " << result.second << std::endl;
}
mult *= 100;
}
}
此程序的可能输出:
Results for 10 iterations:
Number: 1; Occurrences: 2
Number: 2; Occurrences: 2
Number: 3; Occurrences: 1
Number: 4; Occurrences: 1
Number: 5; Occurrences: 1
Number: 6; Occurrences: 3
Results for 1000 iterations:
Number: 1; Occurrences: 154
Number: 2; Occurrences: 184
Number: 3; Occurrences: 179
Number: 4; Occurrences: 162
Number: 5; Occurrences: 172
Number: 6; Occurrences: 149
Results for 100000 iterations:
Number: 1; Occurrences: 16811
Number: 2; Occurrences: 16666
Number: 3; Occurrences: 16534
Number: 4; Occurrences: 16815
Number: 5; Occurrences: 16577
Number: 6; Occurrences: 16597
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- 是否需要删除包含对象的"pair"?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在h文件中包含.o对象文件
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- Eclipse-cdt boost shared_ptr.hpp:目录中没有此类文件,但它的一部分是包含文件
- for循环不包括需要包含数组的一部分
- 使库名称成为 make 中包含路径的一部分
- CMake:如何只包含OpenCV的一部分
- 在编译时创建一个标识符,其中包含行号作为标识符的一部分
- 当一个字符串包含另一个字符串的一部分时比较字符串
- 函数在作为 GLUT 项目的一部分包含时不读取文件
- 如何找出哪些包含的文件不是当前VC++解决方案的一部分
- 如何通过C++预处理器 #include 包含文件的一部分