| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define    LEN    20

void transform(double *source, double *target, int n, double (*fp)(double));

int    main(void)
{
double    source[LEN];//源数组
double    target[LEN];//目标数组
double    num;
transform(source, target, LEN, sin(num)); //这里提示 错误
return    0;
}
void transform(double sou[LEN], double tar[LEN], int LEN, double sin(double num))
{
int i;
srand((unsigned)time(NULL));
for(i = 0; i < LEN; i++)        //为源数组赋值
{
sou[i] = rand() % 100 +1;
printf("%.2f  ", sou[i]);
}
for(i = 0; i < LEN; i++)        //把源数组值求正玄值赋值给目标数组
{
tar[i] = sin(sou[i]);
printf("%.2f  ", tar[i]);
}
}

[Error] cannot convert 'double' to 'double (*)(double)' for argument '4' to 'void transform(double*, double*, int, double (*)(double))'

```int main(void)
{
double    source[LEN];//源数组
double    target[LEN];//目标数组
double    num;
transform(source, target, LEN, &sin); //这里提示 错误
return    0;
}
void transform(double sou[], double tar[], int LEN, double (*fp)(double))
{
int i;
srand((unsigned)time(NULL));
for(i = 0; i < LEN; i++)        //为源数组赋值
{
sou[i] = rand() % 100 +1;
printf("%.2f  ", sou[i]);
}
printf("\n");
for(i = 0; i < LEN; i++)        //把源数组值求正玄值赋值给目标数组
{
tar[i] = fp(sou[i]*3.14159265/180);//库函数 double sin(double x) 参数是弧度角。
printf("%.2f  ", tar[i]);
}
}

```

#define    LEN    20

void transform(double sou[LEN], double tar[LEN], int LEN, double sin(double num))

LEN 冲突

[此贴子已经被作者于2021-7-12 12:35编辑过]

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

void transform( const double* restrict src, double* restrict dst, size_t n, double(*fp)(double) )
{
for( size_t i=0; i!=n; ++i )
dst[i] = fp( src[i] );
}

int main( void )
{
enum { LEN = 20 };
double src[LEN];
srand( (unsigned)time(NULL) );
for( size_t i=0; i!=LEN; ++i )
src[i] = (int)(rand()/(RAND_MAX+1.)*100) + 1;

double dst[LEN];
transform( src, dst, LEN, &sin );

for( size_t i=0; i!=LEN; ++i )
printf( "%5.2f%c", src[i], " \n"[i+1==LEN] );
for( size_t i=0; i!=LEN; ++i )
printf( "%+5.2f%c", dst[i], " \n"[i+1==LEN] );
}```

84.00 42.00　3.00 34.00　9.00　4.00 25.00 78.00 18.00 17.00 61.00 54.00 22.00 68.00 48.00 93.00 39.00 18.00　4.00 95.00
+0.73 -0.92 +0.14 +0.53 +0.41 -0.76 -0.13 +0.51 -0.75 -0.96 -0.97 -0.56 -0.01 -0.90 -0.77 -0.95 +0.96 -0.75 -0.76 +0.68

[此贴子已经被作者于2021-7-12 13:15编辑过]

• 7
• 1/1页
• 1