Thread: Help with pointer to function problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65

    Help with pointer to function problem

    Hi everyone, a few months ago my professor gave us a problem which I didn't manage to solve, then I forgot about it but now I found it again and I'm still stuck with it, the problem is:
    given a prototype: int sum (int (*f) (int), int start, int end);
    /* Post sum(f,start,end)= i = start, i = start+1, .... i = end f(i) *\
    Implement recursively the funcion sum and show how it has to be called in order to output
    i = 1, ... i = N g(i) where g is a function which prototype is int g(int).


    I wrote this code:

    Code:
    #include <stdio.h>
    
    int g(int n) 
    {
    	return n*(n+1) / 2;
    }
    int sum(int (*f)(int), int start, int end)
    {	
    	if(start==end) return f(start);
    	return sum(f, start+1, end);
    }
    int main()
    {
    	int start, end, result;	
    	
    	scanf("%d%d", &start, &end);	
    	
    	result = sum(g, start, end);	
    	
    	printf("%d\n", result);	
    	
    	return 0;
    }
    However I'm not sure this is the right way to solve it, if someone can give me a few hints I would really appreciate it since i guess my professor is going to give us a similar problem in the next exam, thanks
    Last edited by rob90; 05-28-2010 at 07:41 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're only calculating the last value. When you do the recursion, you split your list up into (start) and (start+1, ..., end) -- you find the value of the last bit, but not the first bit.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Quote Originally Posted by tabstop View Post
    You're only calculating the last value. When you do the recursion, you split your list up into (start) and (start+1, ..., end) -- you find the value of the last bit, but not the first bit.
    Do you mean that I just have to modify the sum function this way?
    Code:
    int sum(int (*f)(int), int start, int end)
    {
    	if(start == end) return f(start);
    	printf("%d\n", f(start));
    	return sum(g, start+1, end);
    }
    So that for each value of start it outputs its summation? Thanks for the help anyway

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rob90 View Post
    Do you mean that I just have to modify the sum function this way?
    Code:
    int sum(int (*f)(int), int start, int end)
    {
    	if(start == end) return f(start);
    	printf("%d\n", f(start));
    	return sum(g, start+1, end);
    }
    So that for each value of start it outputs its summation? Thanks for the help anyway
    No, I mean you need to actually add it up.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Here:
    Code:
    int g(int n) 
    {
    	return n;
    }
    int sum(int (*f)(int), int start, int end)
    {	
    	if(start==end) return f(start);
    	return f(start) + sum(f, start+1, end);
    }
    Am I right?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That looks a lot more like adding.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    int sum(int (*f)(int), int start, int end)
    {	
    	if(start==end) return f(start);
    	return sum(f, start+1, end);
    }
    This will just return the original value of f(end), ie. it's meaningless. I presume you want to accumulate a value somehow (what does that big greek letter mean?* ), either by using "return start + sum(....)" or else via a static variable inside sum.

    * seriously, in fact -- anyone know of anywhere where all those symbols are listed with their meaning?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    * seriously, in fact -- anyone know of anywhere where all those symbols are listed with their meaning?
    They are just letters, the meaning can differ with context.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    I was just using Gauss formula in order to calculate the summation of end without actually adding each value using recursion, I understand it now, thanks tabstop for the help
    Last edited by rob90; 05-28-2010 at 08:43 AM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    They are just letters, the meaning can differ with context.
    Hmmmph. So I would actually have to study formal math or sumthin', is what yer saying.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Heh, not quite. There are some that have a commonly accepted meaning if there is a lack of context clues. Sigma usually means sum for example. But most symbols are just that, without any inherent meaning beyond what they are given in a particular situation.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Hmmmph. So I would actually have to study formal math or sumthin', is what yer saying.
    There's always Greek letters used in mathematics, science, and engineering - Wikipedia, the free encyclopedia

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Well I'll be -- thanks tabstop. So in this case the epsilon indicates a set with a definable order to it?
    Last edited by MK27; 05-28-2010 at 09:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Quote Originally Posted by MK27 View Post
    Well I'll be -- thanks tabstop. So in this case the epsilon indicates a set?
    Haha and looking at that list you see my point ^_^

    I haven't seen even a tenth of those uses for any of those symbols in my life.

    o in this case the epsilon indicates a set?
    Epsilon? If you mean the symbol in the first post, it is Sigma, and it represents a sum from i = 1 to N of some function g(i) a far as I can make out, though the OP's use of the symbol is a little confusing, so I could be off the mark there.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It can actually mean a sum from anything to anything, not even necessarily in discrete steps. All of these details are usually specified underneath and above the sigma.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:41 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM