I need help passing a 2D array

closed account (EAp4z8AR)
I have this 2D array but how do I pass it into a function, I know I can use pointers but I don't know how to use them yet.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    srand((unsigned) time(0));
    const int SIZE_ROW = 10, SIZE_COLUMN = 10, NUM_X = 50;
    char array[SIZE_ROW][SIZE_COLUMN];
    
    fill(&array[0][0], &array[SIZE_ROW][0], ' ');
    
    for (int i=0; i<NUM_X; i++)
    {
        int row, col;
        do
        {
            row = rand()%SIZE_ROW;
            col = rand()%SIZE_COLUMN;
        }
        while (array[row][col] == 'x');
        
        array[row][col] = 'x';
        
    }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

const int SIZE_ROW = 10, SIZE_COLUMN = 10, NUM_X = SIZE_ROW * SIZE_COLUMN / 2;

void init(char grid[][SIZE_COLUMN])
{
    fill(&grid[0][0], &grid[SIZE_ROW][0], ' ');
    
    for (int i = 0; i < NUM_X; i++)
    {
        int row, col;
        do
        {
            row = rand()%SIZE_ROW;
            col = rand()%SIZE_COLUMN;
        }
        while (grid[row][col] == 'x');
        
        grid[row][col] = 'x';
    }
}

void print(char grid[][SIZE_COLUMN])
{
    for (int r = 0; r < SIZE_ROW; r++)
    {
        for (int c = 0; c < SIZE_COLUMN; c++)
            cout << grid[r][c];
        cout << '\n';
    }
}

int main()
{
    srand(time(0));
    char grid[SIZE_ROW][SIZE_COLUMN];
    init(grid);
    print(grid);
}

Last edited on
Topic archived. No new replies allowed.