返回数组并打印它

时间:2014-11-29 18:48:50

标签: c arrays function pointers return

我需要返回T中数组taylor_tan()中的所有值并将其打印在函数myprint()中,还是应该使用T作为指针?如果是的话,你能告诉我怎么做吗?

void myprint(char *argv[],double x)
{
      int j = atoi(argv[4]);
      int n = atoi(argv[3]);
      double T[13] = taylor_tan(x,n);
      double M = tang(x);
      double C = taylor_tan(x,n);

   for(int i = n; i <= j; i++)
   {
      printf("%d %e %e %e %e %e\n", i , M , T[i] , M - T[i] , C , M - C);
   }
}

double taylor_tan(double x,int n)
{ 
   double a [13] = {1, 1, 2, 17, 62, 1382, 21844, 929569, 6404582, 443861162, 18888466084}       
   double b [13] = {1, 3, 15, 315, 2835, 155925, 6081075, 638512875, 10854718875, 1856156927625}
   int i = 0;
   int k = -1;
   double D = 0;
   double T[13];

 if((x >= 0) && (x <= 1.4))                     //kontroluje ci je x v spravnom intervale
 {
    for (i = 0 ; i < 13 ; i++)
    {   
     n++;
     k += 2;
     D = D + ((a[i] * (square(x,k))) / b[i] );  
     T[i] = D;                      
   //printf("%.10e\n", T[i]);
    }
  }
 else 
 {
   printf("error\n");    
   return -1;
  }
return T[i];
}  

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,因为我无法测试它是否有效,但首先:

   double a [13] = {1, 1, 2, 17, 62, 1382, 21844, 929569, 6404582, 443861162, 18888466084}; \\don't forget the ;       
   double b [13] = {1, 3, 15, 315, 2835, 155925, 6081075, 638512875, 10854718875, 1856156927625}; \\don't forget the ;

然后是原始问题。您必须将指向要获取值的数组的指针传递给函数taylor_tan

更改myprint

  double* T;
  double M = tang(x);
  double C = taylor_tan(x,n, &T);

并更改taylor_tan的功能参数,如下所示:

double taylor_tan(double x,int n, double** T1)

并且在taylor_tan中包括在开始时或返回之前

*T1=T;
希望这会有所帮助。