如何将50行50列的二维数组编码为一个函数,该函数将一个星号随机分配给一个元素? [英] How can I code a two-dimensional array of 50 rows and 50 columns into a function which randomly assigns an asterisk to an element?
问题描述
我有一个CS课,我正在学习C ++.对于此分配,我必须编写一个可以传递给函数的二维字符数组.该数组必须包含50行和50列.所有元素都必须初始化为空格('').
I have an assignment for a CS class where I'm learning C++. For this assignment, I have to write a two dimensional character array which can be passed to a function. The array has to consist of 50 rows and 50 columns. All elements must be initialized to a space (' ').
我在这里创建了数组,我还编写了一个for循环以将数组放置在网格中.现在,我必须将星号随机分配给数组的元素,这些元素仍然留为空白,我不知道该怎么做.
I have creating the array here I have also written a for loop to place the array in a grid. Now, I have to assign asterisks randomly to elements of the array, which are still left blank, and I cannot figure out how to do so.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()
{
const int rows = 50; // Variables
const int col = 50;
const char SIZE = ' ';
const int hgt = 48;
int X = rand() % 50; // *Edited: This code was copied from an older save
int y = rand() % 50;
char board[rows][col]; // Array initialization
int i;
int j;
int x;
srand((unsigned)time(0));
for (i = 0; i < rows; i++) // For loop to place array in grid.
{
for (j = 0; j < col; j++)
{
board[i][j] = SIZE;
}
board[x][y] = '*'
}
cout << setfill('-') << setw(50) << "" << endl; // Grid
for (X = 0; X < hgt; X++)
{
cout << "|" << setfill(' ') << setw(49) << "|" << endl;
}
cout << setfill('-') << setw(50) << "" << endl;
cin.ignore();
cout << "Press Enter to continue..." << endl;
cin.ignore();
return 0;
}
数组起作用,网格起作用.我只是不知道如何分配随机放置在网格中的星号,以及如何将该数组传递给函数.
The array works, the grid works. I just cannot figure out how to assign asterisks randomly placed in the grid and how to pass that array into a function.
推荐答案
这可能是对c ++代码样式的理解.避免使用c样式数组,并使用 std :: array
.不要使用 std :: rand
,而是使用< random>
.使用stl容器和算法.使用基于范围的for循环,而不是经典的for循环.尽可能使用 const
.
This is may understanding of a c++ code style. Avoid c style arrays and use std::array
. Don't use std::rand
but use <random>
. Use the stl containers and algorithms. Use range based for loops instead of classic for loops. Use const
whenever you can.
#include <algorithm>
#include <array>
#include <iostream>
#include <random>
using Board = std::array<std::array<char, 50>, 50>;
void randomFill(Board &board, std::size_t count) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> disX(0, board.size());
std::uniform_int_distribution<> disY(0, board[0].size());
while (count > 0) {
const std::size_t x = disX(gen);
const std::size_t y = disY(gen);
if (board[y][x] == '*') continue;
board[y][x] = '*';
--count;
}
}
int main() {
Board board;
for (auto &row : board) {
std::fill(std::begin(row), std::end(row), ' ');
}
randomFill(board, 50);
for (const auto &row : board) {
for (const auto &field : row) {
std::cout << field << ' ';
}
std::cout << '\n';
}
return 0;
}
这篇关于如何将50行50列的二维数组编码为一个函数,该函数将一个星号随机分配给一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!