# C version of "accumulate" ?

This is a discussion on C version of "accumulate" ? within the C Programming forums, part of the General Programming Boards category; hello there, I'm new to these boards, and also very new to programming, so I hope you could help me ...

1. ## C version of "accumulate" ?

hello there, I'm new to these boards, and also very new to programming, so I hope you could help me out

I'm currently writing a program (in C) that requires me to sum the values of a function from 2 up to (N-1).

now I've had a search on the net and come across this "accumulate" thing, which seems to do exactly what I want! one problem though, it seems to be a C++ command, rather than one for C.

now I know how to get a loop to run from 2 up to (N-1) by doing something similar to this;

Code:
```
{
for (i=2; i<=(N-1); i++);
/* b is the sum of all values of f(xi) from i=2 to i=(N-1) */
{
f(xi);
}
}```
(and yes, that doesn't work as it is but hopefully it'll give an idea of what I want to do)

but I really dont know how to get every consecutive value of my function to sum together... Am I going to have to input every value of the loop into some sort of array to get around this problem...?

Also, dont know whether this will make a difference, but I'm going to have to take N up to as large a value as possible to give a vague approximation of infinity

2. > for (i=2; i<=(N-1); i++);
Well you need to remove the ; at the ens of this line.

sum = sum + i;

Originally Posted by Salem
> for (i=2; i<=(N-1); i++);
Well you need to remove the ; at the end of this line.
see, I told you I was new to this oops! thanks for pointing it out...

as for the

" sum = sum + i; "

what I need is

sum = f(x2)+f(x3)+...+f(x(N-1))

so, the sum of all values of f(xi) from i=2 to i=(N-1)

if that makes sense

4. and what exactly f(x2), f(x3) is, is that some function or what???
If you need a sum from 2 to N-1, use as Salem said, sum+=i;
because as the "i" changes, "i" becomes the right number you need to add to your sum.

IMPORTANT, you have to set sum as 0, or eitherway it would produce some unwanted solutions, unless you declared sum ad global.

5. Well, variables are made up of a certain number of bytes. For instance, an int is generally 4 bytes (32 bits) so the largest number it can hold before overflowing is 2^32-1.

If you forget about the large number support, you could do something like this:
Code:
```itsme@dreams:~/C\$ cat accumulate.c
#include <stdio.h>

int accumulate(int n)
{
int sum = 0;
int i;

for(i = 2;i < n;++i)
sum += i;

return sum;
}

int main(void)
{
int n;

do
{
printf("Enter number: ");
fflush(stdout);
scanf("%d", &n);

if(n < 3)
puts("Please enter a number greater than 2");
} while(n < 3);

printf("accumulate(n) = %d\n", accumulate(n));

return 0;
}```
Code:
```itsme@dreams:~/C\$ ./accumulate
Enter number: 9
accumulate(n) = 35
itsme@dreams:~/C\$ ./accumulate
Enter number: 2934848
accumulate(n) = -1187273761```
You can see the overflow in action with the second run.

If you want to be able to have large numbers you'll have to use some tricks. Something like this:
Code:
```itsme@dreams:~/C\$ cat accumulate2.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXLEN 4096

void big_add(char *s1, char *s2, char *buf)
{
char temp[MAXLEN];
char *p, *q, *t;
int num, carry = 0;

p = s1 + strlen(s1) - 1;
q = s2 + strlen(s2) - 1;
t = temp;

while(p >= s1 || q >= s2)
{
num = carry;
num += p >= s1 ? *p - '0' : 0;
num += q >= s2 ? *q - '0' : 0;

if((carry = num / 10))
num -= 10;

*t++ = num + '0';
p--;
q--;
}

if(carry && t - temp < MAXLEN)
*t = '1';
else
t--;

while(t >= temp)
*buf++ = *t--;
*buf = '\0';
}

char *accumulate(char *n)
{
static char sum[MAXLEN];
char ctr[MAXLEN] = "2";
char buf[MAXLEN];

strcpy(sum, "0");

while(strcmp(ctr, n))
{
strcpy(sum, buf);

strcpy(ctr, buf);
}

return sum;
}

int main(void)
{
char input[MAXLEN], *p;

for(;;)
{
printf("Enter number: ");
fflush(stdout);
fgets(input, sizeof(input), stdin);

for(p = input;isdigit(*p);p++)
;
*p = '\0';

if(p - input == 1 && input[0] < '3')
puts("Please enter a number greater than 2");
else
break;
}

printf("accumulate(n) = %s\n", accumulate(input));

return 0;
}```
Code:
```itsme@dreams:~/C\$ ./accumulate2
Enter number: 5
accumulate(n) = 9
itsme@dreams:~/C\$ ./accumulate2
Enter number: 7234724
accumulate(n) = 26170612060725```
EDIT: I'll let you figure out how to add the f() function

