Rectangular Approximation Program Help

This is a discussion on Rectangular Approximation Program Help within the C Programming forums, part of the General Programming Boards category; My program takes in a polynomial of any degree, and outputs it back the user. I want to be able ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Rectangular Approximation Program Help

    My program takes in a polynomial of any degree, and outputs it back the user. I want to be able to then pass this polynomial to a subfunction where it will be evaluated using the rectangular rule process. Currently i'm writing the function and I have run into a few problems, mainly having the correct coefficient be multiplied by the polynomial. I highlighted the parts of my program that deal with the rectangular rule.

    Now for the rectangular rule, for instance if I had a function that was just x^2 to be integrated on the bounds of [0,1] with 2 subintervals, the change in x, or delta x, would be (1-0)/2, or right bound minus left bound divided by subintervals. So that means the total approximate area would be f(1/2)*deltax + f(1)*deltax, which can be simplified to [f(1/2) + f(1)] * deltax

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int menu(void);
    void simpsonsrule(void);
    double rectanglerule(char *, double, double, double, int);
    
    
    main()
    {
    
    	int j=1;  //Loop for the menu
    	int technique; //Integration technique 
    	int order; //Order for the polynomial
    	int totterms; //Total number of terms in the equation
    	double a; //left endpoint
    	double b; //right endpoint
    	double boundtotal; //endpoint math
    	double subint; //subintervals
    	int loopvar; //for loop variable
    	double *ptr;
    	int count;
    	double rectarea; //Total area for the rectangle rule
    	double simparea; //Total area for the simpson rule
    
    	while(j)
    	{
    		puts("Welcome to the integration program.");
    		puts("You can compute an integral using either Simpson's or the Rectangular rule.");
    		puts("\nEnter the order of the polynomial you wish to integrate.");
    		scanf("%d",&order);
    		totterms= order+1;
    		ptr=malloc(totterms*sizeof(double));
    
    		for(count=totterms-1;count >=0; count--)
    		{		
    			if(count != 0)
    			{
    				printf("\nEnter the coefficient for the %d power: ", count);
    				scanf("%lf", &ptr[count]);
    			}
    			else
    			{
    				printf("\nEnter the constant: ");
    				scanf("%lf", &ptr[count]);
    			}
    		}
    
    		puts("\nEnter the left endpoint of the integral.");
    		scanf("%lf",&a);
    		puts("\nEnter the right endpoint of the integral.");
    		scanf("%lf",&b);
    		puts("\nEnter the number of sub-intervals you want to use.");
    		scanf("%lf",&subint);
    		boundtotal= (b-a)/subint;
    		printf("\nThe polynomial you entered was.\n");
    		
    		for(loopvar=totterms-1;loopvar >=0;loopvar--)
    		{
    			if(loopvar != 0)
    			{
    				printf("%lf^%d + ",ptr[loopvar],loopvar);
    			}
    			else
    			{
    				printf("%lf.",ptr[loopvar],loopvar);
    			}
    		}
    		printf("\nIt is going to be integrated from %lf to %lf.",a,b);
    		printf("\nWith %lf subintervals.",subint);
    
    		puts("\nTo integrate using Simpson's Rule enter 1.");
    		puts("To integrate using the Rectangular Rule enter 2.");
    		puts("To integrate using both techniques enter 3.");
    		scanf("%d",&technique);
    		if(technique == 1)
    		{
    
    		}
    		else if(technique == 2)
    		{
    			rectarea=rectanglerule(ptr,a,b,subint,order);
    			printf("The total area under the curve is %lf.", rectarea);
    		}
    		else if(technique == 3)
    		{
    
    		}
    		else
    		{
    			puts("You did not enter a valid menu option, goodbye.");
    		}
    		
    
    
    		j=menu();
    	}
    
    	
    
    }
    
    int menu(void)
    {
    	int j;
    	puts("To restart the program enter 1.");
    	puts("To quit the program enter 0.");
    	scanf("%d",&j);
    	return j;
    }
    void simpsonsrule(void)
    {
    
    }
    double rectanglerule(char *ptr, double a, double b, double subint, int order)
    {
    	double approxarea=0;  //Approximated total area
    	int i;
    	double deltax = (b-a)/subint;  //Change in x throughout the entire x-axis
    	double deltaxadd = (b-a)/subint; //Addition of the intervals to plug into functions
    
    	for(i=subint;i>0;i--)
    	{
    		//approxarea+=(function expression);
    		deltaxadd+=deltaxadd;
    	}
    }
    I'm not sure if I need to pass the order through this function as well and decrease it as the for loops continues or not.

    So far I have come up with...

    Code:
    double rectanglerule(char *ptr, double a, double b, double subint, int order)
    {
    	double approxarea=0;  //Approximated total area
    	int i;
    	double deltax = (b-a)/subint;  //Change in x throughout the entire x-axis
    	double deltaxadd = (b-a)/subint; //Addition of the intervals to plug into functions
    	double funcval=0; 
    	
    	printf("Order of pol is %d", order);
    
    	for(i=subint;i>0;i--)
    	{
    		
    		for(order=order; order >= 0; order--)  //order-1 because the constant can be added at the end
    		{
    			if(order !=0)
    			{
    				funcval+=(pow(&ptr[order], order));
    			}
    			else
    			{
    				funcval+=(&ptr[order]);
    			}
    		}
    		approxarea=funcval*deltax;
                                    approxarea+=approxarea;
    		deltaxadd+=deltaxadd;
    	}
    }
    This is giving me some errors though, saying chars cant be converted to doubles...
    Last edited by Noah; 03-14-2006 at 06:54 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    funcval+=(pow(&ptr[order], order));
    You're feeding a pointer to a char as the first parameter to pow when it expects a double. Take another look at your function prototype for starters.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Quote Originally Posted by Dave_Sinkula
    Code:
    funcval+=(pow(&ptr[order], order));
    You're feeding a pointer to a char as the first parameter to pow when it expects a double. Take another look at your function prototype for starters.
    Is there anyway I can make it so pow will accept a char pointer?
    If you can't tell I am a beginner at C
    Last edited by Noah; 03-15-2006 at 09:43 AM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Well you could use atof to convert a char* to a double.
    Or better yet, change your prototype/definition, as suggested, to accept a pointer to a double instead of a pointer to a char as the first parameter.
    You are passing in a double pointer from main.

    Also, don't use & when trying to use the value ptr points to

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Ok now I have working code (for the most part)

    Code:
    double rectanglerule(double *ptr, double a, double b, double subint, int order)
    {
    	double approxarea=0;  //Approximated total area
    	double i;
    	double deltax = (b-a)/subint;  //Change in x throughout the entire x-axis
    	double deltaxadd = (b-a)/subint; //Addition of the intervals to plug into functions
    	double funcval=0; 
    
    	
    	for(i=subint;i>0;i--)
    	{		
    		for(order=order; order >= 0; order--)  //order-1 because the constant can be added at the end
    		{
    			if(order !=0)
    			{
    				funcval+=(pow(ptr[order], order));
    			}
    			else
    			{
    				funcval+=(ptr[order]);
    			}
    		}
    		approxarea=funcval*deltax;
    		approxarea+=approxarea;
    		deltaxadd+=deltaxadd;
    	}
    	return(approxarea);
    }
    I'm trying to think of a way to have the deltaxadd variable be plugged into the function so I can obtain that value... should I make an array that equals the number of subintervals and have them incremented by deltaxadd each time?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21