434,741 Members | 2,033 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

How to call functions from main section of program?

 P: 22 Hello I'm having difficulty calling functions from the main section of my program here. What I am trying to do is call the addition function below and add the two arrays Q and R together. When the code runs using a.out I receive segmentation fault error. Any help would be grately appreciated. Expand|Select|Wrap|Line Numbers #include  #include  #include  #define DELTA_T 0.5 #define DATA 2 #define T 10   /*********Definitions for global variables for use in the entire kalman filter code****************/   /*A = nxn matrix relating the state at the previous time step to the state at the current state A_t = transpose matrix of A B = refers to tghe optional control input to the state x. P_k = the a priori estimate error covariance matrix Pk =  the a posteriori estimate error covariance matrix H = measurement prediction matrix H_t = Transpose of marix H K = Kalman gain matrix Q = Process noise covariance matrix R = Measurement noise covariance matrix I = Intital error covariance matrix */   static double A = {{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 = {{50, 0, 0}, {0, 50, 0}, {0, 0, 50}, {10, 0, 0}, {0, 10, 0}, {0, 0, 10}}; static double I = {{1, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0}}; static double P_k = {{1, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0}};  static double Q = {{3, 2, 1, 5, 0, 0}, {6, 1, 5, 4, 3, 5}, {4, 5, 8, 5, 5, 6}, {5, 6, 7, 8, 5, 3}, {3, 4, 4, 5, 6, 7}, {7, 4, 4, 4, 4, 4}}; static double R = {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}}; static double H = {{1, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0}}; static double acc[] = {{0, -9.81, 0}};   /*********** random number generator *******************/   double frand()   {       return 2*((rand()/(double)RAND_MAX) - 0.5);   } /************ matrix transpose function ***************/ int MatrixTrans() { double M1; double M2; int m,n; int i,j,k; for(i=0;i

✓ answered by tdlr

Expand|Select|Wrap|Line Numbers
1. int MatrixAdd(double M1, double M2)
2. /* Addition of Matrix*/
3. {
4. double M3;
5. int m,n; //Define m and n
6. int i,j,k;
7.
8. for(i=0;i<m;i++) // Use m without initialization
9. {
10. for(j=0;j<n;j++) // Use n without initialization
11. M3[i][j]=M1[i][j]+ M2[i][j];
12. }
13. }

8 Replies

 P: 22 Expand|Select|Wrap|Line Numbers int MatrixAdd(double M1, double M2) /* Addition of Matrix*/ { double M3; int m,n; //Define m and n int i,j,k;   for(i=0;i

 P: 22 Right okay, then what should I intialise m and n as if they are supposed to equal the rows and columns of te matrix? Feb 4 '11 #3

 P: 22 If they are always 6x6, use 6. If not, use the same dimension that the two matrices M1 and M2 have. In the latter case, also set the dimension of M3 properly. Feb 4 '11 #4

 P: 22 So I should put 1. int MatrixAdd(double M1, double M2) 2. /* Addition of Matrix*/ 3. { 4. double M3; 5. int m = 6, n = 6; //Define m and n 6. int i,j,k; 7. 8. for(i=0;i

 P: 22 Yes, this works. But if the dimension is always 6, you could define a const integer somewhere in your code which stores all the matrices dimensions. Feb 4 '11 #6

 P: 22 Thanks also what is the conversion type four a double to output the value for a printf statement,as I would like to show what the value of M3 is; printf(" the array of M3 is ...",M3) Thanks for your help. Feb 4 '11 #7

 Expert 100+ P: 2,400 Take a look at Arrays Revealed for insights on how to work with multidimensional arrays, how to pass arrays to functions and how to return arrays from functions. Feb 4 '11 #8

 P: 22 Expand|Select|Wrap|Line Numbers Thanks also what is the conversion type four a double to output the value for a printf statement,as I would like to show what the value of M3 is;   You can find this information in every online cpp reference. If you don't know any, look here: Post by Banfa This time, here it is: %.f %.4f would print the double with 4 characters after the decimal dot. Feb 5 '11 #9 