K
kostas
Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;
if(fabs(tmp)<=tol)
break;
if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}
1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi
2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415
3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.
4. Correct the previous bug.
----------------------------------------------------------------------------------------
What do you think is the difficulty level of the exercise
Any other comments?
regards
Kostas
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;
if(fabs(tmp)<=tol)
break;
if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}
1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi
2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415
3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.
4. Correct the previous bug.
----------------------------------------------------------------------------------------
What do you think is the difficulty level of the exercise
Any other comments?
regards
Kostas