# Thread: Output of a piecewise defined function

1. ## Output of a piecewise defined function

Hi all, I'm having trouble printing the output of a piecewise defined function

the function is f(x)= 1 for x=0,1,2
f(x-2) + f(x-3) for x>2

so the user will enter a value for x and I have to print all of the values of the function up to x. I'm trying to do this with loops, I'm not sure if recursion can be used but I would like to do it with loops/if statements only. I can obviously print the first 3 values of x. Can anyone add any tips? Thanks

2. Post what you've got so far and let the gang have a look...

I'm sure you'll get plenty of helpful hints and tips.

3. Okay, with this I'm actually having a bit of trouble even starting it. I use the obvious if (x<=2) then it will go into the block of the if statement and print either 1, 1 1, or 1 1 1 for the first three values.

For x>2, I don't even know how to get started. I'll think I have an idea, like adding two numbers and putting it into a variable and looping it somehow, but it just turns into a bunch of garble. What I'm looking for here is some way to start this program, like what the fundamental idea is behind it, or just to be thrown a bone.

4. recursion would be the way to go

5. No recursion please, only methods with loops and if statements.

6. So, the fact is you're waiting for one of us to write you your program?

7. No not at all, I was hoping for a one or two-liner hint that might be able to point me in the right direction because I am stuck.

8. Never mind, I got it.

Lock thread, ban op, etc.

Popular pages Recent additions