Eratosthenes sieve and MPI
Since you tagged this question c++ I assume you would like a C++ approach also.

I would suggest using std::vector&lt;int&gt; rather than an int* and malloc. Using the vector will give you a number of useful advantages over a normal pointer, for example, memory management is greatly simplified, and there is no need to worry about how big an int is. With vector, the order of all elements is guaranteed to be contiguous.

#include &lt;algorithm&gt;
#include &lt;functional&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

int nextNumber(int k, const std::vector&lt;int&gt; &amp;array, int n)
{
    std::vector&lt;int&gt;::iterator i = std::find_if(array.begin(), array.begin() + n, std::bind2nd(std::greater&lt;int&gt;(), k));

    return (i == array.end() ? -1 : *i);
}

bool isMultiple(int a, int b)
{
    return (a % k == 0) &amp;&amp; (a != k);
}

void markMultiples(int k, std::vector&lt;int&gt; &amp;array, int n)
{
    std::replace_if(array.begin(), array.begin() + n, std::bind2nd(std::ptr_fun(isMultiple), k), -1);
}


void printArray(const std::vector&lt;int&gt; &amp;array)
{
    for(int i = 0; i &lt; array.size(); i++)
        std::cout &lt;&lt; "array[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; array[i] &lt;&lt; std::endl;
}


Disclaimer:

I am not very good at C++, so you may want to take this advice with a teaspoon of salt.