title: MPI calculate difference sign from double array
body: <p>I'm trying to calculate difference sign from double array with MPI, no need to use outside rows or cols, the result may look like that:</p>
  <p>In array</p>
  <blockquote>
  <p>1 0 1 1 1</p>
  <p>2 1 3 1 4</p>
  <p>1 3 1 0 1</p>
  <p>2 1 -1 1 2</p>
  </blockquote>
  <p>out array</p>
  <blockquote>
  <p>0 0 0 0 0</p>
  <p>0 -1 0 -1 0</p>
  <p>0 1 -1 1 0</p>
  <p>0 0 0 0 0</p>
  </blockquote>
  <p>I wrote some code but when I'm trying to write b array to the console I'm getting Access Violation Reading Location error. I'm new in MPI and I don't know if this code is correct. Any ideas how to get it work?</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;chrono&gt;
  #include &lt;mpi.h&gt;
  #include &lt;iomanip&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  int main(int argc, char *argv[])
  {
  
  int size, rank;
  MPI_Init(&amp;argc, &amp;argv);
  MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
  MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
  
  srand(time(NULL));
  
  int R = 4;
  int C = 8;
  
  
  int **a = new int *[R];
  a = new int *[R]; 
  a[0] = new int[R*C];
  for (int i = 1; i &lt; R; i++)
  {
      a[i] = &amp;a[0][i*C];
  }
  
  if (rank == 0) {
      for (int i = 0; i &lt; R; i++)
      {
          for (int j = 0; j &lt; C; j++) 
          {
              a[i][j] = (rand() % 10) - 5;
              cout &lt;&lt; setw(5) &lt;&lt; a[i][j] &lt;&lt; &quot; &quot;;
          }
          cout &lt;&lt; endl;
      }
  
  }
  
  
  int **b = new int *[R];
  b = new int *[R]; 
  b[0] = new int[R*C];
  for (int i = 1; i &lt; R; i++)
  {
      b[i] = &amp;b[0][i*C];
  }
  
  
  MPI_Bcast(a, R*C, MPI_INT, 0, MPI_COMM_WORLD);
  MPI_Barrier(MPI_COMM_WORLD);
  
  int partR = R / size;
  int partC = C / size;
  int part = partR * partC*size; 
  
  int *c = new int[part];
  
  int sumF = 0;
  int sumS = 0;
  int tmp = 0;
  int x = 0;
  
  auto start = chrono::high_resolution_clock::now();
  
  for (int i = rank*partR; i &lt;= rank*partR+partR; i++) {
      for (int j = rank*partC; j &lt;= rank*partC + partC; j++) {
          if (i != 0 &amp;&amp; i != R - 1 &amp;&amp; j != 0 &amp;&amp; j != C - 1) {
              sumF = a[i - 1][j - 1] + a[i + 1][j - 1] + a[i - 1][j + 1] + a[i + 1][j + 1];
              sumS = a[i - 1][j] + a[i + 1][j] + a[i][j - 1] + a[i][j + 1];
              if ((sumF - sumS) &gt; 0)
                  tmp = 1;
              if ((sumF - sumS) &lt; 0)
                  tmp = -1;
              if ((sumF - sumS) == 0)
                  tmp = 0;
              c[x] = tmp;
              x++;
          }
      }
  }
  
  MPI_Gather(c, part, MPI_INT, &amp;(b[0][0]), part, MPI_INT, 0, MPI_COMM_WORLD);
  
  
  auto finish = chrono::high_resolution_clock::now();
  
  chrono::duration&lt;double&gt; elapsed = finish - start;
  
  if (rank == 0) {
      cout &lt;&lt; endl &lt;&lt; &quot;Result: &quot; &lt;&lt; endl;
      for (int i = 0; i &lt; R; i++) {
          for (int j = 0; j &lt; C; j++) {
              cout &lt;&lt; b[i][j] &lt;&lt; &quot; &quot;;
          }
          cout &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl &lt;&lt; &quot;T: &quot; &lt;&lt; elapsed.count() &lt;&lt; endl;
  }
  
  
  MPI_Finalize();
  return 0;
  system(&quot;PAUSE&quot;);
  }
  </code></pre>