Thread: C version of "accumulate" ?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    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

    thanks very much for any help you can offer

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (i=2; i<=(N-1); i++);
    Well you need to remove the ; at the ens of this line.

    How about for the rest
    sum = sum + i;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Thanks for your reply!

    Quote 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. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Varaždin, Croatia
    Posts
    11
    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. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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))
      {
        big_add(sum, ctr, buf);
        strcpy(sum, buf);
    
        big_add(ctr, "1", 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
    Last edited by itsme86; 03-31-2005 at 11:49 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    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!

    edit: just read your post itsme86, wow, thanks very much
    Last edited by Unbeliever; 03-31-2005 at 11:55 AM.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    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
    Last edited by Unbeliever; 03-31-2005 at 12:06 PM.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Quote 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. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Varaždin, Croatia
    Posts
    11
    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. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Quote 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. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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. #13
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Quote 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..
    :wq

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Quote 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. #15
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    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.
    :wq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. No Version info tab in file properties?
    By cpjust in forum Windows Programming
    Replies: 2
    Last Post: 06-03-2008, 03:42 PM
  3. How to set File Version of VC++ 6 dll
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 12-08-2006, 02:49 PM
  4. Finding the windows version...
    By The_Muffin_Man in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2004, 11:39 PM
  5. Dev C++ Version 5
    By Zoalord in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 01:56 PM