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;
<<endl;
cin>>start;
cout<<"Your Start Number Is Now "<<start
<<endl;
<<endl;
cin>>end;
cout<<"Your End Number Is Now "<<end
<<endl;
return 0;
}```
that's what i have so far

2. 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.

3. 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. 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)

5. 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.

6. 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. 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?

8. 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
*************
insert end number
4
***************
//and then at the end it should display the answer
that's it

9. Code:
```cout << "The answer is: " << nAccumulate << endl;
cout << "Have a nice day" << endl;```

10. What don't you understand?

11. so the nAccumulate+=i; adds the value to the total?

12. nAccumulate+=i;

is the same as saying:

nAccumulate=nAccumulate+i;

I just don't like typing things out a lot.

13. THX VERY MUCH FOR ALL YOUR HELP, attached is the final .cpp if you want to check it out!

14. This is the more refined version of my program :-0

15. 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.