Question: A program should be made which finds if the two vectors a = (a0, a1, ..., an-1) and b = (b0, b1, ..., bn-1) (1 ≤ n ≤ 20) are linearly dependant. The input should be n, and the coordinates of the two vectors and the output should be 1 if the vectors are linearly dependant, else - 0.

I've been struggling for hours over this now and I've got absolutely nothing. I know only basic C++ stuff and my geometry sucks way too much. I'd be really thankful if someone would write me a solution or at least give me some hint. Thanks in advance !

```
#include <iostream>
using namespace std;

int main()
{
    int n;
    double a[20], b[20];
    cin >> n;
    int counter = n;
    bool flag = false;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        cin >> b[i];
    }
    double k;
    for (int i = 0; i < n; i++)
    {
        for (k = 0; k < 1000; k = k + 0.01)
        {
            if (a[i] == b[i])
            {
                counter--;
            }
        }
    }
    if (counter == 0 && k != 0)
        flag = true;
    cout << flag;
    return 0;
}

```

Apparently that was all I could possibly come up with. The "for" cycle is wrong on so many levels but I don't know how to fix it. I'm open to suggestions.

Answer: 
There are 4 parts to the problem:

### 1. Math and algorithms

Vectors **a** and **b** are linearly depndent if ∃*k*. **a** = *k* **b**. That is expanded to ∃*k*. ∑i=1..n *ai* = *k ai* and that is a set of equations any of which can be *solved for k.*

So you calculate *k* as *b0* / *a0* and check that the same *k* works for the other dimensions.

Don't forget to handle *a0* = 0 (or small, see below). I'd probably swap the vectors so the larger absolute value is denominator.

### 2. Limited precision numeric calculations

Since the precision is limited, calculations involve rounding error. You need to check for approximate equality, not exact, because most likely you won't get exact results even when you expect them.

Approximate equality comes in two forms, absolute (|*x* - *y*| < *ε*) and relative (1 - ε < |*x* / *y*| < 1 + ε). Obviously the relative makes more sense here (you want to ignore the last significant digit only), but again you have to handle the case where the values are too small.

### 3. C++

* Don't use plain arrays, use `std::vector`. That way you won't have arbitrary limits.
* Iterate with iterator, not indices. Iterators work for all container types, indices only work for the few with continuous integral indices and random access. Which is basically just vector and plain array. And note that iterators are designed so that pointer is iterator, so you can iterate with iterator over plain arrays too.

### 4. Plain old bugs

You have the loop over *k*, but you don't use the value inside the loop.

The logic with counter does not seem to make any sense. I don't even see what you wanted to achieve with that.
You're right, that code bears no reationship to the problem at all.

It's easier than you think (at least conceptually). Divide each element in the vector by the corressponding element in the other vector. If all those division result in the same number then the vectors are linearly dependendent. So `{ 1, 2, 4 }` and `{ 3, 6, 12 }` are linear because `1/3 == 2/6 == 4/12`.

However there are two technical problems. First you have to consider what happens when your elements are zero, you don't want to divide by zero.

Secondly because you are dealing with floating point numbers it's not sufficient to test if two numbers are equal. Because of rounding errors they often won't be. So you have to come up with some test to see if two numbers are *nearly* equal. 

I'll leave you to think about both those problems.