Help! C programming...
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 12 楼,当前显示第 5 楼 : 从楼主开始阅读 : 本帖树形列表 : 返回上一页
作者:远帆 (等级:2 - 初出茅庐,发帖:65) 发表:2007-04-23 13:55:50  5楼 
给你改了一下,可以编译了。不过计算数值的步骤可能有逻辑错误。你的程序里,变量 "h" 没有初始值,导致后面的结果为 0, 不过现在可以编译和运行了,检查一下是不是那些变量都设置对了。 good luck 程序如下 ------------------------ #include #include #define NT double NT f(NT x, NT y) { return x+y; } NT rkstep (NT x, NT y, NT h, NT *err_i) { NT k, y12, y1, k12; k = f(x,y); y12 = y + k*h/2; k12 = f(x, y12); y1 = y + k12*h; *err_i = fabs(k-k12)*h/2; return y1; } int main () { int i = 0; // for loop index NT x = 1; NT h = 1; // what is the value of "h" ? // dynamic sized arrays, (actually these are pointers) NT *y; NT *err; NT *result; int bound = 4; // "bound" is the size of your array x=1; // allocate memory for pointers y = (NT*) malloc(bound*sizeof(NT)); err = (NT*) malloc(bound*sizeof(NT)); result = (NT*) malloc(bound*sizeof(NT)); // ensure memory allocation is successful if(y == NULL || err == NULL || result == (more...)
copy paste 的时候 有问题,再改一下
#include <stdio.h>
#include <math.h>
#define NT double

NT f(NT x, NT y)
{
return x+y;
}

NT rkstep (NT x, NT y, NT h, NT *err_i)
{
NT k, y12, y1, k12;

k = f(x,y);

y12 = y + k*h/2;

k12 = f(x, y12);

y1 = y + k12*h;

*err_i = fabs(k-k12)*h/2;

return y1;
}

int main ()
{
int i = 0; // for loop index
NT x = 1;
NT h = 1; // what is the value of "h" ?

// dynamic sized arrays, (actually these are pointers)
NT *y;
NT *err;
NT *result;

int bound = 4; // "bound" is the size of your array

x=1;


// allocate memory for pointers
y = (NT*) malloc(bound*sizeof(NT));
err = (NT*) malloc(bound*sizeof(NT));
result = (NT*) malloc(bound*sizeof(NT));

// ensure memory allocation is successful
if(y == NULL || err == NULL || result == NULL)
{
printf("malloc() error: y, error or result!");
return 0;
}

y[0]=10;


for (i=0; i<bound; i++)
{
result[i]=rkstep(x, y[i], h, &err[i]);

printf("i=%d\n", i);
printf("result=%lf\n\n", result[i]);
}

// free the allocated memory space
free(y);
free(err);
free(result);

return 0;
}


Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表

本帖共有 12 楼,当前显示第 5 楼,本文还有 N-1 层楼,要不你试试看:点击此处阅读更多 >>



请登录后回复:帐号   密码