示例程序:#include stdio.h#include math.h#define EPSILON 1e-6double f(double x) {return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;}double f_prime(double x) {return 6 * pow(x, 2) - 8 * x + 3;}...

示例程序:
#include <stdio.h>
#include <math.h>
#define EPSILON 1e-6
double f(double x) {
return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}
double f_prime(double x) {
return 6 * pow(x, 2) - 8 * x + 3;
}
double h(double x) {
return pow(x, 3) - 4*pow(x, 2) + 3*x - 6;
}
double h_prime(double x) {
return 3*pow(x,2) - 8*x + 3;
}
double newton(double (*fp)(double), double (*fp_prime)(double)) {
double x = 1.5;
while (fabs(fp(x)) > EPSILON){
x = x - fp(x) / fp_prime(x);
}
return x;
}
int main() {
printf("%g\n", newton(f, f_prime));
printf("%g\n", newton(h, h_prime));
return 0;
}
沃梦达教程
本文标题为:C语言学习笔记 —— 函数作为参数


基础教程推荐
猜你喜欢
- C语言基础全局变量与局部变量教程详解 2022-12-31
- C/C++编程中const的使用详解 2023-03-26
- C利用语言实现数据结构之队列 2022-11-22
- 详解c# Emit技术 2023-03-25
- C语言 structural body结构体详解用法 2022-12-06
- 一文带你了解C++中的字符替换方法 2023-07-20
- 如何C++使用模板特化功能 2023-03-05
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- C++详细实现完整图书管理功能 2023-04-04
- C++中的atoi 函数简介 2023-01-05