Jump to content

Summation function.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
This is a function that returns the sum of all outputs from a function f, for all integers from m to n:
double summf(int m, int n, double (*f)(int)){

 double t = 0.0;

 int i = m;

 while(i != n)

  t += (*f)(i++);

 return t;

}
Here is one that gives the summation of an array of doubles:
double summa(int n, double * a){

 double t = 0.0;

 int i = 0;

 while(i != n)

  t += a[i++];

 return t;

}
And finally, here is one that applies a function to the elements of an array:
double summaf(int n, double * a, double (*f)(double)){

 double t = 0.0;

 int i = m;

 while(i != n)

  t += (*f)(a[i++]);

 return t;

}
What do you think?

#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
What language? And do they all go into the one file or not?
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#3
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
These summation functions are in C. The idea is that you pass a pointer to a function or array, and it does the sum of the results from passing m to n. Here:

#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Oh, nice work mate :) Thanks for clearing it up Aere.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!