Mọi người xem giúp em bài này code đã đúng chưa với ạ. Đề bài là: “Tính tích phân từ 1 đến 2 của hàm f(x) =x^2”. Em tính bằng máy thì ra 2,3333333 nhưng mà code xong thì kết quả toàn < 2 thôi ạ.
Em cảm ơn mọi người ạ !!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAX 99
float f(float x)
{
return (x*x);
}
int main()
{
int N, m=0, n=0,i=0;
double S;
float x[99999];
float y[99999];
printf("Nhap so diem muon tao: ");
scanf("%d", &N);
for( ; i<N; i++)
{
x[i]=rand() %2 + 1;
y[i]=rand() %4 + 1;
if(y[i]>f(x[i]))
{
m++;
}
else
{
n++;
}
}
printf("%d", n);
S = (double)((2-1)*(4-1)*(n))/(N);
printf("\nTich phan tu 1 den 2 cua f(x) = x^2 la: %10.6f", S);
return 0;
}