I've been trying to make my vector calculator subtract. It was doing it fine with vectors that store the same amount of numbers, for example: (+) 503 - (-) 305. It would give me a good result on this kind of cases.
But when I tried subtracting different size vectors the problems appeared. I tried to solve the problem by making different filters to make the program act how I want it. But now, instead of subtracting all it does is add.
This is my setup:
const int MAX = 102;
typedef int vector[MAX];
A(0): Puts all the values of a vector at 0. For example:
Before using it:
[0][3][5][0][5]
Where the first 0 means it is a positive number, if it was [1] it would mean its negative, that [3] means that there are 3 numbers stored in the vector.
After using A0:
[0][1][0][0][0][0][0]...[0]
ES_MAJOR_VECTOR checks if vector Z > X. On that case returns true, else it's going to return false.
SUMA_VEC basically adds Z to X and then assings the value to W.
void RESTA_VEC(vector Z, vector X, vector W) {
int ZigualX(0), Xmes1(0), Xactual(0), ZmajorX(0), XmajorZ(0), contador(0);
vector copia_Z, copia_X;
A0(copia_Z);
A0(copia_X);
for (int k = 0; k < MAX; k++)
{
copia_Z[k] = Z[k];
copia_X[k] = X[k];
}
if (Z[0] == X[0])
{
if (Z[0] == 0)
{
for (int y = MAX; y >= 2; y--)
{
if (Z[y] < X[y])
{//RESTA
Z[y] = Z[y] + 10;
W[y] = Z[y] - X[y];
X[y + 1] = X[y + 1] + 1;
}
if (Z[y] > X[y])
{
W[y] = Z[y] - X[y];
}
if (Z[y] == X[y])
{
W[y] = 0;
}
}
}
if (Z[0] == 1)
{
SUMA_VEC(Z, X, W);
}
}
if (Z[0] != X[0])
{
if (ES_MAJOR_VECTOR(Z, X) == true)
{
for (int y = MAX; y >= 2; y--)
{
if (Z[y] < X[y])
{
Z[y] = Z[y] + 10;
W[y] = Z[y] - X[y];
X[y + 1] = X[y + 1] + 1;
}
if (Z[y] > X[y])
{
W[y] = Z[y] - X[y];
}
if (Z[y] == X[y])
{
W[y] = 0;
}
}
}
else
{
for (int y = MAX; y >= 2; y--)
{
if (X[y] < Z[y])
{
X[y] = X[y] + 10;
W[y] = X[y] - Z[y];
Z[y + 1] = Z[y + 1] + 1;
}
if (X[y] > Z[y])
{
W[y] = X[y] - Z[y];
}
if (X[y] == Z[y])
{
W[y] = 0;
}
}
}
}
for (int h = 0; h < MAX; h++)
{
Z[h] = copia_Z[h];
X[h] = copia_X[h];
}
}
How can I solve this? I've thought that I need to check: If they are positive or negative, if they're both positive I have to execute a subtraction, if they're negative I have to add Z to X and assign it to W.
If the vectors are different (Meaning one is positive and the another one is negative) I have to check which one is bigger, do the subtraction and then assign either 0 or 1 depending on the bigger vector of the two used earlier.
For example:
+5050
-305
W[0] = +
-5050
+305
W[0] = -