Hey guys. I was working on a simple program that computes an area beneath a graph, and this is what i have come up with until now:
Code:
#include <stdio.h>
#include <math.h>
double area(double a, double b, double n);
double areatriangle(double h, double w);
double areasquare(double h, double w);
double f(double x) { return (sqrt(4 - pow(x,2))); }
int main(void) {
double a, b, n, grapharea;
printf("\nPlease enter a: "); scanf("%lf", &a);
printf("\nPlease enter b: "); scanf("%lf", &b);
printf("\nPlease enter n: "); scanf("%lf", &n);
grapharea = area(a, b, n);
printf("\nArea beneath the graph is: %f", grapharea);
}
double area(double a, double b, double n) {
double x, sum = 0.0;
for (x = a; x < b; x += n) {
printf("\n%f", areatriangle(n, (f(x) - f(x-1))));
sum += areatriangle(n, (f(x) - f(x-1)));
sum += areasquare(n, f(x));
//printf("\nSum: %f X: %f",sum, x);
}
return (sum);
}
double areatriangle(double h, double w) {
double area = (h * w / 2);
//printf("\nTriangle = W: %f, area: %f", w, area);
return (area);
}
double areasquare(double h, double w) {
double area = (h * w);
return (area);
}
The thing is that whenever we had to get the area from a "defined interval" (I'm not sure what its called in english), and i attempted to let you choose the interval - but we where supposed to get the answer for the bestemt interval of -2 to 2. The thing is, that as long as X is less then -1, the function areatriangle() will return nan. I know that nan means not a number, but i really can't see why it can compute as long as x > -1. Can anyone push me in the right direction ?