C++具有随机计数的数组
C++ array with random counts
我被困在某个地方(随机数有一些问题(,我知道,但找不到修复的地方......
必须使用两个 void 函数:void randomNumbers(int numbers[][3], introwSize(, void randomCounts(int numbers[][3], int size, int counts[](
我无法在.exe文件中放置图像来显示它应该如何并且看起来确实如此,因为我今天刚刚注册......希望这;(有效
预期成果:
//========================
// 7 6 5
// 2 1 1
// 6 7 2
// 9 3 3
// 8 1 1
//========================
//Ran. Number: 0 1 2 3 4 5 6 7 8 9
//Frequency(Counts): 0 4 2 2 0 1 2 2 1 1
我做了什么:
//========================
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// ========================
// Ran. Number: 0 1 2 3 4 5 6 7 8 9
// Frequency(Counts): 001A148D
法典:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int COL = 3;
const int SIZE = 5;
void randomNumbers(int inumbers[][3], int rowSize) {
int num = 0;
for (int i = 0; i < 10; i++) {
num = rand() % 10;
}
}
void randomCounts(int inumbers[][3], int size, int counts[]) {
for (int i = 0; i < 10; i++) {
counts[i]++;
cout << setw(5) << counts[i];
}
}
int main(){
int random[SIZE][COL] = {};
srand((unsigned)time(NULL));
cout << endl;
cout << "==================" << endl;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < COL; j++) {
cout << setw(5) << random[i][j];
if (j == COL - 1) {
cout << endl;
}
}
}
cout << "==================" << endl;
cout << endl;
cout << "Ran. Number: " << setw(5) << "0" << setw(5) << "1" << setw(5) << "2" << setw(5) << "3" << setw(5) << "4" << setw(5) << "5" << setw(5) << "6" << setw(5) << "7" << setw(5) << "8" << setw(5) << "9" << endl;
cout << "Frequency(Counts): " << randomCounts << endl;
return 0;
}
好的,那你为什么要0, 0, 0...
.因为你从来没有真正调用过你的函数。初始化数组:
int random[SIZE][COL] = {};
然后你在这里打印它:
cout << setw(5) << random[i][j];
在这两者之间,您不会在此数组中设置任何内容。当你开始调用你的函数时,你会发现它们不起作用,因为复制输入和执行一些未定义的行为。当您对此进行了更多调试后,请提出一个新问题。
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- C++具有随机计数的数组
- 从C++数组中选择一个随机元素
- 为什么我的 2d 数组的第二行从 RAM 中获取随机值?
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 为什么我的数组值会更新为随机值?
- 在 c++ 的 10x10 数组中生成特定数量的多个随机字符
- SFINAE - 检测类型 T 是指针、数组还是带有随机访问运算符的容器,以及给定的值类型
- 动态数组传递给 main 中用随机值填充的函数,返回值3221226356
- 选择随机字符串数组的排序
- 如何在 3x4 数组中随机 4 个不同的位置
- 字符数组中的十六进制导致写入套接字时出现随机字符
- 更快地访问 C++ 数组中的随机元素
- 通过数组C 随机增加
- C++ 数组随机播放不起作用
- 将多维数组随机化(随机播放)为另一个 2D 数组
- 使用二维数组随机生成器objective c和c++
- 数组随机存取c++
- 多维数组随机漫步程序