By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,033 Online
Bytes IT Community
+ 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
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define DELTA_T 0.5
  5. #define DATA 2
  6. #define T 10
  7.  
  8. /*********Definitions for global variables for use in the entire kalman filter code****************/
  9.  
  10. /*A = nxn matrix relating the state at the previous time step to the state at the current state
  11. A_t = transpose matrix of A
  12. B = refers to tghe optional control input to the state x.
  13. P_k = the a priori estimate error covariance matrix
  14. Pk =  the a posteriori estimate error covariance matrix
  15. H = measurement prediction matrix
  16. H_t = Transpose of marix H
  17. K = Kalman gain matrix
  18. Q = Process noise covariance matrix
  19. R = Measurement noise covariance matrix
  20. I = Intital error covariance matrix
  21. */
  22.  
  23. 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}};
  24. static double B[6][6] = {{50, 0, 0}, {0, 50, 0}, {0, 0, 50}, {10, 0, 0}, {0, 10, 0}, {0, 0, 10}};
  25. static double I[6][6] = {{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}};
  26. static double P_k[6][6] = {{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}}; 
  27. static double Q[6][6] = {{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}};
  28. static double R[6][6] = {{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}};
  29. static double H[3][6] = {{1, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0}};
  30. static double acc[][3] = {{0, -9.81, 0}};
  31.  
  32. /*********** random number generator *******************/
  33.  
  34. double frand()   {
  35.  
  36.     return 2*((rand()/(double)RAND_MAX) - 0.5);
  37.  
  38. }
  39. /************ matrix transpose function ***************/
  40. int MatrixTrans()
  41. {
  42. double M1[6][6];
  43. double M2[6][6];
  44. int m,n;
  45. int i,j,k;
  46. for(i=0;i<n;i++)
  47. {
  48. for(j=0;j<m;j++)
  49. {
  50. M2[j][i]=M1[i][j];
  51. }
  52. }
  53. }
  54. /************* matrix multiplication function **********/
  55. int MatrixMult()
  56. {
  57. double M1[6][6];
  58. double M2[6][6];
  59. double M3[6][6];
  60. int m, n;
  61. int i, j, k;
  62. for(i=0;i<m;i++)
  63.     for(j=0;j<m;j++)
  64.     {  M3[i][j]=0;
  65.        for(k=0;k<n;k++)
  66.         M3[i][j] = M3[i][j]+M1[i][k]*M2[k][j];
  67.     }
  68. }
  69. /************** matrix addition funciton **************/
  70. int MatrixAdd(double M1[6][6], double M2[6][6])
  71. /* Addition of Matrix*/
  72. {
  73.  
  74. double M3[6][6];
  75. int m,n;
  76. int i,j,k;
  77.  
  78. for(i=0;i<m;i++)
  79. {
  80. for(j=0;j<n;j++)
  81. M3[i][j]=M1[i][j]+ M2[i][j];
  82. }
  83. }
  84. /************** matrix subtraction functiom ************/
  85. int MatrixSub(double M1[][6],double M2[][6])
  86. /* Addition of Matrix*/
  87. {
  88. double M3[6][6];
  89. int m,n;
  90. int i,j,k;
  91.  
  92. for(i=0;i<m;i++)
  93. {
  94. for(j=0;j<n;j++)
  95. M3[i][j]=M1[i][j] - M2[i][j];
  96. }
  97. }
  98. /********************************************************/
  99. main()
  100. {
  101.  
  102. int i,j,k,m,n;
  103.  
  104. double S[6][6];
  105.  
  106. S[6][6] = MatrixAdd(Q,R);
  107. }
Feb 4 '11 #1

✓ answered by tdlr

Expand|Select|Wrap|Line Numbers
  1. int MatrixAdd(double M1[6][6], double M2[6][6])
  2. /* Addition of Matrix*/
  3. {
  4. double M3[6][6];
  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. }

Share this Question
Share on Google+
8 Replies


P: 22
Expand|Select|Wrap|Line Numbers
  1. int MatrixAdd(double M1[6][6], double M2[6][6])
  2. /* Addition of Matrix*/
  3. {
  4. double M3[6][6];
  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. }
Feb 4 '11 #2

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[6][6], double M2[6][6])
2. /* Addition of Matrix*/
3. {
4. double M3[6][6];
5. int m = 6, n = 6; //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. }
Feb 4 '11 #5

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[6][6])

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
  1. 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;
  2.  
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:
%.<precision>f
%.4f would print the double with 4 characters after the decimal dot.
Feb 5 '11 #9

Post your reply

Sign in to post your reply or Sign up for a free account.