please explain output
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: please explain output

  1. #1
    Join Date
    Jun 2014
    Posts
    6

    please explain output

    Hi,

    on running program below - I get this as output

    9 5 11 1
    3 5 6 2
    3 11 9 1
    2 5 1 3
    5 6 9 1
    2 6 3 2
    2 9 5 1

    Code:
    void crazy(int a, int b, int m, int n){
         if(n == 0)
              return;
         crazy(a+m, b, m+b, n-1);
         printf("\n %d %d %d %d \n", a, b, m, n );
         crazy(a,b+m,m+a,n-1);
         }
    int main(){
        crazy(2,5,1,3);
    }
    Can anyone explain this for me ? Thanks !!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,470

    Re: please explain output

    Which line of the code snippet you have posted is unclear for you?
    In few words: function crazy is called recursively until its last paremeter will be zero. And function printf prints out the values of parameters passed into crazy.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: please explain output

    Quote Originally Posted by mithlesh View Post
    Hi,

    on running program below - I get this as output

    9 5 11 1
    3 5 6 2
    3 11 9 1
    2 5 1 3
    5 6 9 1
    2 6 3 2
    2 9 5 1

    Code:
    void crazy(int a, int b, int m, int n){
         if(n == 0)
              return;
         crazy(a+m, b, m+b, n-1);
         printf("\n %d %d %d %d \n", a, b, m, n );
         crazy(a,b+m,m+a,n-1);
         }
    int main(){
        crazy(2,5,1,3);
    }
    Can anyone explain this for me ? Thanks !!
    Sounds like homework. In any case, it's a simply recursive calls crazy in a tree like fashion, "in-order".

    Simply tracing your code with a pen and paper would help here:


    Code:
                                   +------>crazy(20,5,16,0);
                      +------>crazy(9,5,11,1);
                      |            +------>crazy(9,16,20,0);
                      |
         +------>crazy(3,5,6,2);
         |            |
         |            |            +------>crazy(12,11,20,0);
         |            +------>crazy(3,11,9,1);
         |                         +------>crazy(3,20,12,0);
    crazy(2,5,1,3);
         |
         |                         +------>crazy(14,6,15,0);
         |            +------>crazy(5,6,9,1);
         |            |            +------>crazy(5,15,14,0);
         |            |
         +------>crazy(2,6,3,2);
                      |
                      |            +------>crazy(7,9,14,0);
                      +------>crazy(2,9,5,1);
                                   +------>crazy(2,14,7,0);
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center