[英]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.