C 中计算二次轴和 x 轴之间面积的程序

发布于 2024-10-26 23:15:26 字数 916 浏览 2 评论 0原文

我对整个编程事情都很陌生,所以请耐心等待。 我想编写一个可以计算二次轴和 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)))`

有什么建议吗?

I'm new to this whole programming things so bear with me.
I want to make a program that could calculat the area between quadratic and x-axis.
Right now my code is only designed for functions were a is postive and c is negative.

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

The problem is that the program just never stops running. What im trying to do is to make small squares and measure their area (remmember LRAM and RRAM). So what im doing is (zero + a little bit) times y value (a*(y*y))+(b*y)+(c)))`

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤檠 2024-11-02 23:15:26

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 永远不会在循环期间改变。

In

do {
    z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
    y+0.01;
} while (x>y);

you should change y+0.01 to y += 0.01, if you use y+0.01, y never change during the loop.

¢蛋碎的人ぎ生 2024-11-02 23:15:26

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

Increment both z and y in the do while loop.

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);  
九歌凝 2024-11-02 23:15:26

我知道这不会立即有帮助,但对于这样的东西,《数值食谱》是终极书籍。旧的 C 版本(当然,在 C++ 中仍然可以很好地工作)是免费提供的。您还可以购买他们的 C++ 版本。

它包含书中每个算法的代码,并逐步解释您正在做什么以及为什么这样做。

主页链接

C 版本链接

I understand it doesn't help immediately, but for stuff like this, Numerical Recipes is the ultimate book. The old C version (which of course, still works quite nicely in C++) is available for free. They also have a C++ version you can buy.

It has code for every algorithm in the book, and explains step by step what you are doing and why.

Link to homepage

Link to C version

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文