# Thread: Problem with for loop calling external function

1. ## Problem with for loop calling external function

Hello everyone,

I've been reading these boards for a week or so and have gained some useful pointers from it. However I am still having trouble with the program that I am currently trying to write. The program has to integrate a polynomial, after taking input values from the user for the order of the polynomial, the coefficients, and the integration limits. I want the inputs to be as bullet proof as possible, i.e. asking for reentry if invalid. This is not the problem however.

My program:
Code:
```/*****************************************************
This program takes numerical input from the user,and
checks that the entry is a valid number of the required
type i.e. no alphabetic or other characters except decimal
points where required. It then uses Horner's Rule to
evaluate the value of the integral of a polynomial
created using the user's input values.
******************************************************************/

#include <stdio.h>
#include <stdlib.h>

/*Validation functions*/

long int OrderValidation()
{
int temp=0;
long int order;
char bufOrd[BUFSIZ], *ptrO;

printf("Please enter the order of the polynomial, which must be between 0 and 100 inclusive: ");
while(temp==0)
{
if(fgets(bufOrd, sizeof(bufOrd), stdin) != NULL)
/*Reads user input as a string*/
{
order=strtol(bufOrd, &ptrO, 10);

if(bufOrd!='\n' && (*ptrO=='\n' || *ptrO=='\0')) 			                {
if(order<0 || order>100)
{
printf("Please ensure that the order lies between 0 and 100.\n ");
printf("Please re-enter the order of the polynomial: ");
}
else
temp=1;
}
else
printf("Please re-enter the order of the polynomial, using only numeric characters: ");
}
else
printf("Please enter the order of the polynomial:\n");
}
return(order);
}

double CoeffLimitValidation(int i, int number_terms)
{
int counter=0;
double coeff;
char bufCo[BUFSIZ], *ptrC;

if(i>=0 && i<number_terms)
printf("Please enter the value of the coefficient for x^%d: ", i);
else if(i==-1)
printf("Please enter the value of the lower integration limit: ");
else if(i==-2)
printf("Please enter the value of the upper integration limit: ");

while(counter==0)
{
if(fgets(bufCo, sizeof(bufCo), stdin) != NULL)
{
coeff=strtod(bufCo, &ptrC);

if(bufCo!='\n' && (*ptrC=='\n' || *ptrC=='\0'))
counter=1;
else
printf("Invalid entry. Please re-enter the value: ");
}
}
return(coeff);
}

int main()
{
int number_terms, i, j, k;
long int order;
double *coeff, low_limit, up_limit, lower, upper, integral;

order=OrderValidation();
printf("The order of the polynomial is: %d\n", order);			/*Test Function*/

number_terms=order+1;
coeff = (double*) malloc((number_terms)*sizeof(double));
/*Assign memory to coeff array*/

if(coeff!=NULL)
printf("Please enter the coefficients in order of increasing power of x, starting with the constant.\n");

else
{
printf("Error allocating memory. the program will now terminate");
exit(EXIT_FAILURE);
}

for(i=0; i<number_terms; i++);	/*Take into account constant term*/
{
coeff[i]=CoeffLimitValidation(i, number_terms);
/*validates each individual coeff, progressing when correctly input*/

printf("Coefficient for x^%d: %g\n", i, coeff[i]);	/*Test function*/
}
/*reset the value of i in order to reuse CoeffLimitValidation to get the limits*/
i=-1;
low_limit=CoeffLimitValidation(i, number_terms);

/*reset i to reuse CoeffLimitValidation to get the limit*/
i=-2;
up_limit=CoeffLimitValidation(i, number_terms);

/*Calculate the lower integration value*/
for(j=order; j>=0; j--)
lower=((lower*low_limit)+(coeff[j]/(j+1)));
/*Uses Horner's rule with adjustment to account for integration*/

/*Calculate the upper integration value*/
for(k=order; k>=0; k--)
upper=((upper*up_limit)+(coeff[k]/(k+1)));
/*Uses Horner's rule with adjustment to account for integration*/

integral=upper-lower;
printf("The value of the integral is %g.\n", integral);

free(coeff);
return(0);
}```
The problem seems to be occuring somewhere in the sections in red. The order is accepted correctly, but then the program only asks me for 1 coefficient, then gives that as the coefficient for x^(order+1), which clearly doesn't exist. I'm guessing that there's a problem with how i've set up the loop, but i'm stumped after spending ages looking for the problem and would appreciate any help.

For information, here's an example of the output i'm getting.
Code:
```Please enter the order of the polynomial, which must be between 0 and 100 inclusive: 4
The order of the polynomial is: 4
Please enter the coefficients in order of increasing power of x, starting with the constant.
3
Coefficient for x^5: 3
Please enter the value of the lower integration limit: 32
Please enter the value of the upper integration limit: 21
The value of the integral is INF```
Thanks for any help. 2. > for(i=0; i<number_terms; i++);
Watch that ; right at the end of the line. It turns this for loop into a NOP. 3. Thanks you very much. That's solved that problem. I can't believe I missed that.

Although i'm still getting a value of Inf for the value of the integral. Is that likely to be because I'm overrunning the size of the variable? Popular pages Recent additions 