473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How to call functions from main section of program?

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. }

8 1866
tdlr
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
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
tdlr
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
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
tdlr
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
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
donbock
2,426 Expert 2GB
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
tdlr
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

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

Similar topics

3
by: Charles Larry | last post by:
Is there a way to define functions after the main part of a Python script? Example: #!/usr/local/bin/python # this code yields a NameError print_message("hello world")
3
by: Twinkletoes | last post by:
Hello people, my first post here... *wave* I need to know how to call a function in a different document. At the moment I've tried onFocus but I'm kinda in over my depth. Perhaps if I tell you...
1
by: Ben | last post by:
I am having trouble calling functions in another file.. Lets look at the scenario below: File xyz.cc: (I have both xyz.h and xyz.cc, for simplicity I've avoided xyz.h) ------------ struct xyz...
6
by: Charlie | last post by:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp. I would like to call a function that is located in ui.cpp from main() in calc.cpp. Both files have calc.h included, but when I tried to...
3
by: Matt Churchyard | last post by:
Now I realize this is off topic, and will probably quickly receive a list of replys from people who are quicker to criticize posters than give them help but if you know of a more suitable newsgroup...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
5
by: Mark | last post by:
What I want to do is call a function in my main form/class from the server (which the another program/client calls). The main form does not really create a server object so I can't pass the...
2
by: Brian | last post by:
I just have a basic style question here. Suppose you have the program: def foo1(): do something def foo2() do something else Assume that you want to call these functions at execution. Is...
7
by: Kenneth Brody | last post by:
The recent thread on "query about main()" got me thinking... As I recall, calling a function with the wrong parameters causes undefined behavior. (These all assume that no prototype of foo()...
2
by: Hek Mek | last post by:
Hi! I am writing a plugin for a program which will be loaded as a shared object. In the plugin I need to use a function (actually, several functions) from the main program that is not included in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.