Thread: Request Explanation (For Understanding)

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    1

    Request Explanation (For Understanding)

    Code:
    #include <stdio.h>
    #define MAX 5
    
    int func(const int a[], size_t b);
    
    int main(void)
    {
      int c;
      int p[MAX] = { 1, 2, 3, 4, 5 };
      c = func(p, MAX);
      printf("Result is %d\n", c);
    }
    
    int func(const int a[], size_t b)
    {
      if (b == 1)
        return a[0];
      else
        return a[b - 1] + func(a, b - 1);
    }
    Could someone explain to me how this code gets to print the value 15?
    I'm quite new to C-Programming and I'm keen to understand how the code works.

    Thank You!
    Last edited by Salem; 10-30-2018 at 02:16 AM. Reason: Please use copy as text / paste as text. Removed crayola formatting.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post the code as plain text in code bbcode tags, with only whitespace for the formatting rather than any additional markup. The forum software will then insert syntax highlighting and line numbering to make the code easier to read:
    Code:
    #include <stdio.h>
    
    #define MAX 5
    
    int func( const int a[ ], size_t b );
    
    int main( void )
    {
        int c;
        int p[MAX] = {1, 2, 3, 4, 5};
        c = func( p, MAX );
        printf( "Result is %d\n", c );
    }
    
    int func( const int a[ ], size_t b )
    {
        if( b == 1 )
            return a[0];
        else
            return a[b-1] + func( a, b-1 );
    }
    Quote Originally Posted by asiankappa47
    Could someone explain to me how this code gets to print the value 15?
    Let's say you want to write a function to sum the elements of an integer array. You could of course write a loop and keep track of the current sum, but what if you were lazy and had access to another function that would sum the elements of the array, except for the last element? Then your job is easy: you just call that other function, then add its return value to the last element, and tada! return the result and you're done. Of course, if your array only has one element, you don't need to call that other function, but can just return that one element itself. This is what func is doing, except that "other function" is func itself, i.e., recursion.

    Of course, this is a somewhat more high level view of what func is about. It would still be instructive for you to trace through what is happening step by step yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > return a[b - 1] + func(a, b - 1);
    What can you say about this line?

    You can rewrite it to add more information you know, or just step through the code with a debugger.


    Code:
    int func(const int a[], size_t b)
    {
      if (b == 1) {
        printf("Simple case, answer=%d\n", a[0] );
        return a[0];
      } else {
        int x = a[b - 1];
        int y = func(a, b - 1);
        printf("x=%d, y=%d, b=%d\n", x, y, b );
        return x + y;
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 07-20-2018, 10:19 PM
  2. Sorry, one more request for help
    By ericdabear in forum C Programming
    Replies: 3
    Last Post: 03-15-2010, 12:56 PM
  3. Request>>>
    By planet_abhi in forum Game Programming
    Replies: 1
    Last Post: 10-06-2003, 08:55 AM

Tags for this Thread