C语言问题,迭代法
#include #include #include #define CALLOC(ARRAY, NUM, TYPE) ARRAY = (TYPE*) calloc(NUM, sizeof(TYPE)); if (ARRAY == NULL) { printf("File: %s, Line: %d: ", __FILE__, __LINE__); printf("Allocating memory failed。
"); exit(0); }#define REALLOC(ARRAY, NUM, TYPE) ARRAY = (TYPE*) realloc(ARRAY, (N...全部
#include #include #include #define CALLOC(ARRAY, NUM, TYPE) ARRAY = (TYPE*) calloc(NUM, sizeof(TYPE)); if (ARRAY == NULL) { printf("File: %s, Line: %d: ", __FILE__, __LINE__); printf("Allocating memory failed。
"); exit(0); }#define REALLOC(ARRAY, NUM, TYPE) ARRAY = (TYPE*) realloc(ARRAY, (NUM)*sizeof(TYPE)); if (ARRAY == NULL) { printf("File: %s, Line: %d: ", __FILE__, __LINE__); printf("Allocating memory failed。
"); exit(0); }int calcsqrt(double* sqrta, int n, double* a){ int i; double xp, xn; for (i=0; i 1e-6); sqrta[i] = xn; } return 0;}int main(){ double* a = NULL; double* sqrta = NULL; int n = 0; double tmp; int i; printf("Please input a series of positive numbers, 0 to end:
"); while(1) { scanf("%lf", &tmp); if (tmp>0) { n ; REALLOC(a, n, double); a[n-1] = tmp; } else if (tmp==0) { break; } else { printf("The number must be greater or equal to 0。
"); } } CALLOC(sqrta, n, double); calcsqrt(sqrta, n, a); for (i=0; i printf("%-10。6f
", sqrta[i]); return 0;}编译、链接、运行程序,输入与输出如下::!gcc -Wall iter。
c -o iter:! 。/iterPlease input a series of positive numbers, 0 to end:12301。0000001。4142141。732051。
收起