440,320 Members | 2,109 Online
Need help? Post your question and get tips & solutions from a community of 440,320 IT Pros & Developers. It's quick & easy.

# How to return the correct value of M2 in the Matrix Multiply function?

 P: 22 Hello my code below is returning the wrong value when the MatrixMult function is called in StateEstimate for the value M2. When the code is debugged, with a break point at void MatrixMult() and i step through, M2 returns only the first line of A {1, 0, 0, T, 0, 0} where it should return the complete array which i believe is the reason why x gives out garbage and segmentation error at the end instead of {0,0,0,5,10,5}? Any help would be gratefully aprreciated. Expand|Select|Wrap|Line Numbers #include  #include  #include  #define T 10 /*********** global variables which remain constant ****/ static double A[6][6] = {{1, 0, 0, T, 0, 0}, {0, 1, 0, 0, T, 0}, {0, 0, 1, 0, 0, T}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 1}}; static double B[6][6] = {{50, 0, 0, 0, 0, 0}, {0, 50, 0, 0, 0, 0}, {0, 0, 50, 0, 0, 0}, {10, 0, 0, 0, 0, 0}, {0, 10, 0, 0, 0, 0}, {0, 0, 10, 0, 0, 0}}; static double U[1][6] = {0, -9.81, 0, 0, 0, 0 };   /************* matrix multiplication function **********/   void MatrixMult(double M1[][6], double M2[][6], double M3[][6],int m, int n) {   int i, j, k;   for(i=0;i

There are two problems that I see off the top, from just looking at your code:
1. In MatrixMult, lines 17 and 18, you use the same limit variable, m, for both loops.
2. In StateEstimate, line 50, your program should start printing garbage in the second pass through the i loop (i = 1).

Cheers!
Oralloy

 Expert 100+ P: 983 Chris, Well, the line 50 loop runs across six array rows. The array you're sending to the function `StateEstimate` only has one row (your array x at line 59). The remaining five row displays will pick up garbage from the stack. Luck! Oralloy Feb 14 '11 #4