-1

when I tried to multiple two negative numbers the value it is zero in c++, for example -5 * -3 the result is zero, why? this is my code

  #include <bits/stdc++.h>
      #include <iostream>

   using namespace std;
     void Multiply(const int v_arr[], const int m_arr[][3], int signed 
  o_arr[], int size)

{

for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
        o_arr[i] = 0;
        for (int k = 0; k < 3; k++)
            o_arr[i] += v_arr[k] * m_arr[k][i];
    }
}

//End your code here
    }
    int main()
     {
  int n;
   cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
    cin >> v_array[i];
}

for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
        cin >> m_array[i][j];
    }
}

      //fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
    cout << o_array[j] << " ";
   }
     return 0;
  }

how to fix it to get the correct result? the input is

2 2 -3 2 -3 2 -4

leen M
  • 477
  • 1
  • 5
  • 14
  • 2
    `cin >> n; int v_array[n];` is not valid c++. You should use `vector` instead. Also, `o_arr[i] += v_arr[k] * m_arr[k][i];` looks wrong. Shouldn't it be `m_arr[i][k]`? – cigien Aug 16 '20 at 19:52
  • how? please explain – leen M Aug 16 '20 at 19:53
  • Please also read: [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Ted Lyngmo Aug 16 '20 at 19:54
  • 2
    `int v_array[n];` is a VLA: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Ted Lyngmo Aug 16 '20 at 19:55
  • how to solve the problem? – leen M Aug 16 '20 at 19:57
  • Since you're not allowed to change `main()` I guess you are stuck with that non-standard code. Add `-Wall -Wextra -pedantic -pedantic-errors` to the compiler options to get more info. – Ted Lyngmo Aug 16 '20 at 20:04
  • @leenM *Do not change the code in the main function* -- Well, anyone willing to run your code using the Visual C++ compiler has no choice except to change the code. As others mentioned, variable length arrays are not C++. – PaulMcKenzie Aug 16 '20 at 20:07
  • we can change it i edit the question – leen M Aug 16 '20 at 20:10
  • how to solve the issue – leen M Aug 16 '20 at 20:10
  • `o_arr[2]` is never initialized here, as you're indexing it with `i` and not `k`. Maybe the loop conditions should be `i < 3` and `k < size`? – IlCapitano Aug 16 '20 at 20:13
  • Have you tried something simple, like `cout << ((-5) * (-3)) << endl;`? Next, you should build up from there: `int x = -5; int y = -3; int product = x * y; cout << product << endl;` This should give you confidence that multiplying negative numbers works in C++. Your problem may be elsewhere or with accessing data structures. – Thomas Matthews Aug 16 '20 at 20:17

1 Answers1

3

Your issue is here:

for (int k = 0; k < 3; k++)
    o_arr[i] += v_arr[k] * m_arr[k][i];
}

You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.

Assuming this is matrix*vector multiplication code, it should look like this (untested):

for (int k = 0; k < 3; k++)
    o_arr[k] += v_arr[i] * m_arr[i][k];
}

Also, your loop based on j is useless. You can remove it:

void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
    for(int k = 0; k < 3; k++) { //initialize output array
        o_arr[k] = 0;
    }
    for (int i = 0; i < size; i++) {
        for (int k = 0; k < 3; k++)
            o_arr[k] += v_arr[i] * m_arr[i][k];
    }

}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52