Generate X in 2D board

I managed to create a 2D board containing random numbers. My task now is how could I mark "X" in that board once any number in the board is selected by entering coordinate of x, y.

For example :

Before :
1
2
3
4
5
6
|   8   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  24    |
 ------------------------


After :
1
2
3
4
5
6
7
|   8   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  X    |
 ------------------------
Number selected = 24


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
  void PrintGrid(int& BoxSize){

    cout << setw(12);
    for (int z = 1; z <= BoxSize; z++) {
            cout << z << setw(7);
    }
    cout << "\n";
   for (int y = 1; y <= BoxSize; y++) {

        if(BoxSize == 2)
        cout << "        --------------" << endl;
        else if(BoxSize == 3)
        cout << "        ---------------------" << endl;
        else if(BoxSize == 4)
        cout << "        ----------------------------" << endl;
        else if(BoxSize == 5)
        cout << "        -----------------------------------" << endl;
        else if(BoxSize == 6)
        cout << "        ------------------------------------------" << endl;
        else if(BoxSize == 7)
        cout << "        -------------------------------------------------" << endl;
        else if(BoxSize == 8)
        cout << "        --------------------------------------------------------" << endl;
        cout << setw(3) << "Row " << y;
     for (int x = 1; x <= BoxSize; x++) {

//        gridRand[x][y] = rand() % 61 + -30;
        cout << setw(3) << "|" << setw(4) << gridRand[x][y] << setw(4);
        }
        cout << "|" << endl;
    }
        if(BoxSize == 2)
        cout << "        --------------" << endl;
        else if(BoxSize == 3)
        cout << "        ---------------------" << endl;
        else if(BoxSize == 4)
        cout << "        ----------------------------" << endl;
        else if(BoxSize == 5)
        cout << "        -----------------------------------" << endl;
        else if(BoxSize == 6)
        cout << "        ------------------------------------------" << endl;
        else if(BoxSize == 7)
        cout << "        -------------------------------------------------" << endl;
        else if(BoxSize == 8)
        cout << "        --------------------------------------------------------" << endl;
}


Any suggestion guys?...
I see in your commented line that you go from -30 to 30. When you select the desired number, put a value outside the range. Then replace line 28 with

1
2
3
4
5
6
7
8
if (gridRand[x][y]>MaxRange)
{
   cout << setw(3) << "|" << setw(4) << "X" << setw(4);
}
else
{
   cout << setw(3) << "|" << setw(4) << gridRand[x][y] << setw(4);
}

The other thing I would suggest, replace the multiple if / else if statements by a for loop
1
2
3
4
5
cout<<"        ";
for(int i=1;i<=BoxSize;i++)
    cout<<"-------";
cout<<endl;
    


And, by the way, you want to pass gridRand as an argument to this function
Last edited on
Dear ats15,

When you select the desired number, put a value outside the range. Then replace line 28 with


1
2
3
4
5
6
7
8
if (gridRand[x][y]>MaxRange)
{
   cout << setw(3) << "|" << setw(4) << "X" << setw(4);
}
else
{
   cout << setw(3) << "|" << setw(4) << gridRand[x][y] << setw(4);
}


Thanks for the 'for loop' suggestion. It saved me quite a line. But I don't quite understand the above suggestion. Okay, lets assume I enter x = 4 and y = 5 the program will output the gridRand[4][5] as a result right. So, how MaxRange is playing its role here?

Topic archived. No new replies allowed.