1. ## 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. try this:

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

3. I've tried it but it doesn't work.

Thanks anyway.

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

5. 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. 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).

7. 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. aspand: I believe it would be the
polynomial that is being evaluated.

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

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);
}```

13. Are you sure you're not supposed to reset any of the vars for each round in the outer loop(result)?

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. Originally posted by dana_h