我希望实现以下功能:
void calc ( double* a, double* b, int r, int c, double (*f) (double) )
输入参数a,r,c和f,输出b。“ a”和“ b”是具有“ r”行和“ c”列的二维矩阵。“ f”是一个函数指针,可以指向以下类型的任何函数:
double function‐name ( double x ) {
…
}
函数 calc
将矩阵a中的每个元素(即aij)转换为矩阵b中的bij = f(aij)。
我实现了以下calc
功能,并将其放在程序中进行测试:
#include <stdlib.h>
#include <iostream>
using namespace std;
double f1( double x ){
return x * 1.7;
}
void calc ( double* a, double* b, int r, int c, double (*f) (double) )
{
double input;
double output;
for(int i=0;i<r*c;i++)
{
input=a[i];
output=(*f)(input);
b[i]=output;
}
}
int main()
{
//input array:
int r=3;
int c=4;
double* a = new double[r*c];
double* b = new double[r*c];
//fill "a" with test data
//...
for (int i=0;i<r*c;i++)
{
a[i]=i;
}
//transform a to b
calc ( a, b, r, c, f1 );
//print to test if the results are OK
//...
for (int i=0;i<r*c;i++)
{
b[i]=i;
}
return 0;
}
问题是,我无法编译它。当我单击“编译并执行”按钮时,这是DevC ++的输出:
怎么了?
我感谢任何使实施效率更高的评论。
您的来源中似乎包含非法字符。我无法弄清楚\240
应该是哪个字符,但显然是在第10行的开头
在您发布的代码中,问题不存在:Live On Coliru
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句