1

Topic: To find an error. The sapper

2

Re: To find an error. The sapper

GAWAIN;
Understood that is primary all cells have not been filled, and there is nothing was to address. Transferred, all the same does not work

\#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
int main ()
{
int star;//a variable for determination of an amount of mines nearby
int k1, k2, k;//k1 - row number with a mine, k2 - number of a pole with a mine, k-quantity of mines
int n, m;//n-quantity of lines, m-quantity of columns
cin>> n>> m>> k;
char ** dinArr = new char * [n];
for (int i = 0; i <n; i ++) dinArr [i] = new char [m];//matrix creation
for (int i=0; i <n; i ++)
{
for (int z=0; z <m; z ++)
{
dinArr [i] [z] = '. ';
}
}
if (k == 0)
{
for (int i = 0; i <n; i ++)//filling of an array with characters
{
for (int z = 0; z <m; z ++)
{
cout <<dinArr [i] [z];
}
cout <<endl;
}
return 0;
}
for (int i = 0; i <k; i ++)
{
cin>> k1>> k2;//instructions on mine "address"
dinArr [k1 - 1] [k2 - 1] = ' * ';//-1 since on a condition n and m begin with 1, and at us with 0
}
for (int i = 0; i <n; i ++)//check on  stars nearby
{
for (int z = 0; z <m; z ++)
{
star = 0;
if (i == 0 && z == 0)//For left upper "adjacent" cells
{
if (dinArr [i] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z] == ' * ') star ++;
}
else if (z! = 0 && i == 0&&z!=m)//for a zero line not a zero column
{
if (dinArr [i] [z - 1] == ' * ') star ++;
if (dinArr [i + 1] [z - 1] == ' * ') star ++;
if (dinArr [i + 1] [z] == ' * ') star ++;
if (dinArr [i] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z + 1] == ' * ') star ++;
}
else if (z == 0 && i! = 0&&i!=n)//for a zero column not zero 
{
if (dinArr [i - 1] [z] == ' * ') star ++;
if (dinArr [i - 1] [z + 1] == ' * ') star ++;
if (dinArr [i] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z] == ' * ') star ++;
}
else if (i==n&&z==0)//for the right left corner
{
if (dinArr [i-1] [z] == ' * ') star ++;
if (dinArr [i-1] [z+1] == ' * ') star ++;
if (dinArr [i] [z+1 == ' * ']) star ++;
}
else if (i==0&&z==m)//for the first cell m a pole
{
if (dinArr [i] [z-1] == ' * ') star ++;
if (dinArr [i+1] [z-1] == ' * ') star ++;
if (dinArr [i+1] [z] == ' * ') star ++;
}
else if (i==n&&z!=0&&z!=m)//for all last line except the first and last pole
{
if (dinArr [i] [z-1] == ' * ') star ++;
if (dinArr [i-1] [z-1] == ' * ') star ++;
if (dinArr [i-1] [z] == ' * ') star ++;
if (dinArr [i+1] [z+1] == ' * ') star ++;
if (dinArr [i] [z+1] == ' * ') star ++;
}
else if (z==m&&i!=0&&i!=n)//for all last pole, except the first and last cell
{
if (dinArr [i-1] [z] == ' * ') star ++;
if (dinArr [i-1] [z-1] == ' * ') star ++;
if (dinArr [i] [z-1] == ' * ') star ++;
if (dinArr [i+1] [z-1] == ' * ') star ++;
if (dinArr [i+1] [z+1] == ' * ') star ++;
}
else if (i==n&&z==m)//for the left lower cell
{
if (dinArr [i] [z-1] == ' * ') star ++;
if (dinArr [i-1] [z-1] == ' * ') star ++;
if (dinArr [i-1] [z] == ' * ') star ++;
}
else//all remaining
{
if (dinArr [i - 1] [z - 1] == ' * ') star ++;
if (dinArr [i - 1] [z] == ' * ') star ++;
if (dinArr [i - 1] [z + 1] == ' * ') star ++;
if (dinArr [i] [z - 1] == ' * ') star ++;
if (dinArr [i] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z - 1] == ' * ') star ++;
if (dinArr [i + 1] [z] == ' * ') star ++;
if (dinArr [i + 1] [z + 1] == ' * ') star ++;
}
dinArr [i] [z] = (star + 48);//it is writeable in a cell an amount of stars nearby
if (star == 0) dinArr [i] [z] = '. ';//if stars 0,  a point
}
}
for (int i = 0; i <n; i ++)//an output
{
for (int z = 0; z <m; z ++)
{
cout <<dinArr [i] [z];
}
cout <<endl;
}
return 0;
}

3

Re: To find an error. The sapper

In similar  (in handling of matrixes)

if (dinArr [i - 1] [z - 1] == ' * ') star ++;
if (dinArr [i - 1] [z] == ' * ') star ++;
if (dinArr [i - 1] [z + 1] == ' * ') star ++;
if (dinArr [i] [z - 1] == ' * ') star ++;
if (dinArr [i] [z + 1] == ' * ') star ++;
if (dinArr [i + 1] [z - 1] == ' * ') star ++;
if (dinArr [i + 1] [z] == ' * ') star ++;
if (dinArr [i + 1] [z + 1] == ' * ') star ++;

I normally entered 8 variables which showed a state of adjacent cells.
Well... As a last resort it is possible macroes. It is less than easier letters.
For matrixes of very big sizes (pictures more than 2k in the size) are
And other optimization if to pass from a two-dimensional array to the one-dimensional.
P.S.  I on a habit do code-review.
By the way strongly I advise to practise TDD. It is useful for itself and for our forum.