Thread: exponent math, calculus?

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    15

    exponent math, calculus?

    Hey everyone. I need a little exponent help.

    This is the problem that I am currently working on:
    ***Note: Since I don't know if I can use {sup}{/sup} and html on here, I will simply type my exponents in graphing calculator format.. like 5 ^ 3 = (5*5*5)=125 ***

    "Write an interactive C++ program to calculate the following summation using an iteration,

    Sum = 1 + 1/2 + 1/4 + ... + 1/2n (note: the n is an exponent for the denomenator, as 1/(2^n) )
    Your program should prompt for the value of n. Please show your results for n = 10.
    The series above has a closed form analytical solution, namely,

    Sum(n) = 2 - 1/2n = 1 + 1/2 + 1/4 + ... + 1/2n (note: the n is an exponent for the denomenator, as 1/(2^n) )


    In the output for your C++ program, compare the iterative solution to the analytical solution. "



    I'm not exactly sure what to do...While poking around google, I found solutions to base/exponent problems. If I follow these, I think I may need <math.h> or <cmath> in order to do exponents with the "pow" command. Upon reviewing my problem, however, I began to wonder if maybe I need a loop or something. My teacher was explaining this problem on the board, and told us that the ... in "1 + 1/2 + 1/4 + ... + 1/2n " meant something like 1/(2^0) + 1/(2^1) + 1/(2^2) + ... + 1/(2^n) . Have any thoughts/tips/suggestions?
    As always, any help would be greatly appreaciated.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You are on the right track.
    1/(2^0) + 1/(2^1) + 1/(2^2) + ... + 1/(2^n)
    Simply have a loop that starts at zero and goes to (and includes) n. Have a variable to keep track of the sum. Use pow() to do the actually calculation.

    You could also search google for "sum finite series" and you'll get a bunch of pages that can show you formulas that wouldn't require a loop.
    Last edited by Thantos; 10-18-2004 at 01:25 PM. Reason: noticed a spelling mistake

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    15
    Ok...I sort of see...

    however, I am having trouble understanding a basic principle of this problem. You have this little set of things you need to do..in this case, the "1/(2^0) + 1/(2^1) + 1/(2^2) + ... + 1/(2^n) "

    Because I am a noob/can't understand directions, I have no idea as to how I would implement this into code. How would you go about doing this? If you wish, you can make up a problem/solution that is somewhat similar.

    Thank you for sharing your expertise.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    I'm just wondering what type of class this is, and how much math have you had? Anyway this is not a very hard problem (unless you have not covered loops).

    Code:
    double sum = 0;
    
    for(int i = 0, n = 10; i <=n; i++)
    {
         sum += 1 / pow(2,n);
    }

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    in this case, the "1/(2^0) + 1/(2^1) + 1/(2^2) + ... + 1/(2^n) "
    If that's all you're trying to do, consider this: The only number that's changing in each term is the exponent, and it's increasing with a fixed difference of 1. This suggests that you could use a loop, in this case a for loop since you need an increasing counter. So each term in the series is:

    1/(2^x), where x is an integer from 0 to n.

    The for loop would then be constructed as follows:
    Code:
    for(int x = 0; x <= n; x++) //'x' runs from 0 to n
    {
    //add 1/(2^x) to a running total
    }
    To calculate an exponent, you'll need to include <cmath> and use the pow() function (check it out at http://www.cppreference.com under the math library), since ^ isn't exponent in C++. I'll leave the exact code for adding to the 'running total' to you.

    And, as Thantos suggested, you could also look up 'sum of finite series' on google or something, there's a nice formula for it that doesn't require a loop, although I don't care to derive it right now and type it out in a textbox

    **EDIT**
    *batters the wall with head* neop, we try to give hints and partial solutions to help in the right direction, without giving an outright full code solution. The philosophy is, give a person a fish and you feed them for a day.. teach them to fish, and feed them for life.
    Last edited by Hunter2; 10-19-2004 at 10:41 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    Hey, if they don't want to learn it fine.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually so far I'd have to say the OP is doing a good job. Granted the request for code is a little much but they at least stated the question / problem clearly. If they had kept on their current course I'm sure we could have guided them into a proper solution.

    Hey, if they don't want to learn it fine.
    If you insist on that attitude I suggest you leave.

  8. #8
    Banned
    Join Date
    Oct 2004
    Posts
    15
    Thank you very much for your help. I think I understand it now.

    Are you not allowed to ask people to show you how to do something on these forums? I am sorry if I broke the rules by asking. However, I am terrible at "picking up" on sublime suggestions and hints. I am a slow person, and you have to show me something STEP BY STEP in EXCRUCIATING DETAIL before it can stick to my dense head. Also, I suck at math. I haven't taken calculus yet, so I didn't know exactly how that whole "1, 2 ,3 ... n" thing worked. And in my programming class..if you ask for help, you can't get a simple answer. Instead, he goes up to the board, and spends 15 minutes talking about stuff. I get lost easy. So, I turn to forums. If I'm not supposed to ask for help on here, then please tell me.


    Thank you very much for your assistance, I am appreciative.
    Last edited by phosphorousgobu; 10-19-2004 at 06:57 PM. Reason: Ug...previewing post is a good idea

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by phosphorousgobu
    Are you not allowed to ask people to show you how to do something on these forums?
    Asking for examples doesn't seem to be bad. I think it's if someone came on and said "do my homework for me" that things get ugly.

    And congradulations on understanding.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Are you not allowed to ask people to show you how to do something on these forums?
    That's what these forums are here for We just don't like when people ask someone to do all their homework for them.. but it's fine, as long as you show that you've done some work and some thinking yourself, which in this case you have. So, no, you haven't broken any rules

    It's neop that we're complaining against, because he/she decided to simply write the whole code for it, without explaining anything.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Banned
    Join Date
    Oct 2004
    Posts
    15

    Lookee here...

    Code:
    #include <cmath>
    #include <iostream>
    
    int main()
    
    {
    	double sum = 0;
    	int n;
    	std::cout << "Enter value for n: \n";
    	std::cin >> n;
    	for(int i = 0; i <=n; i++)
    	sum += 1 / pow(2,n);
    	std::cout << "Answer is " << sum << "\n";	
    	return (0);
    }
    If I execute this (in Visual Studio 6.0) and enter "2" as my n value, I get a return of "0.75". Is this correct?

    I'm still not sure what the heck I'm doing. From what I gleaned from the problem, you are starting off with 1. You keep dividing the variables by two until you reach n, which is actually 1/2^n. Let's look at what I just did. If I enter "2" as my n variable, what theoretically happens? Is it...
    1 + 1/2 + 1/2^n ?
    Since I put "2" as my n, I am understanding that it will go from the beginning up to where 2^2 (or 4) would be
    1 + 1/2 + 1/4 = 1.75 (on a graphing calc).

    There are either 1 of two things wrong...1). I don't understand what I'm doing and this is why I don't get the results, or 2). The code simply needs to be tweaked.


    Thank you all very much for your patience and help.

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    	for(int i = 0; i <=n; i++)
    	sum += 1 / pow(2,n);
    should be
    Code:
    	for(int i = 0; i <=n; i++)
    	    sum += 1 / pow(2,i);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  3. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  4. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM
  5. I want nothing more than to take calculus
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 61
    Last Post: 06-20-2003, 04:23 PM