Algorithm that generates orthogonal vectors: C++ implementation Recently I asked the similar question, but the algorithm was implemented in Python. Now I've tried to implement the same algorithm, but in C++ (I'm very new to it):

#include&lt;iostream&gt;
#include&lt;cstdlib&gt;
#include&lt;ctime&gt;
#include&lt;cmath&gt;
#include &lt;tgmath.h&gt;
using namespace std;



const int MAXRANGE = 1000;

int randint(int low, int high){
    return rand() % (high - low) + low;

    }

void generate_vector(float vec[],int dimension){
    for (int i = 0; i &lt; dimension; i++){
        vec[i] = randint(-MAXRANGE,MAXRANGE);
        }

    // We need to ensure that the last entry doesn't equal to zero

    if (vec[dimension-1] == 0){
        while (vec[dimension-1] == 0){
            vec[dimension-1] = randint(-MAXRANGE,MAXRANGE);

        }
    }


}

void generate_orthogonal (float *vector, float orthogonal_vector[], int dimension){
    float last_entry;
    float dot_product = 0;
    for (int i = 0; i &lt; dimension -1; i++){
        orthogonal_vector[i] = randint(-MAXRANGE,MAXRANGE);
    }
    for (int i = 0; i &lt; dimension -1; i++){
        dot_product = dot_product + (vector[i] * orthogonal_vector[i]);
    }
    last_entry = -(dot_product/vector[dimension-1]);
    orthogonal_vector[dimension-1] = last_entry;
}


float dot_product(float *vector1, float vector2[],int dimension){
    float sum = 0;
    for (int i = 0; i &lt; dimension; i++){
        sum += (vector1[i])*(vector2[i]);
    }
    return sum;
}


void print_vector(float *A,int dim){
    cout &lt;&lt; "(";
    for (int i = 0; i &lt; dim; i++){
        if (i == dim -1){
            cout &lt;&lt; A[i];
        }
        else{
            cout &lt;&lt; A[i] &lt;&lt; ",";}
    }
    cout &lt;&lt; ")^T" &lt;&lt; endl;
}
int main(){
    srand(time(0));
    int dimension;
    cout &lt;&lt; "Choose dimension for the vector: ";
    cin &gt;&gt; dimension;
    float arbitrary_vector[dimension], orthogonal[dimension];
    generate_vector(arbitrary_vector,dimension);
    generate_orthogonal(arbitrary_vector,orthogonal,dimension);
    cout &lt;&lt; "First vector: ";
    print_vector(arbitrary_vector,dimension);
    cout &lt;&lt; "Orthogonal vector: ";
    print_vector(orthogonal,dimension);
    cout &lt;&lt; "Dot product of the vectors: ";
    cout &lt;&lt; dot_product(arbitrary_vector,orthogonal,dimension) &lt;&lt; endl;

}




What can be improved?



As some people previously pointed out, the algorithm would throw exception if the last entry of the first vector was \$0\$. I made an a slight change to ensure that generate_vector function won't generate vector where the last element is zero.
Welcome to C++.

It seems that you are more or less trying to write C code rather than C++ though. Lets have a look at what you can improve:


Never do using namespace std; It is a terrible practise that gets you into trouble really fast. std:: is not that hard to type so get in the habit early.
If you have compile time constants use them via constexpr
Your dot_product function is already implemented in the standard library. It is called inner_product. So the following code can be replaced


float dot_product(float *vector1, float vector2[],int dimension){
    float sum = 0;
    for (int i = 0; i &lt; dimension; i++){
        sum += (vector1[i])*(vector2[i]);
    }
    return sum;
}



by the following code

std::inner_product(a.cbegin(), a.cend(), b.cbegin(), float(0.0));

Similarly there is a generate function that fills a vector with a generator. Note that C++ has a variety of good random number generators (rand() is not one of them) in the &lt;random&gt; header. I don't know why you use integers for the float vectors but you can adapt that easily.

EDIT: Incorporated problems found by @L.F.

#include &lt;algorithm&gt;
#include &lt;random&gt;

