c++随机生成二维数组
时间: 2023-11-30 21:40:52 浏览: 92
C++随机数的生成
5星 · 资源好评率100%
以下是使用C++随机生成二维数组的示例代码:
```c++
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int** generateRandomArray(int n, int m) {
int** arr = new int*[n];
srand((unsigned)time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = new int[m];
for (int j = 0; j < m; j++) {
arr[i][j] = (rand() % 900) + 100;
}
}
return arr;
}
void printArray(int** arr, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
int n, m;
cin >> n >> m;
int** arr = generateRandomArray(n, m);
printArray(arr, n, m);
return 0;
}
```
阅读全文