Thread: 2D array help...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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];
    }
    }

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    i know i need to store the values into an array, but i'm a little confused on how to do that with variables?
    Let me answer that with your own source code:
    Code:
    int mineArray[nRows][nColumns];
    This will work with a modern compiler.

    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.
    Use rand(). Think about how you would put mines in a field with pencil and paper.
    Code:
    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
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate space for 2d array
    By jtay in forum C Programming
    Replies: 7
    Last Post: 04-25-2010, 10:34 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM