c++ - 如何将 50 行和 50 列的二维数组编码为一个随机为元素分配星号的函数?
问题描述
我有一个正在学习 C++ 的 CS 课程的作业。对于这个任务,我必须编写一个可以传递给函数的二维字符数组。该数组必须由 50 行和 50 列组成。所有元素都必须初始化为空格 (' ')。
我在这里创建了数组我还编写了一个 for 循环来将数组放在网格中。现在,我必须将星号随机分配给数组的元素,这些元素仍然是空白的,我不知道该怎么做。
#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;
}
阵列有效,网格有效。我只是不知道如何分配随机放置在网格中的星号以及如何将该数组传递给函数。
解决方案
关于
如何将该数组传递给函数
数组是关于函数参数的一种特殊情况:
void f(int a[10]);
不是一个int[10]
看起来像值参数的函数。数组不是按值传递的——它们衰减为指向第一个元素的指针。因此,上述函数声明与
void g(int *a); // the same as g(int a[]);
如果数组是二维数组(数组的数组),则不会改变:
void f(int a[10][3]);
是相同的:
void g(int (*a)[3]); // the same as g(int a[][3]);
指针和维度的混合使事情变得有点复杂:括号*a
是绝对必要的,因为
void h(int *a[3]); // the same as void h(int *a[]); or void h(int **a);
会有一个指向指针的指针作为参数,这是完全不同的东西。
但是,有一个非常简单的技巧可以解决所有这些问题:
使用typedef
:
typedef char Board[10][3];
或使用using
(更现代):
using Board = char[10][3];
现在,事情变得非常简单:
void f(Board &board); // passing array by reference
也可以写成:
void f(char (&board)[10][3]);
但后者可能看起来有点吓人。
顺便提一句。通过引用传递数组可防止数组类型衰减为指针类型,如下面的小示例所示:
#include <iostream>
void f(char a[20])
{
std::cout << "sizeof a in f(): " << sizeof a << '\n';
std::cout << "sizeof a == sizeof(char*)? "
<< (sizeof a == sizeof(char*) ? "yes" : "no")
<< '\n';
}
void g(char (&a)[20])
{
std::cout << "sizeof a in g(): " << sizeof a << '\n';
}
int main()
{
char a[20];
std::cout << "sizeof a in main(): " << sizeof a << '\n';
f(a);
g(a);
}
输出:
sizeof a in main(): 20
sizeof a in f(): 8
sizeof a == sizeof(char*)? yes
sizeof a in g(): 20
关于
我只是不知道如何分配随机放置在网格中的星号
我不能说它比molbdilno短:
你需要反复做。
为了演示,我重新设计了 OP 代码:
#include <iomanip>
#include <iostream>
const int Rows = 10; //50;
const int Cols = 10; //50;
// for convenience
//typedef char Board[Rows][Cols];
using Board = char[Rows][Cols];
void fillGrid(Board &board, char c)
{
for (int y = 0; y < Rows; ++y) {
for (int x = 0; x < Cols; ++x) board[y][x] = c;
}
}
void populateGrid(Board &board, int n, char c)
{
while (n) {
const int x = rand() % Cols;
const int y = rand() % Rows;
if (board[y][x] == c) continue; // accidental duplicate
board[y][x] = c;
--n;
}
}
void printGrid(const Board &board)
{
std::cout << '+' << std::setfill('-') << std::setw(Cols) << "" << "+\n";
for (int y = 0; y < Rows; ++y) {
std::cout << '|';
for (int x = 0; x < Cols; ++x) std::cout << board[y][x];
std::cout << "|\n";
}
std::cout << '+' << std::setfill('-') << std::setw(Cols) << "" << "+\n";
}
int main()
{
srand((unsigned)time(0));
Board board;
fillGrid(board, ' ');
std::cout << "Clean grid:\n";
printGrid(board);
std::cout << '\n';
populateGrid(board, 10, '*');
std::cout << "Initialized grid:\n";
printGrid(board);
std::cout << '\n';
}
输出:
Clean grid:
+----------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+----------+
Initialized grid:
+----------+
| |
| * |
|** * * |
| * |
| |
| |
|* |
| |
| * |
| * * |
+----------+
推荐阅读
- python - 算法交易代码中的 except 子句出错
- javascript - How to combine two array, but take care for no same elements
- android - 使用 Flutter 制作应用程序时如何通过 WiFi 将设备与 ADB 连接?
- matlab - Image Processing MLP - Detecting Classes
- html - 自动刷新网页视图
- c# - 有什么方法可以在.Net 中创建只需要访问一个键的多键字典?
- android - 按下电源按钮超过 10 秒时设备不重启
- netty - Spring Webflux / Reactor 发布调用线程
- java - 枚举类型的 Java 编译错误“公共类型 EngineType 必须在其自己的文件 Car.java 中定义”
- dart - Dart:一次创建多个对象