[英]Program that calculates area between quadratic and x-axis in C
我是整个编程领域的新手,请耐心等待。 我想制作一个可以计算二次方和x轴之间区域的程序。 现在,我的代码仅适用于a为正而c为负的函数。
#include <stdio.h>
int main()
{
float a;
float b;
float c;
float x; /* this is the zero that is to the right*/
float y; /* this is the zero that is to the left*/
float z;
{
printf("Consider the function ax^2 + bx + c\n");
printf("Enter the a value: \n");
scanf("%f",&a);
printf("Enter the b value: \n");
scanf("%f",&b);
printf("Enter the c value: \n");
scanf("%f", &c);
x = (( -b + sqrt(b*b - 4*a*c)) / (2*a));
y = (( -b - sqrt(b*b - 4*a*c)) / (2*a));
do {
z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y+0.01;} while (x>y);
if (y>=x) {
printf("The area is %f", z);
}
问题在于该程序永远不会停止运行。 我想做的是制作一个小正方形并测量它们的面积(记住LRAM和RRAM)。 所以我在做什么是(零+一点)乘以y值(a *(y * y))+(b * y)+(c))))
有小费吗?
在
do {
z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y+0.01;
} while (x>y);
您应该将y+0.01
更改为y += 0.01
,如果您使用y+0.01
,则y
在循环期间永不更改。
在do while
循环中增加z
和y
。
z = 0; //initialize z to remove any garbage values.
do {
// here z is NOT getting assigned, but incremented each time this loop runs.
// NOTE: A += C; is short for A = A + C;
z += (((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y += 0.01;
}
while (x>y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.