Output of a piecewise defined function

This is a discussion on Output of a piecewise defined function within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    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. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    recursion would be the way to go
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    No recursion please, only methods with loops and if statements.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    So, the fact is you're waiting for one of us to write you your program?

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    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. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Never mind, I got it.

    Lock thread, ban op, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 10:20 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21