C programming problem C2064?

2015-10-26 4:48 pm
Ok so my professor wants us to do some calculations, using 4 prototypes that will be placed after the main function, and output to the main function. Here is where the problem is in my code:

int computingcartesianToPolar1(int x, int y){
r=sqrt(x*x+y*y);
return cartesianToPolar1;
}
int computingcartesianToPolar2(int x, int y){
theta=atan(y/x);
return cartesianToPolar2;
}
int computingpolarToCartesian1(int r, int theta){
x=r*cos(theta);
return polarToCartesian1;
}
int computingpolarToCartesian2(int r, int theta){
y=r*sin(theta);
return polarToCartesian2;
}
This is supposed to relay it back to the main function, but it is not doing that and giving me the error message at each line where there is a calculation. It says that the term does not evaluate to taking one argument. Any tips on how to fix this? I would put up the whole code, but yahoo answers is having a problem uploading photos right now. Any help would be appreciated, thanks.

回答 (2)

2015-10-26 6:06 pm
Those are functions, not prototypes. Yes, you can place them after main(), and then you really will need to have prototypes. See below. The error message is confusing because Visual C++ is not really much of a C compiler. It says there's no function declared with the right arguments. That can be a missing prototype or a wrong argument list. The message makes sense in C++, where functions can be overloaded, but not in C.

The library functions sqrt(), sin(), cos() and atan() operate on doubles, not on ints. Even if you start with integer data, the conversion should be done in double, and then converted by the caller from and back to int, if that's what is needed. Maybe x and y coordinates can be ints without loss of accuracy, but an int for the polar angle in radians does not work at all.

Also, to get the proper polar angle, you need to do more than just atan(y/x). That will give the same polar angle for (-1, -1) as for (1, 1), even though they are in opposite directions from the origin. The <math.h> header file has a special version atan2(y,x) just for this purpose.

double polarAngle(double x, double y)
{
.... return atan2(y, x);
}

Basically, all that does is reverse the x,y arguments and call atan2.

That's a function definition. If you need to place that after main(), or in another source file, you will need a prototype to declare what it is before you use it. That's a fundamental rule in C: All names must be declared before they are used. A prototype for the polarAngle() function above looks like:

double polarAngle(double x, double y);

...and that has to be placed somewhere that's visible from every use of the function, and before the first use. Normally, this is at the top of your source file, after any #include lines but before the first function.
2015-10-26 5:02 pm
prototypes go before main()
definitions can go after main()
your functions are wrong...
the prototype is
double computingcartesianToPolar1(int , int );

and goes before main() to let the compiler know what the functions look like and how to call them...


the function should be:
double computingcartesianToPolar1(int x, int y){
return sqrt(x*x+y*y);
} // no need for a separate variable for the return value...


int computingcartesianToPolar2(int x, int y){
theta=atan(y/x);
bzzzt --------^^^
since both x and y are ints you will get an int result
not what you want for atan()
return cartesianToPolar2;
}

should be:
double computingcartesianToPolar2(int x, int y){
return atan((double) y/(double)x);
}


收錄日期: 2021-04-21 14:44:11
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20151026084804AAXWqkc

檢視 Wayback Machine 備份