class randomStreamUniformInt {
public:
    explicit randomStreamUniformInt(int lower_bound, int upper_bound)
    : mt(std::random_device{}()), uniform_dist(lower_bound, upper_bound) {}
    explicit randomStreamUniformInt(int lower_bound, int upper_bound, double seed)
    : mt(seed), uniform_dist(lower_bound, upper_bound) {}

    int operator() () { return uniform_dist(mt); }
private:
    std::mt19937_64                     mt;
    std::uniform_int_distribution&lt;&gt;     uniform_dist;
};

static randomStreamUniformInt rng(-MAXRANGE, MAXRANGE); 

std::vector&lt;float&gt; generate random(const std::size_t numElements) {
    std::vector&lt;float&gt; res(numElements);
    std::generate(res.begin(), res.end(), rng);
    return res;
}


It would be even better to pass the random number generator as an argument rather than a global variable. I leave that as an exercise.
You should use standard facilities for arrays such as std::vector or std::array depending on whether you know the size at compile time. So the following code:


float arbitrary_vector[dimension], orthogonal[dimension]



Should be better written as

std::vector&lt;float&gt; arbitrary_vector = generate_random(dimension);


Note that it is always better to put each declaration into a single line.
Your method generate_orthogonal can be improved accordingly.

std::vector&lt;float&gt; generate_orthogonal(const std::vector&lt;float&gt;&amp; a) {
    // get some random data
    std::vector&lt;float&gt; b = generate_random(a.size());

    // find the last non zero entry in a
    // We have to turn the reverse iterator into an iterator via std::prev(rit.base())
    auto IsZero = [] (const float f) -&gt; bool { return f == float(0.0);};
    auto end = std::prev(std::find_if_not(a.crbegin(), a.crend(), IsZero).base());

    // determine the dot product up to end
    float dot_product = std::inner_product(a.cbegin(), end, b.cbegin(), float(0.0));

    // set the value of b so that the inner product is zero
    b[std::distance(a.cbegin(), end)] = - dot_product / (*end);

    return b;
}



So put together it would look something like this:

#include &lt;algorithm&gt;
#include &lt;random&gt;
#include &lt;vector&gt;

constexpr int MAXRANGE = 1000;

class randomStreamUniformInt {
public:
    explicit randomStreamUniformInt(int lower_bound, int upper_bound)
    : mt(std::random_device{}()), uniform_dist(lower_bound, upper_bound) {}
    explicit randomStreamUniformInt(int lower_bound, int upper_bound, double seed)
    : mt(seed), uniform_dist(lower_bound, upper_bound) {}

    int operator() () { return uniform_dist(mt); }
private:
    std::mt19937_64                     mt;
    std::uniform_int_distribution&lt;&gt;     uniform_dist;
};

static randomStreamUniformInt rng(-MAXRANGE, MAXRANGE); 

std::vector&lt;float&gt; generate random(const std::size_t numElements) {
    std::vector&lt;float&gt; res(numElements);
    std::generate(res.begin(), res.end(), rng);
    return res;
}

std::vector&lt;float&gt; generate_orthogonal(const std::vector&lt;float&gt;&amp; a) {
    // get some random data
    std::vector&lt;float&gt; b = generate_random(a.size());

    // find the last non zero entry in a
    // We have to turn the reverse iterator into an iterator via std::prev(rit.base())
    auto IsZero = [] (const float f) -&gt; bool { return f == float(0.0);};
    auto end = std::prev(std::find_if_not(a.crbegin(), a.crend(), IsZero).base());

    // determine the dot product up to end
    float dot_product = std::inner_product(a.cbegin(), end, b.cbegin(), float(0.0));

    // set the value of b so that the inner product is zero
    b[std::distance(a.cbegin(), end)] = - dot_product / (*end);

    return b;
}

int main() {
    std::size_t dimension = 20;

    std::vector&lt;float&gt; a = generate_random(dimension);
    std::vector&lt;float&gt; b = generate_orthogonal(a);
}