title: MPI C++ | How do I make the processes wait for the master?
body: <p>I have a program to make that should do the following: Read the size of an array, read each element of the array, and then a number that I want to find in that array. The program must split the search for each process. The problem I have is that I do not know how to make all the slave processes wait for the master to finish the reading process. Here is what I've got so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;cstdlib&gt;
  #include "mpi.h"
  
  using namespace std;
  
  void searchForElementInArray(int left, int right, int myArray[], int number);
  void readArrayAndNumber(int myArray[], int &amp;arraySize, int &amp;number);
  
  int main(int argc, char *argv[])
  {
      int LIMIT = 100;
      int numberOfProcesses;
      int processRank;
      int error;
      int length;
  
      int myArray[1010];
      int arraySize;
      int number;
  
      error = MPI_Init(&amp;argc, &amp;argv);
      if (error != MPI_SUCCESS) {
          cout &lt;&lt; "Error starting MPI program. Terminating.\n";
          MPI_Abort(MPI_COMM_WORLD, error);
      }
  
      // get the number of processes
      MPI_Comm_size(MPI_COMM_WORLD, &amp;numberOfProcesses);
      MPI_Bcast(&amp;arraySize, 1, MPI_INT, 0, MPI_COMM_WORLD);
  
      // Length of each interval
      length = arraySize / numberOfProcesses;
  
      //get the process rank
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;processRank);
  
      if (!processRank) { // Master
  
          readArrayAndNumber(myArray, arraySize, number);
  
          MPI_Send(&amp;myArray, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
          MPI_Send(&amp;arraySize, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
          MPI_Send(&amp;number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  
          MPI_Barrier(MPI_COMM_WORLD);
  
      }
      else { // Slave
  
          if (processRank != numberOfProcesses - 1) {
  
              MPI_Recv(&amp;myArray, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
              MPI_Recv(&amp;arraySize, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
              MPI_Recv(&amp;number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  
  
              searchForElementInArray(length * processRank, length * processRank + length, myArray, 66);
  
              MPI_Barrier(MPI_COMM_WORLD);
  
          }
          else {
  
  
              MPI_Recv(&amp;myArray, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
              MPI_Recv(&amp;arraySize, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
              MPI_Recv(&amp;number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  
  
              searchForElementInArray(length * processRank, arraySize, myArray, 66);
  
              MPI_Barrier(MPI_COMM_WORLD);
  
          }
  
      }
  
      MPI_Finalize();
      _getch();
      return 0;
  }
  
  void searchForElementInArray(int left, int right, int myArray[], int number) {
  
      int flag = 0;
  
      cout &lt;&lt; "Searched positions: ";
  
      for (int i = left; i &lt; right; i++) {
  
          cout &lt;&lt; i &lt;&lt; " ";
          if (myArray[i] == number) {
  
              cout &lt;&lt; endl &lt;&lt; "Found on position " &lt;&lt; i;
              flag = 1;
              break;
  
          }
  
  
      }
  
      if (flag == 0) {
  
          cout &lt;&lt; endl &lt;&lt; "Not found";
  
      }
  
      int processRank;
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;processRank);
      cout &lt;&lt; " by Process: " &lt;&lt; processRank &lt;&lt; endl &lt;&lt; endl;
  
  }
  
  void readArrayAndNumber(int myArray[], int &amp;arraySize, int &amp;number) {
  
      cin &gt;&gt; arraySize;
      for (int i = 0; i &lt; arraySize; i++) {
  
          cin &gt;&gt; myArray[i];
  
      }
      cin &gt;&gt; number;
  
  }
  </code></pre>