C++随机生成一个5*5的二维数组
时间: 2023-12-31 09:04:43 浏览: 196
下面是使用C++标准库的随机数生成器生成一个5*5的二维数组的示例代码:
```c++
#include <iostream>
#include <random>
const int ROWS = 5;
const int COLS = 5;
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 100);
int arr[ROWS][COLS];
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
arr[i][j] = distr(gen);
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
```
这个程序使用了C++11标准库中的随机数生成器`std::mt19937`和均匀分布器`std::uniform_int_distribution`。在每个数组元素中生成一个1到100之间的随机整数。
相关问题
c++随机生成一个5*5的二维数组并求最大值,最小值,平均数和主对角线上的元素之和
在C++中,我们可以使用`<iostream>`、`<vector>`、 `<cstdlib>`和 `<ctime>`头文件来创建一个5x5的二维数组,并通过`std::rand()`和`std::srand()`函数生成随机数。然后计算最大值、最小值、平均数以及主对角线上的元素之和。以下是简单的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
int main() {
// 初始化随机数种子
std::srand(std::time(0));
int size = 5;
std::vector<std::vector<int>> matrix(size, std::vector<int>(size));
// 遍历矩阵,生成并填充随机数
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
matrix[i][j] = rand() % 100; // 生成0到99之间的随机整数
}
}
// 计算最大值、最小值和平均数
int max_value = *std::max_element(matrix.begin(), matrix.end());
int min_value = *std::min_element(matrix.begin(), matrix.end());
double sum = 0.0;
for (int i = 0; i < size; ++i) {
sum += matrix[i][i];
}
double average = sum / static_cast<double>(size * size);
// 输出结果
std::cout << "最大值: " << max_value << "\n";
std::cout << "最小值: " << min_value << "\n";
std::cout << "主对角线上元素之和: " << sum << "\n";
std::cout << "平均数: " << average << "\n";
return 0;
}
```
c++随机生成二维数组
以下是使用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;
}
```
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)