开发者

Program that calculates area between quadratic and x-axis in 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 fu开发者_开发技巧nctions 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?


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