# 2D array help...

• 05-01-2010
mayoussa89
2D array help...
i need to write a program as follows:

Write a program that prompts the user to enter three positive integers representing the number of rows, the number of columns of a grid, and the number of mines hidden in the grid, the program then calls the following function:

void Minesweeper(int nRows, int nColumns, int nMines )

The function Minesweeper( ) prints a grid of nRows x nColumns of 0's and 1's. A 0 represents a square that has no mine and a 1 represents a square that has a mine. The parameter nMines represents the number of mines.

Hint: Store the 0's and 1's in a two dimensional array first then print that array.

i have started it, but i have no idea on where to to go about with this. i will paste my code down below, but i know it wont help much.

i just need someone to point me to the right direction on how to solve it. i know i need to store the values into an array, but i'm a little confused on how to do that with variables? and i assume that my task is to generate the user-defined number of mines into random parts of the array- i dont even know where i would go about trying to figuring that one out.

here's my code:

Code:

```#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; void minesweeper(int numbers_r, int numbers_c, int numbers_m); int main () { int numbers_r, numbers_c, numbers_m; //int mineArray[numbers_r][numbers_c]; cout<<"Enter the number of rows: "; cin>>numbers_r; cout<<"Enter the number of columns: "; cin>>numbers_c; cout<<"Enter the number of mines: "; cin>>numbers_m; minesweeper(numbers_r, numbers_c, numbers_m); return 0; } void minesweeper(int nRows, int nColumns, int nMines) { char mines_chr= '1'; char not_mines_chr= '0'; if (nMines>0) n_not_mines=(nRows*nColumns)-nMines; int mineArray[nRows][nColumns]; for (int i=0; i<nRows; i++) { for(int j=0; j<nColumns; j++) mineArray[i][j]= n_not_mines; cout<<setw(5)<<mineArray[i][j]; } }```
• 05-01-2010
bernt
Quote:

i know i need to store the values into an array, but i'm a little confused on how to do that with variables?
`int mineArray[nRows][nColumns];`
```1) Pick a random spot 2) If there is already a mine there, pick another spot 3) If there isn't, put a mine in that spot 4) Go back to 1```