Abr*_*ham 7 c++ arrays pointers function matrix
我想要实现以下功能:
void calc ( double* a, double* b, int r, int c, double (*f) (double) )
Run Code Online (Sandbox Code Playgroud)
输入参数a,r,c和f,输出b."a"和"b"是具有"r"行和"c"列的2d矩阵."f"是一个函数指针,可以指向以下类型的任何函数:
double function?name ( double x ) {
…
}
Run Code Online (Sandbox Code Playgroud)
函数 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;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我无法编译它.当我点击Compile and Execute按钮时,这是DevC++的输出:
怎么了?
我感谢任何评论,以提高实施效率.
看来您的来源中有非法字符。我不知道\240应该是什么字符,但显然它在第 10 行的开头
在您发布的代码中,该问题不存在:Live On Coliru
小智 6
如前一个回复中所述,这通常在编译复制粘贴代码时出现.如果您有bash shell,则以下命令通常有效:
iconv -f utf-8 -t ascii//translit input.c > output.c
Run Code Online (Sandbox Code Playgroud)
该/240错误是由于线的每个代码之前非法的空间。
例如。
做
printf("任何东西");
代替
printf("任何东西");
当您在 IDE 中复制和粘贴代码时,此错误很常见。