C++具有随机计数的数组

C++ array with random counts

本文关键字:数组 随机 C++      更新时间:2023-10-16

我被困在某个地方(随机数有一些问题(,我知道,但找不到修复的地方......

必须使用两个 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];

在这两者之间,您不会在此数组中设置任何内容。当你开始调用你的函数时,你会发现它们不起作用,因为复制输入和执行一些未定义的行为。当您对此进行了更多调试后,请提出一个新问题。