Thread: functions

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    28

    functions

    Hi,
    it's me again...

    Can someone help me to find why I don't get the
    return I need?


    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);
    }
    return printf("%d",total);
    }

  2. #2
    Unregistered
    Guest
    try this:

    printf("%d",total);
    return(total);

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    28
    I've tried it but it doesn't work.

    Thanks anyway.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: functions

    Originally posted by dana_h
    Can someone help me to find why I don't get the
    return I need?
    What result do you need? Can you explain what you think you should be getting, and what you are getting.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    28
    I need to evaluate a polynomial for eg.:

    2x^3+4x

    x=2

    I should be getting "24"
    what I get are completley crazy numbers "12471158" or "0"

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, if it was a C problem I could help you, but I haven't done proper maths in years... sorry!

    If you are sure your maths is correct, maybe you could post some more code (the setup and call to that function).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    well..hi..
    what is the purpose of the array poly??
    Are you trying to find the answer to a known polynomial?
    Basically..could you explain your code better..thanks..
    A

  8. #8
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    aspand: I believe it would be the
    polynomial that is being evaluated.

    On your second for loop
    for(m=0;m<i;m++) /* should there be braces here?*/

    so it looks like this
    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); 
    } 
    return printf("%d",total); 
    }
    BTW your return statement there return the number of successful conversions that where printed (I think), but I know it has something to do with successes or failures.

    kwigibo

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

    If you are sure your maths is correct, maybe you could post some more code (the setup and call to that function).

    If the poly was enitiated it should evaluate it and if it wasn't
    since it is zero based it should return "0".

    This is part of the code which uses EVALUATE :


    /*The following program enables
    the user to manipulate two polynomials A and B,
    by using a menu of options for the user to choose from */

    #include <stdio.h>
    void INIT(int[]);
    void PRINTPOLY(int[]);
    void EVALUATE(int[],int);
    void SUM(int[],int[],int[]);
    void DERIVE(int[],int[]);
    void MULT(int[],int[],int[]);

    void main(){

    int A[10]={0};int B[10]={0};int C[19]={0};
    int total=0,count=0,num;
    char choice='0',name='a';

    while(choice!='7'){ /* prints the menu for the user untill he
    options to exit */
    printf("\n");
    printf("Which function do you want to use?\n"); printf("Enter %d to enter a polynomial\n",1);
    printf("%7d to print a polynomial\n",2);
    printf("%7d to evaluate a polynomial\n",3);
    printf("%7d to add two polynomials\n",4);
    printf("%7d to multiply two polynomials\n",5);
    printf("%7d to print a polynomial's derivative\n",6);
    printf("%7d to exit the program\n",7);

    scanf("%s",&choice);

    switch(choice){

    case '3': /* evaluates the polynomial(A,B or C) */

    printf("Enter polynomial to use A or B\n");
    scanf("%s",&name);
    if(name=='A')
    {
    printf("Enter x value\n");
    scanf("%d",&num);
    if(num>=0 && (num<=999))
    EVALUATE(A,num);
    }

    if(name=='B')
    {
    printf("Enter x value\n");
    scanf("%d",&num);
    if(num>=0 && (num<=999))
    EVALUATE(B,num);
    }

    if((name!='A') && (name!='B'))
    printf("wrong polynomial name\n");
    printf("\n");
    break;



    /*******************************************
    ******************************************/

    void EVALUATE(int poly[],int num){
    int i,m,temp,total=0,result=1;

    for(i=9;i>=0;i--)
    {
    temp=poly[i];
    for(m=1;m<=i;m++){
    result*=num;
    }
    total+=(result*temp);
    }
    printf("%d",total);
    }

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    RE: function

    Have you tried a different parameter for printf(%lu)? Maybe there's an overflow..?
    How about total,sum and result? Are these variables lareg enough? Maybe they should be long int's..

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    28

    Re: RE: function

    Originally posted by knutso
    Have you tried a different parameter for printf(%lu)? Maybe there's an overflow..?
    How about total,sum and result? Are these variables lareg enough? Maybe they should be long int's..
    I am new to C and have no idea what %lu means.
    We haven't learned it, therefore I can;t use it.

    Maibe if I knew what it does I could write it difrently (not %lu).

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When I run your code (latest version you've posted), it only printf's a zero, but I presume that's because the poly is all zeros.
    Is that what you are getting and are expecting?

    As per knutso's comments:
    an int can only hold upto a certain value before it will *overflow*. A signed int might hold -32768 to +32767. If your calculations force the number to exceed their limits, they will simply wrap round. So

    >32767 + 1 = -32768

    You can get round this a couple of ways. If you never want negative numbers, use an 'unsigned int'. This will then give you range 0 to 65535.
    Or you can use a 'long int', which has a much higher range (can't remember what).

    The high/low values are platform dependant, so don't take my word for it that your compiler is overflowing numbers. You can you this program to see the limits set though:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
    	printf ("INT_MIN: %d\n", INT_MIN);
    	printf ("INT_MAX: %d\n", INT_MAX);
    	printf ("UINT_MAX: %ud\n", UINT_MAX);
                    return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Are you sure you're not supposed to reset any of the vars for each round in the outer loop(result)?

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    28
    Following your advice:

    I changed my ints to floats and did a little editing.
    It works.
    I do get the answers for eg. "245.000000"
    but who cares IT WORKS.

    Thanks for the help.
    All of you.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by dana_h
    Following your advice:

    I changed my ints to floats and did a little editing.
    It works.
    I do get the answers for eg. "245.000000"
    but who cares IT WORKS.

    Thanks for the help.
    All of you.
    The next size up variable from an int is a "long int". A float holds numbers with digits after the decimal point.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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