Problem with for loop calling external function

This is a discussion on Problem with for loop calling external function within the C Programming forums, part of the General Programming Boards category; Hello everyone, I've been reading these boards for a week or so and have gained some useful pointers from it. ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    16

    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[0]!='\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[0]!='\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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > for(i=0; i<number_terms; i++);
    Watch that ; right at the end of the line. It turns this for loop into a NOP.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    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 subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 09:11 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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