如何在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?

本文关键字:一部分 包含 计数器 循环 另一部 数字 列表 2d 创建 数组 C++      更新时间:2023-10-16

我正在尝试计算随机生成的数字在 3 种不同情况下的出现次数:10、1,000 和 100,000 次迭代。在每次迭代中,我想计算数字 1 到 6 的出现次数,然后比较这些计数。

这是我的实现想法:有一个嵌套的for循环,其中外部循环将从案例 1 迭代到案例 3,内部for循环将为每个案例生成一个随机数,计算发生频率并将其放入数组中。

当我退出内部for 循环时,我想获取该数组并将其放入另一个 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