
February 5th, 2003
10:12 PM
[Insert intelligent information about Nouko here]
Status: Offline!
[C] Question
int func(int x) {
int ans = 0;
while (x > 1) {
ans = ans + 1;
x = x / 2;
}
return ans;
}
That's the problem, the answer is ln(x), x being whatever value you entered for "x". How to you come to the answer though.
Last edited by akuma, February 5th, 2003 11:03 PM (Edited 1 times)

February 5th, 2003
11:19 PM
i hate c and i don't know if this works for c also (it works for c++):
// callinf the function
func(x, ans);
void func(int x, int &ans)
{
while (x > 1)
{
ans++; // is the same as ans = ans +1
x /= 2; // is the same as x = x /2
}
}
that is it! ans will be changed in the main function because it is the address of ans!