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:

#include <iostream>
#include <iomanip>
#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: ";
 cout<<"Enter the number of columns: ";
 cout<<"Enter the number of mines: ";
 minesweeper(numbers_r, numbers_c, numbers_m);
 return 0;

void minesweeper(int nRows, int nColumns, int nMines){

for(int i=0; i<nRows; ++i)
	mineArray_ptr[i]=new int[nColumns];

 char mines_chr= '1';
 char not_mines_chr= '0';
 //if (nMines>0)
//	int n_not_mines=(nRows*nColumns)-nMines;
//	 mineArray[i][j]=0;

// int mineArray[sRows][sColumns];
 for (i=0; i<nRows; i++) {
  for(int j=0; j<nColumns; j++)
   if (nMines>0)
	   int n_not_mines=(nRows*nColumns)-nMines;
  // cout<<" test "<<mineArray_ptr[i][j];

its not a real code really, its just a bunch of my thoughts all put together because i really cant get anywhere.
i feel this program is simple and i am over thinking it.
what i do know is that i need to initialize the whole array to zero, and then somehow randomly place the mines, but i don't really know how to.
some examples and explanations of what i have right/wrong and how to solve will be greatly appreciated.
thanks in advance!

6 Years
Discussion Span
Last Post by Lerner

1) get user input for number of rows, number of columns and number of mines
2) declare a 2d array of char called board using dynamic memory and the user provided values for number of rows and number of columns
3) set all the cells of board to '0'
3) select a cell randomly. If that cell has value of '0' change that cell's value to '1', otherwise select another random cell. repeat until a random selected cell has value of '0'
4) Repeat step 3 as many times as number of mines
5) print board.

To randomly select a cell:
1) seed the random number generator once at the top of main).
2) randomly select a number between zero and number of rows doing something like this: rand() % numberOfRows
3) randomly select a number between zero and number of columns
4) use numbers returned in 2 and 3 as indicies to access a cell "randomly".

Edited by Lerner: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.