Thread: Need help with my program...

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

    Need help with my program...

    Well for my class I have to write a program that .....

    Develop a program that will:
    a.Calculate the integral of a n-order polynomial using Simpson’s rule and rectangle rule, the orders will be positive integers.
    b.The user shall input the coefficients to the polynomial
    c.The user shall be able to define the order of the polynomial
    d.The user will define how many segments will be used to approximate the integral
    e.The user will define the bounds of the integral
    f.The program should use a menu function that will allow the user to rerun the program after 1 execution.
    g.The user shall be able to define which integration technique they want to use (They should also have the option to run both integration techniques in one execution)
    h.The area calculated for each segment should be saved to an array that is dynamically allocated.
    i.The area array should be outputted to a text file for further review.
    j.The output file name should be user defined
    k.Upon successful calculation of an integral the program should output the integral solution to the screen.
    l.The program should be able to handle incorrect inputs
    i.User entering 0, negative number or letters should not cause program to crash

    I need help with the polynomial part, I dont know if I am going in the right direction, I have bolded the code that has to do with the entering of the polynomial.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int menu(void);
    double f(double);
    void simpsonsrule(void);
    void rectanglerule(void);
    
    
    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
    	double *ptr, *p;
    	int count;
    
    	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);
    		ptr=(double *)malloc((order+1)*sizeof(double));
    		p=ptr;
    		totterms= order+1; // +1 Because the order also needs a constant to go along with it at the end.
    
    		for(count=totterms;count >=0; count--)
    		{		
    				double coef;
    				printf("\nEnter the coefficient for the %d power.", count);
    				scanf("lf",coef);
    				*p=coef;
    				p++;
    		}		
                                    puts("\nEnter the left endpoint of the integral.");
    		scanf("%lf",&a);
    		puts("\nEnter the right endpoint of the integral.");
    		scanf("%d",&b);
    		puts("\nEnter the number of sub-intervals you want to use.");
    		scanf("%d",&subint);
    		boundtotal= (b-a)/subint;
    
    		puts("To 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)
    		{
    
    		}
    		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;
    }
    double f(double x)
    {
    
    }
    void simpsonsrule(void)
    {
    
    }
    void rectanglerule(void)
    {
    }
    Last edited by Noah; 03-10-2006 at 08:11 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,656
    > ptr=(double *)malloc((order+1)*sizeof(double));
    This cast is unnecessary in ANSI-C.
    You already included stdlib.h, so it does nothing.
    If you missed including the header file, then the cast would just hide that from you.

    > p=ptr;
    You don't need another copy of the pointer.

    > for(count=totterms;count >=0; count--)
    This loops totterms+1 times, which is order+2 times.

    > scanf("lf",coef);
    1. You forgot the &
    2. You can scan directly into your allocated memory without using a temporary variable.

    Consider
    Code:
    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--)
    {
      printf("\nEnter the coefficient for the %d power.", count);
      scanf("%lf",&ptr[count]);
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Thanks for your input. I have the p=ptr because I want to have two different pointers looking at the array. I am stumped on how I will output the array back to the user to show what they entered. I'm guessing a for loop but i'm just having a hard time coming up with the code to display the correct power along with the coefficient.

    Edit - Actually I think I can manage that part now I thought it out some.
    Last edited by Noah; 03-11-2006 at 10:33 PM.

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, 10:04 PM