Concerning
how to pass that array into a function
An array is a special case concerning function parameters:
void f(int a[10]);
isn't a function with a value parameter of int[10]
as it looks like. Arrays are not passed by value – they decay to a pointer to first element. Hence, the above function declaration is the same as
void g(int *a); // the same as g(int a[]);
If the array is a 2d array (an array of arrays), this doesn't change:
void f(int a[10][3]);
is the same as:
void g(int (*a)[3]); // the same as g(int a[][3]);
The mix of pointer and dimension makes things a bit complicated: The parentheses around *a
are absolutely necessary because
void h(int *a[3]); // the same as void h(int *a[]); or void h(int **a);
would have a pointer to pointer(s) as argument what is something completely different.
However, there is a very simple trick to come around all theses issues:
Using a typedef
:
typedef char Board[10][3];
or using using
(more modern):
using Board = char[10][3];
Now, things become very easy:
void f(Board &board); // passing array by reference
which could be also written as:
void f(char (&board)[10][3]);
but the latter might look a bit scaring.
Btw. passing the array by reference prevents that the array type decays to a pointer type like demonstrated in the following small sample:
#include <iostream>
void f(char a[20])
{
std::cout << "sizeof a in f(): " << sizeof a << '
';
std::cout << "sizeof a == sizeof(char*)? "
<< (sizeof a == sizeof(char*) ? "yes" : "no")
<< '
';
}
void g(char (&a)[20])
{
std::cout << "sizeof a in g(): " << sizeof a << '
';
}
int main()
{
char a[20];
std::cout << "sizeof a in main(): " << sizeof a << '
';
f(a);
g(a);
}
Output:
sizeof a in main(): 20
sizeof a in f(): 8
sizeof a == sizeof(char*)? yes
sizeof a in g(): 20
Live Demo on coliru
Concerning
I just cannot figure out how to assign asterisks randomly placed in the grid
I cannot say it shorter than molbdilno:
You need to do it repeatedly.
For a demonstration, I re-designed OPs code a bit:
#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) << "" << "+
";
for (int y = 0; y < Rows; ++y) {
std::cout << '|';
for (int x = 0; x < Cols; ++x) std::cout << board[y][x];
std::cout << "|
";
}
std::cout << '+' << std::setfill('-') << std::setw(Cols) << "" << "+
";
}
int main()
{
srand((unsigned)time(0));
Board board;
fillGrid(board, ' ');
std::cout << "Clean grid:
";
printGrid(board);
std::cout << '
';
populateGrid(board, 10, '*');
std::cout << "Initialized grid:
";
printGrid(board);
std::cout << '
';
}
Output:
Clean grid:
+----------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+----------+
Initialized grid:
+----------+
| |
| * |
|** * * |
| * |
| |
| |
|* |
| |
| * |
| * * |
+----------+
Live Demo on coliru