-
March 22nd, 2009, 01:37 AM
#1
how to get the output pattern of this function..
Code:
int what(int n,int m)
{
if(!n && !m) return 0;
if (n>m) return n + what (n-1,m);
return what (n,m-1)-m;
}
(1,1) = 0
(1,0) = 1
(2,2)= 1
i cant see a pattern
Last edited by transgalactic; March 22nd, 2009 at 01:49 AM.
-
March 22nd, 2009, 02:14 AM
#2
Re: how to get the output pattern of this function..
Your (2, 2) input is incorrect, it should be 0.
Also, what's the context of this problem? If it's for homework I can give hints, but not the answer.
-
March 22nd, 2009, 03:41 AM
#3
Re: how to get the output pattern of this function..
its questions from a test
give me a system for picking output
so i could get a formula out of it
-
March 22nd, 2009, 04:01 AM
#4
Re: how to get the output pattern of this function..
If you want to just generate a table of inputs and outputs, why not just not write a test program raelly fast and display the results while looping through n = 0 to 10 and m = 0 to 10. If you look over that list you should be able to figure out the formula relatively easily.
A few other things you can think about. This is a recursive function, and it's only non-recursive out put is 0, and only when both m and n are 0. If you look through the logic you should quickly be able to see that whenever n and m are equal, the output of the function is going to be 0. That's kind of a hint.
-
March 22nd, 2009, 10:22 AM
#5
Re: how to get the output pattern of this function..
Well, it looks like it returns the sum of all integers from 1 to n minus the sum of all integers from 1 to m.
Also, it will infinitely loop if either n or m are negative
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|