Code:int degree(int x,int n) { int m=0; if(m<n) degree(x*x,m++) else return x; }
Code:int degree(int x,int n) { int m=0; if(m<n) degree(x*x,m++) else return x; }
what will happen if you call this function with parameters 2 and 1? Could you imagine?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
can you write me a functions for degree exp:2*2*2=8 or 2 on third
As vart was saying...
Unless you start passing in negative numbers to degree, the test portion will always be true, which means that the function will run until it uses all the memory on the stack and crashes the program.Originally Posted by vart
Maybe
if(m<n) return degree(x*x,m++)
It's at this point you get to learn about
degree(x*x,m++)
degree(x*x,++m)
degree(x*x,m+1)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Why make it recursive? Something like this should probably not be recursive.
Most recursive student exercises are best done as a loop, but still you have to practice these things.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
if you write some code, you should be able to play a role of the code interpretator with some simple input value. How you write it otherwise? Just using random guess?Originally Posted by crvenkapa
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
i m a student and i have an exam for recursion functions very soon. can some one wrote me a recursion for degree ??
pls help me
if some one now this
it can send me the code on
my email
<<snipped>>
or you can tell me on
my yahoo messenger::
<<snipped>>
Last edited by Salem; 01-13-2007 at 02:41 PM. Reason: Remove email addresses
The answer is no.Originally Posted by crvenkapa
Did you read the "homework" announcement? We don't do homework here.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
C'mon, you're close to the answer already.
What else have you tried since your first post?
Was it any better, what different answers did you get, did you learn anything?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i tried everything i learned when i call the function again
m is again zero and for that my code is does not work i
dont now how to fix that..
i m new in programming
int m=0; - this is the first line in the function and it says to set the m to 0
the recusive representetion of the degree:
x^n = x* x^(n-1)
f(x,n) = x * f(x,n-1)
it is correct for n>1
and f(x,0) = 1
f(x,1) = x
could you now fix your code to calculate f(x,n)?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
i find my problem
and i can fix it
tnx very much
the code for degree is
Code:int degree(int x,int n) { if(n==0) return 1; if(n==1) return x; return degree(x,n-1)*x; }