Skip to main content
gamedev.net
🔒 Locked

accumulate

Started by TEUTON Apr 12, 2006 at 9:12 PM 7 replies 1.1k views
Original Post
TEUTON
TEUTON
Is it possible to calculate sum of numbers from start to finish rather then sum of all elements of arrays through accumulate. Whenever I pass plain numbers to accumulate it gives me an error of illegal indirection(C2100). Or Is there any other inbuilt algorithm for this purpose?
Fruny
Fruny
My psychic powers tell me that your compiler hates you.

For a more accurate reading, please post the code you used.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
TEUTON
TEUTON
long int num1=0;long int num2=1000;long sum=0;accumulate(1, 10, sum, plus<int>());
Fruny
Fruny
Accumulate adds up array elements (sequence elements, really). The first two parameters should be pointers to the beginning and to one-past-the end of the array, respectively (or begin() and end() of a container, or delimiting the range you want to add up).

int array[5] = { 1, 10, 100, 1000, 10000 };int sum = 0;accumulate(array, array+5, sum, plus<int>());// sum == 11111
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
TEUTON
TEUTON
So, there is no other way to sum up the series?? No other inbuilt algorithm?
chowe6685
chowe6685
If you're just trying to calculate the sum of 1+2+3+....+N there's an explicit formula you can use. It can be derived fairly easily using induction
TEUTON
TEUTON
Basically I was looking for speed...that is why I was looking for some in-built function...
chowe6685
chowe6685
Like I said - you can derive an explicit formula or just google for it. The formula is just a polynomial in N - you can't get much faster than that.
xEricx
xEricx
From N1 to N2 seems pretty easy...

say we pick 5 and 18, if you match pairs you get 5 + 18, 6 + 17, 7 + 16, 8 + 15, 9 + 14, 10 + 13, 11 + 12, which means (N2 - N1 + 1 ) / 2 pairs... each pairs equals N1 + N2. Now, this is only if N2 - N1 + 1 is even, if its odd, you need to add the middle, say 5 and 9... 5 + 9, 6 + 8 and now 7... its (N2 + N1) / 2

there might be gazillions of ways to optimize it, but anyway

#include <iostream>int accumulate(int in_from, int in_to){    const int inner = in_to - in_from + 1;    const int pairs = inner / 2;    const bool isOdd = inner % 2 ? true : false;    const int pairValue = in_to + in_from;    return ((pairs * pairValue) + (isOdd ? (pairValue / 2) : 0));}int main(int ac, char *av[]){    std::cout << "accumulate " << accumulate(5, 10) << std::endl;    std::cout << "accumulate " << accumulate(1, 1) << std::endl;    std::cout << "accumulate " << accumulate(1,3) << std::endl;    return 0;}


which outputs 45, 1 and 6. Since its counting FROM -> TO, 1,1 returns 1, not sure if its your desired behavior.

Hope this helps

Eric

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.