this program using recursion is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aniruddha123
    New Member
    • May 2013
    • 1

    this program using recursion is not working

    this is the question:: Problem 2: Find the Numbers, (K Narayan Kumar, CMI)
    This is a rather simple problem to describe. You will be given three numbers S, P and k. Your task is to find if there are integers n1, n2,...,nk such that n1 + n2 +...+ nk = S, n1 * n2 * ... * nk = P. If such integers exist, print them out. If no such sequence of integers exist, then print "NO".
    For example if S=11, P=48 and k=3 then 3, 4 and 4 is a solution. On the other hand, if S=11, P=100 and k=3, there is no solution and you should print "NO".
    Input format
    A single line with three integers S, P and k.
    Output format
    </>
    A single word "NO" or a seqence of k integers n1, n2,..., nk on a single line. (The ni's must add up to S and their product must be P).
    Test data
    You may assume that 1 = k = 4, 1 = S = 1000 and 1 = P = 1000.
    Example
    We now illustrate the input and output formats using some examples.
    Sample input 1:
    11 48 3
    Sample output 1:
    3 4 4
    Sample input 2:
    11 100 3
    Sample output 2:
    NO





    here is my program::
    /*NOT WORKING----program to find the numbers using recursion*/
    Code:
    #include<stdio.h>
    int main()
    {
        int s,p,a=0,b=0,c=0,match=0;
        printf("\nEnter Sum & Product: ");
        scanf("%d %d",&s,&p);
        recursive (s,p,&a,&b,&c,1,&match);
                if (match==1)
                    printf("\nThe numbers are %d %d %d",a,b,c);
                else
                    printf("\nNumbers are not found.");
        return 0;
    }
     recursive (int s,int p,int *a,int *b,int *c,int level,int *match)
    {
       int i=0;
       printf("\nlevel=%d\t\ta=%d,b=%d,c=%d",level,*a,*b,*c);
        if (level==3)
          {
              if (s==p)
                {
                    *c=s;
                    *match=1;
                }
            return;
          }
    
         if (*match==1)
            {
                 return;
            }
    
             //printf("\ndebug");
        for (i=2;i<p/2;i++)
            if (p%i==0)
                {
                  //printf("\ncase level=%d\t\ti=%d,p=%d",level,i,p);
                   if (level==2)
                     *b=i;
                     if (level==1)
                            *a=i;
                    p=p/i;
                    s=s-i;
                    //printf("\nlevel=%d\t\t,p=%d,s=%d",level,p,s);
                    recursive (s,p,a,b,c,level+1,match);
                }
            if (i>=s)
                return;
    }
    Last edited by Rabbit; May 19 '13, 07:47 PM. Reason: Please use code tags when posting code.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    One problem I can see right away is that you're not scanning for k (which should be the third number given) and you therefore don't use it in the calculation. From what I understand, k should be the number of numbers used for the product and sum, so it's relevant.
    Instead, you pass 3 variables exactly (a, b and c). The right way to do this would be to either use an array or (to go "full recursion") pass no variables at all but rather have the recursive function have one local variable which it prints.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      What Nepomuk says is not quite right. 'k' is not required to find the factors of P using tail recursion. For example 48 yields factors 2 2 2 2 3 which sums to eleven as required. k=3 says combine the first two and second two factors to produce 3 4 4 which again sums to eleven.
      Here is some code which find factors which may assist in finding your own errors.

      Code:
      #include<iostream>
      
      using namespace std;
      
      int main()
      {
      cout<<"This program factors a number\n";
      int n ,sum=0;  //the number to factor
      while(n)
        {
          cout<<"\nEnter an integer (-1 to quit) ";
          cin>>n;
          if(n==-1)return 1;
          cout<<"The factors of "<<n<<" are ";
          int lastFactor = 2;
             while( lastFactor < n )
               {
                      if( n % lastFactor == 0 )
                   {       cout << lastFactor << ' ';
                           n /= lastFactor;
      
                   } else
      
                     ++lastFactor;//tail recursion
      
               }
          if( n != 1 )
          cout << n;
      
      
        }
      
      
      cout<<endl;
      
      cin.get();
      return 0;
      }
      /*This program factors a number

      Enter an integer (-1 to quit) 48
      The factors of 48 are 2 2 2 2 3
      Enter an integer (-1 to quit) -1

      Process returned 1 (0x1) execution time : 12.672 s
      Press any key to continue.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Uhm whodgson... We seem to have very different ideas about what recursion is - for one thing, your solution only has one function (the main function) which doesn't call itself but rather uses loops to find a solution. That is what I would call an iterative solution. How exactly that is supposed to be tail recursive I don't know...

        Also, I didn't say that k necessarily had to be passed to the recursive function (though with the solution I had in mind it would be); I said it was relevant. For that reason, it HAS TO at least be scanned and used in some way. Passing exactly three variables for the output means, that the solution would have a fixed k=3 which is not what the question asks for.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          From Nepomuk's last post.
          "it HAS TO at least be scanned and used in some way."
          Why?... if it is not required to establish the value of P or S it must be redundant or similar.
          By the way tail_recursiven ess is explained (defined) in the reference you provided which is said to be allied to the iterative concept and only a special case of the function which calls itself.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by whodgson
            Why?... if it is not required to establish the value of P or S it must be redundant or similar.
            From the original post:
            Originally posted by Aniruddha123
            You will be given three numbers S, P and k. Your task is to find if there are integers n1, n2,...,nk such that n1 + n2 +...+ nk = S, n1 * n2 * ... * nk = P.
            So, we want exactly k numbers as a result. How do you want to do that if you don't even know what k is?
            Originally posted by whodgson
            By the way tail_recursiven ess is explained (defined) in the reference you provided which is said to be allied to the iterative concept and only a special case of the function which calls itself.
            Actually, no. It sais:
            Originally posted by Wikipedia
            Tail-recursive functions are functions in which all recursive calls are tail calls…
            Now, tail calls can be used in the iterative paradigm. But tail recursion is the combination of tail calls and recursion. And pretty early in the recursion article it sais:
            Originally posted by Wikipedia
            Most computer programming languages support recursion by allowing a function to call itself within the program text. Some functional programming languages do not define any looping constructs but rely solely on recursion to repeatedly call code.
            If you had ever used one of the languages mentioned (e.g. Haskell) you would really understand the difference - if you want to learn functional programming I can only recommend it. But that's beside the point here.
            The task here is clearly to write a recursive program (as stated by the title) and the op is asking what is wrong with his code. So I personally will be getting back to that problem.

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              Aniruddha123,

              As Nepomuk observed, you aren't reading the value of k in your code, so you do not know how many factors to resolve your sum and product into. This is the first thing you should fix.

              Secondly, your function recursive does not take k as an input. It seems that the parameter level takes the place of k, but it is forced to the value three (3) in your main.

              Thirdly, it looks like the internal structure of your function strictly looks to a depth of three (3). Which is not what the specification was. So, regardless of your top-level problems, you will need to work out a better recursive algorithm, once you get the top-level details sorted out.

              Regards,
              Oralloy
              Last edited by Oralloy; May 22 '13, 04:51 PM. Reason: hit reply, instead of preview, finishing now...

              Comment

              Working...