About Me
Categories
Social Media
_Instagram
_Twitter
Contact me
passing and returning array in c language
Wednesday, November 2, 2016
#include
//standard library
#include
//standard mathematical library
//first derivative function
static double* GetDerivative(double t, double A[], double B[], double C[])
//the function must be pointer double*
//for returning array
{
//P・(t) = 3At^2 + 2Bt + C
static double Pdot[3] = {0}; //must be static
Pdot[0] = 3*A[0]*t*t + 2*B[0]*t + C[0];
Pdot[1] = 3*A[1]*t*t + 2*B[1]*t + C[1];
Pdot[2] = 3*A[2]*t*t + 2*B[2]*t + C[2];
return Pdot; //return the array
}
//first derivative function
int main ()
{
//P(t) = At3 + Bt2 + Ct + D
////P・(t) = 3At^2 + 2Bt + C
double *derivative = 0; //must be pointer double *
//because the function is a pointer as well
//double t[4] = {0.0, 0.25, 0.5, 0.75};
double t[1] = {1};
double A[] = {1, 2, 3};
double B[] = {4, 6, 6};
double C[] = {7, 8, 9};
double D[] = {10, 11, 12};
derivative = GetDerivative(t[0], A, B, C); //put the return function to derivative
//plus make it as an array
//do not add [] even if it is array,
// unless you want to point the specific
// address of the array like t[0]
printf("%ft2 + %ft + %f \n", derivative[0], derivative[1], derivative[2]);
return 0;
}
//output 18.000000t2 + 26.000000t + 30.000000
loading..
No comments
Newer Post
Older Post
Subscribe to:
Post Comments (Atom)
No comments