6. ok, so f(x) is for all intensive purposes, an arbitrary function dependant on x.

x however, depends on i, and N, the latter of which is predefined.

f(xi) represnts the general i'th term of this function, where as f(x(N-1)) represents the function at i=(N-1), and f(x3) would represent the function at i=3.

sorry if this was not clear before!

and I need to sum each of the values of this function f(xi) from where i=2, to where i=(N-1).

reading through your replies, I think I understand... that I can write,

sum+=f(xi)

in the middle of a loop, so every iteration of the loop will add to the previous values, correct?
thanks for your help! something so small, but hard to get my head around for a newbie!

7. just out of interest itsme86, what would be the largest number you could use in your second program?

is it larger than the max size of a long double? (~ 1.189*10^4923)*

as I have an infinity as the value of one of the variables in one of the equations I'm using, and I'm thinking of just sticking the largest number possible in there!

*note, this may be wrong

8. There is no limit with my second program. Right now it can have up to 4095 digits, but it's a simple matter of changing how big MAXLEN is and recompiling the program to make it take an even larger number.

9. Originally Posted by itsme86
There is no limit with my second program. Right now it can have up to 4095 digits, but it's a simple matter of changing how big MAXLEN is and recompiling the program to make it take an even larger number.
ahh, I see, thanks for that

10. lol itsme, i think unbeliever wont understand that second code as he's a new to programing and probably dont know how to use pointers

11. Originally Posted by XZminX
lol itsme, i think unbeliever wont understand that second code as he's a new to programing and probably dont know how to use pointers
very true, but it's still good to look at, and plus I have a few reference books and someone nearby who probably will understand it, so it's all good

12. Originally Posted by Unbeliever
f(xi) represnts the general i'th term of this function, where as f(x(N-1)) represents the function at i=(N-1), and f(x3) would represent the function at i=3.

and I need to sum each of the values of this function f(xi) from where i=2, to where i=(N-1).

Here is a general approach to whatever it is that I think you are getting at:
Code:
```#include <stdio.h>

int main()
{
double f(double);

/* the next three statements will define the points on the interval of interest */
double xmin = 0.0;
double xmax = 2.0;
int NumPoints = 11; /* so you have point numbers 0 to 10 */

/* everything from here onward depends on the first three things */
int NumIntervals = NumPoints - 1;
double deltax = (xmax - xmin)/(double)(NumIntervals);
double x;
double sum;
int i;

printf("xmin = %f, xmax = %f, Number of points = %d\n", xmin, xmax, NumPoints);
printf("Number of Intervals = %d, deltax = %f\n\n", NumIntervals, deltax);

x = xmin + 2*deltax;
sum = 0.0;

for (i = 2; i < NumPoints; i++) {
sum += f(x);
printf("i = %2d, x = %f, sum = %f\n", i, x, sum);
x += deltax;
}

printf("\n");
printf ("sum = %f\n", sum);
return 0;
}

double f(double x)
{
double value;
/* calculate whatever function of x you need. For example: */
value = x / 2.0;
return value;
}```
The idea is to start with a small number of points. Print out all of the values so you can verify that the program is doing the right thing over the right range. Once you have seen that it is working OK, make the number of points whatever you want, and comment out the superfluous printf() stuff.

Regards,

Dave

13. Originally Posted by Unbeliever

is it larger than the max size of a long double? (~ 1.189*10^4923)*

*note, this may be wrong
10^4923 ??? where did you get that from? something around 10^37 is more likely..

14. Originally Posted by viaxd
10^4923 ??? where did you get that from? something around 10^37 is more likely..
just on a sheet I have from my programming notes.

that said, the max float value was 10^38, with 32 bits
then double at 10^308 with 64 bits
then long double at 10^4932 with 128 bits for (SPARC)(powerPC), or 80 bits for intel.

not really sure what it all means, dont worry about it though, I'm not that good at this stuff

15. that explains it then if it's 128 bits. see, the more bits a type uses, the more it can hold. and with 128 bits you can hold a pretty huge number.