Thursday, July 30, 2009

Getting a wierd error that I cant seem to fix when compiling my C Program?

Ok so I'm trying to make a program with a function that will raise X to the K power, in the main function I want it to go from -4 to 4....I think im doing everything write but compiler says no....any tips?


%26lt;code%26gt;


#define X 10


#define K -4


#define END 4





double RaiseRealToPower(double X, double K);


main()


{





int i, END;


printf("k %20lg", X);


for(i = K; i %26lt;= END; i++)


{


printf("%d %20.10lf\n", i, RaiseRealToPower(double X, double K));


}


}





double RaiseRealToPower(double X, double K){


double answer, X, K;


if(K %26lt; 0){


answer = 1 / (pow(X, K));


}


answer = pow(X, K);


return (answer);


}


%26lt;/code%26gt;





%26lt;errors%26gt;





hw04.2.c:9: error: syntax error before numeric constant


hw04.2.c: In function `main':


hw04.2.c:13: error: syntax error before numeric constant


hw04.2.c:17: error: syntax error before "double"


hw04.2.c: At top level:


hw04.2.c:21: error: syntax error before numeric constant


hw04.2.c: In function `RaiseRealToPower':


hw04.2.c:22: error: syntax error before numeric

Getting a wierd error that I cant seem to fix when compiling my C Program?
Line "int i, END;"


Translates to "int i, 4;"


Which is not legal. Remove END from this line.





Line 'printf("%d %20.10lf\n", i, RaiseRealToPower(double X, double K));"


You do not call a function by defining the type. Try.


printf("%d %20.10lf\n", i, RaiseRealToPower( X, K));





Line "double RaiseRealToPower(double X, double K){"


X and K are defined to be 10 and -4. Use another name. Try


double RaiseRealToPower(double x, double y){





In the function RaiseRealToPower. Replace X and K with x and y so that the varables match the new declaration. Case matters, I mean lower case X.





You rewrite answer created within the if statement because you assign an answer unconitionally after the if (No else statement).
Reply:fist of all you defined END twice


END is already a constant which is defined in 3rd line (#define END 4). why did you define is again in 9th line (int 1, END)?





fix that first see if it works


i'm too lazy to keep reading your code... haha sorry
Reply:Why are you doing the following:


x == X;


y == K;





You are setting local variables to constants. Additionally, you aren't actually setting anything with these statements, but testing equality using '=='.





Okay, so your main loop is cycling based on i, but you never modify x and y, that is why your value never changes. You should be incrementing one of those.

garden state

No comments:

Post a Comment