Passing 2D Array from one function to another?
Apr 12, 2018 at 5:25pm UTC
So I'm trying to create a minesweeper game (but with gophers), and I have a function that creates a table and then another function that prints the table. The problem is, once I create the table, I don't know how to get the array to the other function to print the array. Is there a way I can return it? Thanks!
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
* Assignment 09
* 04/12/18
* Section 02
*/
#include <iostream>
#include <time.h>
using namespace std;
//-----Global Variables-----//
const int ROWS = 21;
const int COLS = 21;
//-----Function Prototypes-----//
void rules();
void random_gopher();
void create_field();
void print_field(char arr[][COLS]);
void game();
//-----MAIN FUNCTION-----//
int main()
{
rules();
create_field();
print_field(field);
return 0;
}
//-----Other Functions-----//
void rules()
{
cout << "Welcome to gopher hunt!\n\nTo play, enter an X value to pick a row and a Y value to pick a column.\n\n" ;
}
void create_field()
{
cout << "Generating field...\n\n" ;
char field[ROWS][COLS];
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
field[i][j] = '+' ;
}
}
}
void print_field(char field[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << field[i][j];
}
}
}
Apr 12, 2018 at 7:08pm UTC
The game field should be a public object. For your purposes, that probably means that it should be a global.
Topic archived. No new replies allowed.