Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#define max 200
float x[max],y[max];

float f(float x)
{

    return(x*x*x-5*x);

}
float find_c(float a,float b)
{

    float c,dx=0.5;
    c=f(a);
    a=a+dx;
    do
    {
        if(f(a)>c)
            c=f(a);
        a=a+dx;
    }while(a<=b);
    return c;
}
void create_randomX(int a,int b,int N)
{

    int i;
    for (i=0;i<N;i++)
    {

        y[i]=(((float)rand()/(float)(RAND_MAX/(b-a))))+a;
    }
}
void create_randomY(int c, int N)
{

    int i;
    for (i=0;i<N;i++)
    {

        x[i]=(float)rand()/(float)(RAND_MAX/c);
    }
}


int main()
{
    int n=0,N,i;
    float a,b,c,I;
    printf("Enter the lower Bound");
    scanf("%f",&a);
    printf("Enter the UPPER Bound");
    scanf("%f",&b);
    c=find_c(a,b);
    printf("Max f(X)",c);
    printf("Enter the number of iterations");
    scanf("%d",&N);
    create_randomX(a,b,N);
    printf("\n\n");
    create_randomY((int)c, N);
    for (i=0;i<N;i++)
    {
        if(f(x[i])<=y[i])
            n++;
    }
    printf("\n\nn=%d",n);
    I=(float)n*(b-a)*c/N;
    printf("\nI=%f",I);
    return 0;

}