Thread: please help!

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    implementing a formula..plz help!

    hi everyone,

    this is my first post, i have just started c programing and having some difficulties. I hope someone can help me on this website.

    i have to write a c program which calculates the area under a curve using the simson rule.

    simpson rule : Yn+1=Yn-1+1/3[Xn+1+4Xn+Xn-1]

    data points, sould be read from standard input using scanf(). when no more data is available from input then the area and some discription should be written to standard output using printf().

    Implementation:
    -a seperate function called simpson() must be written which calculates each new increment of area using three current data points
    -every secon data point must be used as a centre for evaluating the area using three data points( the centre point is Xn)
    -the main() only reads the input, accumalates the situation, keeps track of the cauurent values of x and y, and finally prints the results.


    i kind of no whats going onm im goin to use a y variable , 3 x variables, a total variable and a null character to exit the while loop.(does this sound about right?)

    im sorry for the long post just wanted to make sure i made sence, im getting some tutoring tomorow but i wanted some help on getting started first.

    thanks very much.any help is appritiated.
    Last edited by efti; 04-10-2005 at 09:41 PM.

  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
    Well simply reposting your question without showing any effort isn't going to earn you many favours
    http://cboard.cprogramming.com/showthread.php?t=64158
    Especially since the previous thread is only a few hours old.

    Also, "help" is a dumb topic title - try being more descriptive.

    Now, what part of the problem are you stuck on - like for example can you at least focus on (and complete) the first line
    > data points, sould be read from standard input using scanf()
    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.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    True.



    I've done a prog using the trapezium rule. Shouldn't be too hard to adapt it for Simpson's rule. I'm not gonna do that or I'd be doing your homework

    The most important part is that you are accumulating each area as you go along.

    Code:
    /**********************************
    
      Integration via the trapezium
                Rule
                
                y=x^2
    ************************************/
    
    #include<iostream>
    #include<fstream>
    #include<math.h>
    
    
    int function(float,float,float&);
    
    using namespace std;
    
    int main()
    {
        float k,b;
        float h;
        //Area of Trapezium = 1/2(a+b)h;
        cout<<"y=x^2"<<endl;
        cout<<"Enter 'a' (upper bound):";
        cin>>k;
        
        cout<<"Enter 'b' (lower bound):";
        cin>>b;
        
        cout<<"Enter 'h' (The smaller h is the more accurate the solution i.e 0.0001):";
        cin>>h;
        
        
        float total_area=0;
        
        for (float a=b; a<k; a=a+h)
        {
            //call function
            float get_value;
            float y=a+h;
            //call function pass a y and return get value
            function(a,y,get_value);
           
            
            float area= 0.5*(get_value)*h;
            total_area=total_area + area;
            
        }
        
        cout<<total_area;
        
        int stop;
        cin>>stop;
        
            
    }
    
    
    int function(float a, float y, float& get_value)
    {
      //change equation to what you want
      
      
      float one=pow(a,2);   //x^2
      float two=pow(y,2);   //(x+h)^2
      
      get_value=one+two;
    
     
    }

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    sorry mate, i no i was reposting but i had figured out i posted it in the wrong section so no desrispect to anyone. but ye i guess my topic was a bit lame. thanks for your help but, ill try to figure out how to do my program, thanks.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    Ive worked out how to input my x and y's , but i am still stuck on how to get that formula into play. Can anyone help.

    i have to write a c program which calculates the area under a curve using the simson rule.

    simpson rule : Yn+1=Yn-1+1/3[Xn+1+4Xn+Xn-1]
    data points, sould be read from standard input using scanf(). when no more data is available from input then the area and some discription should be written to standard output using printf().

    Implementation:
    -a seperate function called simpson() must be written which calculates each new increment of area using three current data points
    -every secon data point must be used as a centre for evaluating the area using three data points( the centre point is Xn)
    -the main() only reads the input, accumalates the situation, keeps track of the cauurent values of x and y, and finally prints the results.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    double variance(double, double, int);
    
    void main(void)
    {
    	int x[3], y[3], n=0,m=0;
    	double total;
    
    	printf("Enter Y variable\n");
    	fflush(stdin);
    	while(scanf("%d", &y[n]))
    	{
    			n++;
    	}
    	printf("Enter X variable\n");
    	fflush(stdin);
    	while(scanf("%d", &x[m]))
    	{
    			m++;
    	}
    	n=0;
    	m=0;
    	printf("the data points are\n");
    	while(y[n] > 0)
    	{	
    		fflush(stdin);
    		printf("%d, %d\n", y[n], x[n]);
    		n++;
    	}
    
    
    	
    	
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    dude, i pretty much gave you your first function in your other, identical post.

    i just forgot to make the expression c/c++ readable by adding the implied multiplication in
    Y*n-1+1/3[X*n+1+4X*n+X*n-1]

    since you didn't pay attention the first time, i'll do it again but without the incrementing loop. oh yea, i didn't bother testing this either - it just seems like a waste of time.

    [code]
    #include <stdio.h>
    Code:
    int main(void)
    {
    	int X;
    	int Y;
    	flush(stdin);
    	
    	printf("Enter X: ");
    	scanf("%d", &X);
    	printf("Enter Y: ");
    	scanf("%d", &Y);
    
    	int n = 1;
    	int result = Y*n-1+1/3[X*n+1+4X*n+X*n-1];
    
    	printf("Given X = %d, Y = %d, and n = %d", X, Y, n);
    	printf("The result of Y*n-1+1/3[X*n+1+4X*n+X*n-1] is ");
    	printf(" %d\n", X, Y, n);
    
    	n = 2;
    	printf("Given X = %d, Y = %d, and n = %d", X, Y, n);
    	printf("The result of Y*n-1+1/3[X*n+1+4X*n+X*n-1] is ");
    	printf(" %d\n", X, Y, n);
    
    	return 0;
    
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed