Thread: error in c program

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Jerusalem, Israel, Israel
    Posts
    3

    Talking error in c program

    hi my friend i have problem in my code when i change the function my quastion is

    For the function above, f(x)=(e^x )/x^2 "on" [2,3]
    Let h=0.1
    1- Estimate the integral of f(x)on [2,3] using composite Simpson 1⁄3 with error less than 〖10〗^(-5).

    2-Estimate the integral of f(x)=e^(x^2+y^2 )/(x^2+y^2 ) on[1,2]×[2,3] using composite Simpson with error ≤〖10〗^(-5)
    this is a good code when i have a function with one variable answer 1
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    #define f(x) (exp(x))/(pow(x,2))
    void simpson();
    int main()
    {
        printf("WELCOME TO MY PROGRAM\n");
        simpson();
        return 0;
    }
    void simpson()
    {
        double f[11],sum=0,S,h=0.1;
        int k;
        for(k=1;k<=5;++k)
        {
    
    
            f[2*k-2]=f((2*k-2)*0.1+2);
            f[2*k-1]=f((2*k-1)*0.1+2);
            f[2*k]=f((2*k)*0.1+2);
            sum+=f[2*k-2]+(4*f[2*k-1])+f[2*k];
        }
        S=(h/3)*sum;
        printf("\nTHE ANSWER USING SIMPSON IS : %.5f\n",S);
    
    
    }
    when i change the function to two variable x, y
    i need another loop for y but i dont know how to make this in my code please help me

    my loop for x



    f[2*k-2]=f((2*k-2)*0.1+1);
    f[2*k-1]=f((2*k-1)*0.1+1);
    f[2*k]=f((2*k)*0.1+1);
    sum+=f[2*k-2]+(4*f[2*k-1])+f[2*k];

    my loop for y



    f[2*k-2]=f((2*k-2)*0.1+2);
    f[2*k-1]=f((2*k-1)*0.1+2);
    f[2*k]=f((2*k)*0.1+2);

    error code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define f(x,y) (exp(pow(x,2)+pow(y,2))/(pow(x,2)+pow(y,2))
    void simpson();
    int main()
    {
        printf("WELCOME TO MY PROGRAM\n");
        simpson();
        return 0;
    }
    void simpson()
    {
        double f[11],sum=0,S,h=0.1;
        int k;
        for(k=1;k<=5;++k)
        {
    
    
            f[2*k-2]=f(((2*k-2)*0.1+1),((2*k-2)*0.1+2)));
            f[2*k-1]=f(((2*k-1)*0.1+1),((2*k-1)*0.1+2)));
            f[2*k]=f(((2*k)*0.1+1),((2*k)*0.1+2);
            sum+=f[2*k-2]+(4*f[2*k-1])+f[2*k];
        }
        S=(h/3)*sum;
        printf("\nTHE ANSWER USING SIMPSON IS : %.5f\n",S);
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  3. C Program... Program or Compiler error?
    By Zemira in forum C Programming
    Replies: 13
    Last Post: 12-02-2009, 08:59 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. i'm trying to fix an error with my error messaging on my program
    By RancidWannaRiot in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2003, 01:02 PM