Thread: functions

  1. #16
    Registered User
    Join Date
    May 2002
    Posts
    28
    Originally posted by Hammer

    The next size up variable from an int is a "long int". A float holds numbers with digits after the decimal point.
    Thanks. I forgot that

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Although the problem seems solved, lemme go ahead and point out why the above code will not work...
    Code:
    int EVALUATE (int poly[],int num)
    { 
     int i,m,temp,total=0,result=1; 
    
     for(i=9;i>=0;i--) 
     { 
      temp=poly[i]; 
      for(m=0;m<i;m++) 
       result*=num; 
      total+=(result*temp); 
     } 
      printf ("%d\n", total);
      return total; 
    }
    This is a nested loop, the outside loop runs through each power of the polynomial, the inner loop handles calculating the power. BUT in order for the inner loop to work, the value of result must be 1 at the beginning of the loop (and it is easy to show that this is not so for any except the first iteration of the outer loop.
    Code:
    int EVALUATE (int poly[],int num)
    { 
     int i,m,temp,total=0,result=1; 
    
     for(i=9;i>=0;i--) 
     { 
      temp=poly[i]; 
      result = 1; // <- The change
      for(m=0;m<i;m++) 
       result*=num; 
      total+=(result*temp); 
     } 
      printf ("%d\n", total);
      return total; 
    }
    That should work, provided the result is within the bounds of an int.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #18
    Registered User
    Join Date
    May 2002
    Posts
    28
    Originally posted by QuestionC
    Although the problem seems solved, lemme go ahead and point out why the above code will not work...
    Code:
    int EVALUATE (int poly[],int num)
    { 
     int i,m,temp,total=0,result=1; 
    
     for(i=9;i>=0;i--) 
     { 
      temp=poly[i]; 
      for(m=0;m<i;m++) 
       result*=num; 
      total+=(result*temp); 
     } 
      printf ("%d\n", total);
      return total; 
    }
    This is a nested loop, the outside loop runs through each power of the polynomial, the inner loop handles calculating the power. BUT in order for the inner loop to work, the value of result must be 1 at the beginning of the loop (and it is easy to show that this is not so for any except the first iteration of the outer loop.
    Code:
    int EVALUATE (int poly[],int num)
    { 
     int i,m,temp,total=0,result=1; 
    
     for(i=9;i>=0;i--) 
     { 
      temp=poly[i]; 
      result = 1; // <- The change
      for(m=0;m<i;m++) 
       result*=num; 
      total+=(result*temp); 
     } 
      printf ("%d\n", total);
      return total; 
    }
    That should work, provided the result is within the bounds of an int.

    You're right.
    I was so happy when it worked twice I just stopped. should have tried a few more times

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM