#include #include float triangolo(float x, float x1, float x2, float xc, float h, int dbg) { float m1, m2, y; if ( ( xc < x1 ) || ( xc > x2 ) ) { printf("xc (=%f) deve esse compreso fra x1 e x2\n", xc); return 0.; } if (dbg) printf("Bene, ora cominciamo a fare i conti\n"); m1 = h / (xc-x1); m2 = h / (xc-x2); if (dbg) printf("Pendenze: m1 = %f; m2 = %f\n", m1, m2); if (x <= xc) { y = m1 * (x - x1); } else { y = h + m2 * (x - xc); } if (dbg) printf("y = %f \n", y); return(y); } int main() { float x1=0, x2=10, xc=4.5, h = 5; float x, y, dx, area; int N = 100; area = (x2-x1) * h / 2.; printf("Area (formula) : %f\n", area); dx = (x2-x1) / (float) N; printf("x1=%f, x2=%f, xc=%f; N=%d, dx=%f\n", x1, x2, xc, N, dx); area = 0.; for( int i = 0; i < (N+1); i++) { x = x1 + i * dx; y = triangolo(x, x1, x2, xc, h, 0); // printf("x = %f, y = %f\n", x, y); area += y * dx; } printf("\nArea calcolata numericamente: %f\n", area); return 0; }