Thread: inclusive addition problem

  1. #1
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14

    Question inclusive addition problem

    I have been trying to figure this one out for at least a day and a half of brainstorming, and am stumped. I am trying to create a program that will add start, end and all numbers in between
    ex. the user imputs 3 and 6, the program does this 3+4+5+6=18.
    I have the beginning code completed but am trying to find a small string of code that will do the addition for me,
    Code:
    #include <iostream.h>
    
    int main()
    {
    int start,end;
    	cout<<"Please Insert Start Number"
    		<<endl;
    			cin>>start;
    				cout<<"Your Start Number Is Now "<<start
    					<<endl;
    	cout<<"Please Insert End Number"
    		<<endl;
    			cin>>end;
    				cout<<"Your End Number Is Now "<<end
    					<<endl;
    	return 0;
    }
    that's what i have so far

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I will give you some pointers, because I think you should be able to create this on your own.

    You'll need a loop obviously, to accumulate the numbers. The loop starts at 'start' and ends at 'end'. It increments by one each iteration of the loop. I'm sure you can guess what kind of loop I'm talking about here. In each iteration of the loop, add the current loop value to the accumulated value. Have a shot and post up what you get.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    ok, i will, i'll try the loop thing, but need one last piece of advice.
    how do i get the loops to accumulate?
    i am not wanting to burden you with such ignorant problems but would like to know how to get the value to acumulate.
    do i need a value outside the loop?, and if so, how do i specify how many times the loop loops?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You should only need two variables, one for start and one for end. You can use a for loop:
    Code:
    //for loop ex.:
    for (int i = 0; i<100; i++) //will run until i is equal to 100...it increments i each time it loops
    In your loop make the i value equal to (start+1) and then just add on the i value each time it loops (start+=i
    Your condition (i<100) should test whether i < end (or end + 1 if you include the last value)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Note that in Jawib's for loop, the variable i will take 100 consecutive values, 0 to 99. The values that you want your loop to take is start to end, for example, 3 to 6. Base your for loop on the one given by Jawib, but change the loop values accordingly.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    I have read the last two posts and find them somewhat helpful, but need a sample of this code. I am pulling my hair out trying to implement this oodle of commands!, Please help!

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok, here you go.
    Code:
    int nAccumulate=0;
    
    cin >> start;
    cin >> end;
    
    
    for (int i=start;i<=end;i++)
        nAccumulate+=i;
    What happens here is the value i loops from start to end. So, if you enter in 3 and 6, the line nAccumulate+=i runs 4 times. The first, time, i=3. Second time, i=4. Third time, i=5. Fourth time, i=6. Each time, we increment the value of nAccumulate by the value in i. Capeesh?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    I also need to output the final value as an integer, so every time the loop runs it needs to ad it's calculation to a total, so when the user imputs the data, they get an answer at the end, as in:

    Code:
    enter start number
    3
    your startnumber is 3
    *************
    insert end number
    4
    your end number is 4
    ***************
    //and then at the end it should display the answer
    the final answer is: 7
    that's it

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    cout << "The answer is: " << nAccumulate << endl;
    cout << "Have a nice day" << endl;
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What don't you understand?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    so the nAccumulate+=i; adds the value to the total?

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    nAccumulate+=i;

    is the same as saying:

    nAccumulate=nAccumulate+i;

    I just don't like typing things out a lot.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    THX VERY MUCH FOR ALL YOUR HELP, attached is the final .cpp if you want to check it out!

  14. #14
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    This is the more refined version of my program :-0

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    There is a faster way to get the result, however. You accumulate through all the integers, which is sloooooow, especially on large ranges. The algorithm you use takes linear time (denoted O(N), which means if you double the range, you double the running time.) O(N) is too slow for large ranges.

    The below algorithm should yield the same results, but is constant time (the time does not increase for larger ranges):

    sum = ( (max + min)*(max - min + 1) ) /2;

    I think this works for all ranges. It is important that you do the multiplication before the division (which is why I added a unnecessary set of ()s), because you don't want roundoff error. For any range, either (max + min) or (max - min +1) will be even, so after the multiplication, your division is always precise.

    This also is an attractive algorithm, because it uses only one multiplication (and you can't rewrite a large summation without at least one multiplication), plus the only division is a divide by 2, which is easy for computers.

    The way you would derive this is by breaking numbers into pairs. For example, say I wanted to add the numbers 2 through 100.

    2 + 3 + 4 + 5 + ... + 100

    Now, let me write them in another order (which is perfectly legal for any sum of a finite set of numbers, and legal for certain sums of infinite sets of numbers):

    (2 + 100) + (3 + 99) + (4 + 98) + ... + (50 + 52) + 51

    Note, that except for our unpaired 51, each sum in parentheses works out to 102 (which is the sum of our min and max). Further, 51 is exactly 1/2 of 102.

    So, how many 102's are we adding together? Well, we started out with 99 numbers (100 - 2 + 1), so we made 49 full pairs, and one half of a pair -- wait, 49.5 is just 99/2.

    So 2 + 3 + 4 + 5 + ... + 100 = (100+2) * (100 - 2 + 1) / 2

    In this example, I used an odd # of summed numbers -- I leave it as an exercise to prove this works for an even # of summed numbers as well. The proof is exactly the same, except you don't have the one odd half of a sum.
    Last edited by Cat; 10-02-2003 at 10:17 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. String Addition Problem...
    By Comrade_Yeti in forum C++ Programming
    Replies: 16
    Last Post: 01-15-2007, 04:01 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM