CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2022
    Posts
    11

    Basic Dynamic Programming code

    Hello
    i'm just trying to do some basic example of dynamic programming and i've an issue with the following code:

    Code:
    #include <cstdio>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    int cut_rod(vector<int>p,int l){
      int m;
      if (l==0) return 0;
      int q=-32768;
      for (int i=1;i<=p.size();i++){
        if ((l-i)<0) continue;
        m=p[i-1]+cut_rod(p,l-i);
        if (q<m) q=m;
      }
    return q;
    }
    
    int main(){
      int n,t;
      int tstart;
      int tend;
      tstart=clock();
      scanf("%i %i\n ",&n,&t);
      int q;
      int l;
      vector<int> p;
      
        for (int j=0;j<n;j++){
          p.push_back(0);
          scanf("%i",&p[j]);
        }
      for (int i=0;i<t;t++){
        scanf("%i",&l);
        q=cut_rod(p,l);
        printf("Case %i: %i \n",i,q);
    
      }
      tend=clock();
      printf("Elapsed time : %i",tend-tstart);
      return 0;
    }
    The program go crazy also for examples with few iteration like:

    Code:
    2 1
    3 4
    3
    What's the problem?
    Last edited by Quasar999; March 26th, 2022 at 10:03 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Basic Dynamic Programming code

    Use the debugger to trace through the code and watch the variables. When the debugger shows behaviour different from that expected from the design then you have found an issue.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Basic Dynamic Programming code

    In addition to what 2kaud said, you should learn to write readable code. Single character variable names, and multiple statements on the same line make for code that's both hard to read and debug.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Basic Dynamic Programming code

    ... and also a mix of C++ and C !
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Mar 2022
    Posts
    9

    Re: Basic Dynamic Programming code

    thanks for this, I had a similar doubt with this one. I also want my kid to learn coding that is why I'm planning to enroll him to QRangers and take the Coding For Kids course.
    Last edited by MIa White; May 1st, 2022 at 10:55 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured