# Thread: Need help with my program...

1. ## 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>

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.");
}

}

}

{
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)
{
}``` 2. > 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. 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. Popular pages Recent additions 