簡體   English   中英

用於在C語言中計算二次軸和x軸之間的面積的程序

[英]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循環中增加zy

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);  

我知道這並不能立即解決問題,但是對於像這樣的東西,《數值食譜》是一本終極的書。 免費提供舊的C版本(當然,在C ++中仍然可以很好地工作)。 他們也有C ++版本,您可以購買。

它具有本書中每種算法的代碼,並逐步說明了您在做什么以及為什么。

鏈接到首頁

鏈接到C版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM