Paying attention when doing algorithm

- When declaring a variable , We should assign it to 0 ;
for example:

double x;
for(int i=0; i<100; i++)
x+=certainfunction(i);

The preceding code won't work because x hasn't assigned to any. Sometimes I forget this assignment, so the result  is an error "Local variable is not assigned".

- Type conversation

double certainfunction(int x)
{
return 1/(3*x);
}

This code will excutate but the expected result will be wrong, because of 1/(3*x) =0  for x=2, 3,5, .....

To make this code work properly,  we must cast the return result as follow:

return (double)(1/(3*x));

Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment