制作一个应计算多项式y值导数和整数的程序...现在,在格式化for循环时遇到问题,因此它会循环遍历我希望它的所有x值(由用户确定)。
可以肯定的是,数学函数现在还不能完全正确,但是一旦我使这个循环正常工作,我以后可以再去研究它们
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x=0;
double fx=0, fD=0, A=0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
for(int i=xi; i<xf; i++) {
math(a, b, c, x, xi, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2*a) + b; //finding derivative values
*A = ((a/3)*pow(x,3) + (a/2)*pow(x,2) + c*x) - ((a/3)*pow(xi,3) + (a/2)*pow(xi,2) + c*xi); //finding integral values
return;
}
这里是输出的屏幕截图,因为您可以看到该表仅打印到1,而不是需要的5(由输入指示)。我需要更改for循环中的内容以使其正确输出,但我不确定该怎么做
答案 0 :(得分:0)
我看到您的代码有问题:
1>您的推导函数不正确。应该是2 * a * x + b
2>我将for循环更改为while循环,其中step是从stdin输入的xc
这是我为您提供最新答案的解决方案:
Image