Thread: Question on functions?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    39

    Question on functions?

    I have an assignment that states that you have to write a function that calculates...

    1 – 2(x^2)/1! + 3(x^4)/2! – 4(x^6)/3! + … + [(-1)^n](n+1)(x^2n)/n!

    The general term is:
    negative one to the power of n
    multiply by (n plus one)
    multiply by x to the power of (2n)
    divided by n factorial

    And it must be written as a program that calls ONE single function, and all the calculations must take place in that one single function.

    Basically all the main program does is output the result, after the function calculates it.

    I'm finding it hard to understand how to write one single function that calculates all of those different equations... I only know about pow(), nFact(), and ourFabs() right now...driving me insane because the teacher doesn't teach anything and hardly speaks english.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The function will have a loop in it - to generate the terms of your formula. When 'n' is passed, the loop will count through 1 to n. You're going to keep a running total. You might want to think about how to do x^n and factorials... I would suggest keeping running products of those as well instead of calculating those from scratch each time.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I think they are hinting using recursion, which is one function calling itself (the same one function). For example, the summation from 1 to (some positive integer) n could be written using recursion as
    Code:
    int sum(int n)
    {
      if ( n == 1 )
      {
        return 1;
      }
      else
      {
        return n + sum(n-1);
      }
    }
    Recursion might be tricky if you arent used to it, so do some examples or try to understand this one. The principle is always the same: you need a base case at the start of your function which terminates the recursion, otherwise you have a general case which does something and calls itself again on new input (ie +1 or -1 of the original input, in the above example).

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Doesn't a function have to be a certain thing? Like pow(base, power)? I can't see how you could compute all those different equations from within one function, since can't a pow() function only compute powers and the such?

    I'm also failing to see a pattern between the first 3 given terms...it goes from 1, to 3, to -4? There's no real hint as to what is supposed to come after that negative 4.

    So there will be a loop involved? Ugh so confused.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well first off do you see a pattern in that function with those constants? Could those be turned into variables and stepped up accordingly? Since this is a series of additions for odd numbers and subtraction for even, just save the result then add/subtract the result by the previous result. Also do you know what data types to use for this?

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    The sequence looks like this....

    - 4(x^6)/3! + 5(x^8)/4! - ...

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    He didn't really specify what data type it is, besides it's c programming. I haven't had any real issues until this assignment. Where I have no idea where to even start. If I can just understand how the program is supposed to work I have a shot at writing it.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    So the sequence looks somewhat like -4, +5, -6, +7, -8? Subtracting for odd numbers and adding for even numbers? That does help but unfortunately I'm not really sure how to make the program know when it's supposed to add and when it's supposed to subtract.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    I understand that the factorial function looks like...

    Code:
    int nFact(int m)
    {
    int i;
    int nfact;
    i = nfact = 1;
    while ( i<=m )
    {
    nfact = nfact * i;
    i = i + 1;
    }
    return nfact;
    }
    and using recursive
    Code:
    int reFact(int m)
    {
    int res = 1;
    if (m !=0)
    res = m * reFact(m-1);
    return res;
    }
    The teacher says writing recursive functions are much simpler than writing iterative functions, but I don't understand what the recursive method is/does/how to do it.

    I feel hopeless!

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by evangela View Post
    Doesn't a function have to be a certain thing? Like pow(base, power)? I can't see how you could compute all those different equations from within one function, since can't a pow() function only compute powers and the such?

    I'm also failing to see a pattern between the first 3 given terms...it goes from 1, to 3, to -4? There's no real hint as to what is supposed to come after that negative 4.

    So there will be a loop involved? Ugh so confused.
    You don't see a pattern? It's a series... and the terms given are specifically shown so that anyone can see a pattern.
    1 - 2 + 3 - 4 + 5 - etc.

    Especially given the [(-1)^n](n+1)(x^2n)/n! at the end which is the generic term (pattern).

    It's simply 1,2,3,4,5 but alternating in sign.

    In the C function you would make a loop... incrementing a value from 1 to whatever... and using that index to calculate parts of those terms.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Using the mod operator should help in the logic of determining odd or even.

    Mod Operator help

    Knowing of the unary negation operator may help has well.
    Last edited by slingerland3g; 11-09-2009 at 02:52 PM.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I wouldn't be worrying about recursive factorial solutions. Just multiply 1 * 2 * 3 etc. for each successive term since each successive term is just the previous value multiplied by the current n.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    So a function is just like a mini program from within a program? So you can make any function that does anything you want?

    ex.
    myFunction()

    it says I need to compute -1^n, and later on x^2n. How can you do something to a power of a variable? I know how to do the power if the power is a number, but not a variable.

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Has your instructor advised on the math library and then the need to compile with -lm?

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Nope, it's supposed to be a fairly simple program, without the need for recursive methods. I know the basics of how to do the actual math equations, I just don't know how to do power of N.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about redefining functions in derived class
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2009, 11:48 AM
  2. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  3. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  4. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  5. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM