Hi there everybody, I am currently studying for my university exams and need 2 make a function for the below scenario.
The function must display the information inside the 2D array as you can see in my code I have tried but to no prevail.

Any help will be GREATLY obliged.

Kindest Regards
Curt


Scenario:
The intruder has broken into a university to steal exam question papers, so that he can
sell them to students. Unfortunately the university has a state of the art security system
which the thief has to contend with, namely robots. The robots and intruder are placed in
a two dimensional area, that can vary in size. If a robot moves into the same square as an
intruder, then the intruder is caught. If two robots move into the same square, they collide
and explode. The intruder moves one square at a time north, south, east, west or diagonally.
There are also a few bushes in the area. If a robot moves into the square of a bush, then it
explodes, and the bush is burnt to the ground. The area is fenced in, so neither the robots,
nor the intruder can escape. The intruder cannot move into a square with a bush in it. There
is a function moverobots provided for you to allow the robots to move around. Look at the
header file robots.h to see how to do this.
You must use a two dimensional dynamic array to solve the problem. The array is represented
as follows:
• 0: Open square
• 1: Robot
• 2: Intruder
• 3: Explosion
• 4: Bush
Write a function to draw the area on the screen using the array representation. Store the row
and column, that the intruder is in so that you can move the intruder from that position. Now
write code to move the intruder that the robots wish to catch, manually, using the keyboard.
The intruder begins at a random position, and so do the robots. Bushes are also placed
randomly. The user must be able to specify the number of bushes and robots. You must
support an arbitrary area size. The simulation ends when the intruder is caught or all the
robots are destroyed. Your program must detect this.
If the intruder is caught, then he will be confined until the exams are over and then handed
over to the authorities. If all the robots are destroyed, then the intruder has enough time to
cut a hole in the fence and escape with the exam question papers.
Additional, separate to main program: The intruder has one bomb that explodes after a
number of moves specified by the intruder. Allow the user to plant the bomb which will later
explode and destroy everything within a radius of 5 blocks of the bomb. The bomb is not
visible.

]#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
int nrows, ncols,i,j,a,b,numbushes,numrobots,rand1,rand2,rand3,rand4,rand5,rand6,prow,pcol;
char move;
int DISPLAYARRAY(int,int);
void PLAYERMOVEMENT();
int field[0][0];

int main()
{

    cout << "\n" << endl;
    cout << "                        Operation Robots and Test Papers                      " << endl;
    cout << "                        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=                      " << endl;
    cout << "\n" << endl;
    cout << "Please input the number of ROWS? " << endl;
    cin >> nrows;

    cout << "Please input the number of COLUMS? " << endl;
    cin >> ncols;
    cout << "\n" << endl;

    field[nrows][ncols];

    for(i = 0; i > nrows; i++)
    {
        for(j = 0; j> ncols; j ++)
        {
            field[i][j] = 0;
        }
    }

    srand((unsigned)time(0));


        cout << "Please enter the number of BUSHES to place on grid:";
    cin >> numbushes;
    cout << endl;

    cout << "Please enter the number of ROBOTS to place on grid:";
    cin >> numrobots;
    cout << endl;
    cout << "\n" << endl;
//////////////////////////////////////////////////////////////////

        for (a = 0; a < numbushes; a++)
        {
            rand2 = 0;
            rand2 = (rand()%ncols) + 1;

            if(field[j][rand2] == 0)
            field[j][rand2] = 4;
        }
/////////////////////////////////////////////////////////////////

        for (b = 0; b < numrobots; b++)
        {
            rand3 = 0;
            rand3 = (rand()%nrows) + 1;

            if(field[rand3][j] == 0)
            field[rand3][j] = 2;

            else if((field[rand3][j] == 4) && (rand3 >= nrows))
            {
                field[rand3 - 1][j] = 2;
            }
        }

////////////////////////////////////////////////////////////////Player
//
         for (j = 0; j < 1; j++)
        {
            rand6 = 0;
            rand5 = 0;
            rand5 = (rand()%ncols)+1;
            rand6 = (rand()%nrows)+1;
            if (field[rand6][rand5] == 0)
            {
                field[rand6][rand5] = 1;
                break;
            }

        }

            prow = rand6;
            pcol = rand5;

        DISPLAYARRAY(nrows,ncols);

    return 0;
}
int  **DISPLAYARRAY()
{
    int **showArray[nrows][ncols];
    //////////////////////////////////////////////////////////////////LABELING THE GRID
    for (i = 0; i < 1; i++)
    {
        cout << "  ";
        for (j = 0; j < ncols; j++)
        {
            cout << j << "|";
        }
        cout << endl;
    }
    cout << endl;

    for (i = 0; i < nrows; i++)
    {
        {
            cout << i << "|";
        }
        for (j = 0; j < ncols; j++)
            **showArray[i][j] = field[i][j];

            cout << showArray[i][j] << "|";
            cout << endl;

    }
    cout << endl;

//    return showArray;

}

What is the problem? Can you simplify the code into a <20 line demonstration of the problem?

I don't see the word "new" or the word "malloc" or the word "calloc" anywhere in your code, so it doesn't look like you are creating a new array anywhere that won't go out of scope once it leaves the function. That's one problem, even if you had set the array up correctly. However, the array is not set up correctly:

int **showArray[nrows][ncols];

There is no need for those first two asterisks. That's where the error is coming from, I would imagine. You have too many levels of indirection (pointer to a pointer to a pointer to a pointer rather than a pointer to a pointer, which the function expects to return).

You must use a two dimensional dynamic array to solve the problem.

That means "new", "malloc", or "calloc" has to be in your program. This is C++, so use "new".

int numRows = 6;
int numCols = 8;
int** array = new int*[numRows];
for (int i = 0; i < numRows; i++)
    array[i] = new int[numCols];

Then if you want to return the 2-D array from your function:

return array;

Thank you so much I understand completely now :)
Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.