Thread: C++ Program Help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    C++ Program Help

    Hi, new to the forum but I have read and understand u guys aren't here to do this for me and I know that and want to learn how to do this.

    My goal is to write a program that to compute the following sum: 1+2*3+3*4*5+4*5*6*7+5*6*7*8*9+6*7*8*9*10*11+...+n* (n+1)*(n+2)*...*(2n-1)

    Now me and some people figured this to be the same thing as
    (2n-1)!/(n-1)!

    here is what I have come up with so far but I am sure it's not correct any tips, suggestions, or constructive criticism is welcomed.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    int k, n, total=0, sum=0;
    
    cout << "Enter a whole number: \n";
    cin >> n;
    k = n;
    
    while (n>=1)
    {
    while (k<=2*n-1)
    {
    sum = k * ++k;
    }
    total= total+sum;
    k = --n;
    --n;
    }
    
    cout << "The answer to the equation for your number is ";
    cout << total << endl;
    
    return 0;
    }
    I know this isnt right b/c if n = 3 the answer output from the program should be 67 b/c
    (5!/2!) + (3!/1!) + (1!/0!) = 67 but I get 72 when I run the program.

    Thanks and sorry for the long post, me and my friends are just noobs trying to learn from mistakes.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by yankees07 View Post
    Now me and some people figured this to be the same thing as
    (2n-1)!/(n-1)!
    If that's the case, then this should be easy.

    Write a function to compute the factorial of a number. Then use that function to compute (2n -1)! and (n - 1)!. Then it's just a matter of simply dividing.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    sum = k * ++k;
    I believe you shouldn't do something like this in C++. I think the compiler is free to do ++k first and the result would be undefined.

    Rather
    Code:
    sum = k * (k + 1);
    ++k;
    My math skills are very rusty, but your formula doesn't look right. If n = 3 then
    1 + 2 * 3 + 3 * 4 * 5 = 67
    and
    (6-1)! / (3-1) ! = 5! / 2! = 60

    You are quite certainly supposed to program this iteratively (dumbly doing all the calculations and steps shown in the requirements). Programmers don't need math because computers are fast!
    Last edited by anon; 10-02-2007 at 02:22 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Ok I figured out how to get it to answer the (2n-1)!/(n-1)! which was way easier than i thought it would be thanks for the comment on it MacGyver. here is the working code:
    Code:
    #include <iostream>
    using namespace std;
    
    int factorial ( int n )
    {
    int result = 1;
    for (int i = 1; i <= n; i++)
    result *= i;
    return result;
    }
    
    int main()
    {
    int x, q, r;
    cout<<"Enter a number: \n";
    cin >> x;
    q = (2 * x - 1);
    r = (x - 1);
    
    cout<<"The answer is "<< factorial(q)/factorial(r) <<endl;
    
    return 0;
    }
    Anon in my example of n =3 this (2n-1)!/(n-1) should be done for n= 3....all the way until it's counted down to one.

    For example if I type in 3 (just using 3 b/c it's small) the program should compute:

    (2(3)-1)!/(3-1)! + (2(2)-1)!/(2-1)! + (2(1)-1)!/(1-1)!) which is simplified to this:

    (5!/2!) + (3!/1!) + (1!/0!) = 67 (note 0! = 1)

    the program I just posted only computes the first one (5!/2!) when i enter in 3 but I think I should have some kind of loop inserted to countdown from what the user inputs to 1 but I am having trouble with this. any tips?

    Thanks so much already guys.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My goal is to write a program that to compute the following sum: 1+2*3+3*4*5+4*5*6*7+5*6*7*8*9+6*7*8*9*10*11+...+n* (n+1)*(n+2)*...*(2n-1)

    Now me and some people figured this to be the same thing as
    (2n-1)!/(n-1)!
    Not quite. That is the same thing as the sum from 1 to n of (2n-1)!/(n-1)!.

    Writing a function to compute factorial is easy, but here's the limitation: for a 32 bit int, the best you can do is compute 11!. 12! is beyond the range of a 32 bit unsigned int. On the other hand, 19!/12! is well within the range of a 32 bit signed int, even though 19! and 12! are both too large.

    The way to overcome this limitation is to compute the product of the integers from 13 to 19 inclusive. Effectively, you just need to note that:
    (2n-1)!/(n-1)! = n * (n+1) * ... * (2n-1)
    Since the 1 * 2 * ... * (n-1) can be cancelled out.

    This product computation is trivial with a for loop. Then, all you need is another outer for loop to loop from 1 to n and take the sum of the products. Ironically, this is what you were directed to do in the first place.
    Last edited by laserlight; 10-02-2007 at 11:46 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    @yankees07, indent your code. You'll find it easier to follow, and people will be more inclined to help you.

    Also, using braces in all the optional places will save you from some hard to find problems caused by slipping from "optional" to "necessary" without realising it.
    Code:
    #include <iostream>
    using namespace std;
    
    int factorial(int n)
    {
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
    
    int main()
    {
        int x, q, r;
        cout << "Enter a number: \n";
        cin >> x;
        q = (2 * x - 1);
        r = (x - 1);
    
        cout << "The answer is " << factorial(q) / factorial(r) << endl;
    
        return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM