### Problem with matrix multiplication I'm trying to create the multiplication of matrix A and matrix B. I almost done, i think, but the result is a little bit off. Do you guys have any idea?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include using namespace std; int main () { int n, m, p; cin >> n >> m >> p; int a[n][m], b[m][p], c[n][p]; //scan matrix A for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } // scan matrix B for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { cin >> b[i][j]; } } // getting matrix C for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { c[i][j] = 0; for (int k; k < m; k++) { c[i][j] += a[i][k] * b[k][j]; } } } // output Matrix C for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { cout << c[i][j]; } cout << endl; } return 0; }`````` Hi,

The compiler is your friend:

 ```In function 'int main()': 7:12: warning: array of array of runtime bound [-Wvla] 7:21: warning: array of array of runtime bound [-Wvla] 7:30: warning: array of array of runtime bound [-Wvla] 27:13: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]```

Line 1 : don't do this, include the headers you need.
Line 2: Try to get out of the habit of doing this. Fine for now, but it will bite you one day. Lots written about this on the web.

Line 7: Illegal. Array size must be known at compile time.

Line 27: what is the value of k ? Results a little bit off, by a lot I would say.

Learn how to use a debugger. Set up a watch list of variables; step through the code 1 line at a time; see how values change; deduce what went wrong. Hopefully there is a debugger in your IDE.

Good Luck !!
Last edited on Thank you so much for the info. i'll try to fix the problem by adding the value of k(duh!) and removing some unnecessary things. again, thank you so much! maybe, initialize c to zero?
Topic archived. No new replies allowed.