search
Log In
39 votes
6.6k views

Consider the following function.

double f(double x){
    if( abs(x*x - 3) < 0.01) 
        return x;
    else 
        return f(x/2 + 1.5/x);
} 

Give a value $q$ (to $2$ decimals) such that $f(q)$ will return $q$:_____. 

in Programming
edited by
6.6k views
3
WHY WE ARE NOT TAKING NEGATIVE VALUE i.e -1.73 becz x has two roots why we decided to take only positive value.
2

@eyeamgj

( sorry for the mistake, yes it should includes negative values also )

x$^2$ < 3.01  ====> x < 1.735 or x >  -1.735

and 

x$^2$ > 2.99⟹ x > 1.729 or x < -1.729

0

  CAN U SHOW INTERVALS NOT GETTING .

0

 OK GOT THE INTERVALS BUT HOW WE ARE ACTUALLY DECIDING VALUE FOR X.?

3 Answers

76 votes
 
Best answer

(We can directly go to the "if" part to get one answer, but we need to solve "else" part too to get all possible answers which though is not asked in question)

Solving the else part:

$\frac{x}{2} + \frac{3}{2x} = \frac{x^2+3}{2x}$

So, the new value of $x$ will be  $\frac{x^2+3}{2x}$ and we need it equal to $x$.

$\frac{x^2+3}{2x} = x \\ \implies x^2 + 3 = 2x^2 \\ \implies x^2 = 3  \\ \implies x = 1.732 $


Now solving the if part.   

  abs(x*x - 3) < 0.01

So, $x^2 - 3 < 0.01  \text { and } -\left(x^2 - 3\right) < 0.01\\ \implies x^2 < 3.01  \text{ and } x^2 > 2.99\\ \implies x < 1.735 \text { and }x > 1.729$

Corrected to $2$ decimal places answer should be $1.73$ or $1.74$. 


edited by
0

although the ans. can be get by the if part also, but i am not getting the else part: why (x + 3)/2x to be equal to x ?

5
No need for getting answer, but to show that no other answer exist.
0
I didn't get if part.

How did you add 3 in 0.01 and have taken its square root? How ?

Guide me.
0
$x^2 - 3 < 0.01 \\ x^2 < 3 + 0.01$

Simple moving of 3 from LHS to RHS.
0

i didn't get for where x2 +3 /2x appears? 

1
Sir,

Abs( ) returns int value so i think 1.70 is also valid and many other such values bcoz 0<0.01. So as per answer key only 1.72-1.74 but many answer are possible.
0
I like that

-(x^2-3)<0.01

Part

Nice ans
5
@Arjun sir,

why have u made (x^2+3)/2x =x. ?

Its the return value of function which needs to be equal to x not the parameter itself???
5

why have u made (x^2+3)/2x =x. ?

Because it is asked "Give a value q (to 2 decimals) such that f(q) will return q:_____. "

If this is not given then first solve IF condition to get a range of values then solve ELSE condition (using this range) to find,  where does this function returns(or converges) for  values  of q (which are obtained via solving IF condition).

PS: Think about Newton–Raphson method :)

1
@chhotu .can you explain last point in some detail?how to get answer from else

if i call for f(x) and it goes to else part => and will call f(x/2 + 1.5/x) => now what to do?As per arjun sir equation we will set it to x again,so we call f(x).

f(x)=>f(x/2 + 1.5/x)=>f(x) //       what is gurantee that x*x<.01 in this case? May be it satisfies if may be it not.
1

@Chhotu

Its not clear what you intend to convey , can you elaborate ?

0
Thankyou @Arjun sir for explaining so well. But why  x can't be -1.73 or -1.74?                 I didn't understand.
0

@Arjun sir

Can you please explain why we consider else part also ???

in else part we want return x...in else it is return f(x/2+1.5x)

so f(x/2+1.5x) should get to ====> x...

but why x/2+1.5x ===>x

0

so f(x/2,1.5x) should get to ====> x

there is + operator but not comma 

1
and $f(q)$ must return $q$ and not $-q$
0

and f(q) must return q and not −q

why ?

0

@Arjun sir please clear this doubt.

What will be the answer if f(q) returns q is not mentioned?

Do we equating else part with x only for this above mentioned line?

0
The correct answer actually should be 1.73, but not a range since the question specifically says to give 'a' value to two decimal places. Only possible answer in this case is 1.73 as (1.74)^2 > 3.01 and abs(1.72^2-3)>0.01

1.74^2=3.027

1.72^2=2.958
0
why the answer is 1.74??
7 votes

I fell  we dont need q i got q as 1.735 it is said for what of  q

f(q) returns   q

so if  x2-3 <0.01 only then f(x) would return x  

so on solving x= (3.01)1/2  

someone please rectify me if m wrong and make me understand

0
Your answer was right. But I didn't understand what have you done in this question? You have taken square root of 3.01. right. But how have you decided to add 3 and 0.01 and find out its answer?

I didn't get such question? Can you tell me what we have to do in this question and how did you find out its answer?
0

actualy if you notice  you will see it is a maths saying for what value of the expression x2-3 <0.01 

i am saying this the maths because see in the code it is said when ever this condition is happening only then the function is returning what was given as parameter  i.e f(x) =x . and they  have told that in question 

Give a value q(to 2 decimals) such that f(q) will return q:_____.

now to backcheck it if we square and check it absolute value the value return would be what was passed

1 vote
f(q) will return q, if
x2 - 3 < 0.01 will become True.
So, x2 - 3 < 0.01
x2 - 3 < 0.01
x2 < 3.01
x < 1.732
Hence, x = 1.73.
Answer:

Related questions

89 votes
4 answers
1
7.8k views
Suppose $n$ and $p$ are unsigned int variables in a C program. We wish to set p to $^nC_3$. If $n$ is large, which one of the following statements is most likely to set p correctly? $p = n * (n-1) * (n-2) / 6;$ $p = n * (n-1) / 2 * (n-2) / 3;$ $p = n * (n-1) / 3 * (n-2) / 2;$ $p = n * (n-1) * (n-2) / 6.0;$
asked Sep 28, 2014 in Programming jothee 7.8k views
55 votes
3 answers
2
11.5k views
Consider the main memory system that consists of $8$ memory modules attached to the system bus, which is one word wide. When a write request is made, the bus is occupied for $100$ nanoseconds (ns) by the data, address, and control signals. During the same $100$ ns, ... be on the bus at any time. The maximum number of stores (of one word each) that can be initiated in $1$ millisecond is ________
asked Sep 28, 2014 in Operating System jothee 11.5k views
36 votes
3 answers
3
6.9k views
The number of distinct minimum spanning trees for the weighted graph below is _____
asked Sep 28, 2014 in Algorithms jothee 6.9k views
30 votes
6 answers
4
8.7k views
A cycle on $n$ vertices is isomorphic to its complement. The value of $n$ is _____.
asked Sep 28, 2014 in Graph Theory jothee 8.7k views
...