Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help please Memoized rod cutting in C while showing the length it is cut

Help please Memoized rod cutting in C while showing the length it is cut

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12763086
    wrote on last edited by
    #1

    r[1000]; *r = (int *) malloc ((n+ 1) * sizeof(int)); but if I use int *r then also it doesn't passes properly .Should I use structure? The problem should take input like division-3 input of price-1,5,8 and give rod cutting whereas there are three iteration which should be one r=0; and the maximum price for this division should be 8 but the result comes 1677218

    #include
    #include
    #include
    #include
    int sum=0;

    int max(int a, int b)
    {
    return(a>b)?a:b;
    }
    int cut_rod_aux(int price[], int n, int r[])
    { int i;
    if(r[n]>=0)
    {
    return r[n];
    printf(" %d\n",r[n]);
    }
    if (n==0)
    {
    sum=0;

          }
        else
         {
              sum =100000;
              for( i=1; i
    
    L L 2 Replies Last reply
    0
    • U User 12763086

      r[1000]; *r = (int *) malloc ((n+ 1) * sizeof(int)); but if I use int *r then also it doesn't passes properly .Should I use structure? The problem should take input like division-3 input of price-1,5,8 and give rod cutting whereas there are three iteration which should be one r=0; and the maximum price for this division should be 8 but the result comes 1677218

      #include
      #include
      #include
      #include
      int sum=0;

      int max(int a, int b)
      {
      return(a>b)?a:b;
      }
      int cut_rod_aux(int price[], int n, int r[])
      { int i;
      if(r[n]>=0)
      {
      return r[n];
      printf(" %d\n",r[n]);
      }
      if (n==0)
      {
      sum=0;

            }
          else
           {
                sum =100000;
                for( i=1; i
      
      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      I am assuming your cut and paste failed because as that appears to me that would not compile the r line has no type Anyhow your malloc is correct the problem is with r 1.) You can't and don't need the array specifier. 2.) You definitely don't dereference the malloc to it ... "*r" means dereference Anyhow any quick check of creating a dynamic array would give you the correct syntax

      int* r;
      r = (int *) malloc ((n+ 1) * sizeof(int));

      where x is the index position in the array. It's your responsibility to release the memory when you are done

      free(r);

      In vino veritas

      L 1 Reply Last reply
      0
      • U User 12763086

        r[1000]; *r = (int *) malloc ((n+ 1) * sizeof(int)); but if I use int *r then also it doesn't passes properly .Should I use structure? The problem should take input like division-3 input of price-1,5,8 and give rod cutting whereas there are three iteration which should be one r=0; and the maximum price for this division should be 8 but the result comes 1677218

        #include
        #include
        #include
        #include
        int sum=0;

        int max(int a, int b)
        {
        return(a>b)?a:b;
        }
        int cut_rod_aux(int price[], int n, int r[])
        { int i;
        if(r[n]>=0)
        {
        return r[n];
        printf(" %d\n",r[n]);
        }
        if (n==0)
        {
        sum=0;

              }
            else
             {
                  sum =100000;
                  for( i=1; i
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You have declared r As an array of 1000 integers and then try to assign it a malloc'ed array of elements. That makes no sense.

        1 Reply Last reply
        0
        • L leon de boer

          I am assuming your cut and paste failed because as that appears to me that would not compile the r line has no type Anyhow your malloc is correct the problem is with r 1.) You can't and don't need the array specifier. 2.) You definitely don't dereference the malloc to it ... "*r" means dereference Anyhow any quick check of creating a dynamic array would give you the correct syntax

          int* r;
          r = (int *) malloc ((n+ 1) * sizeof(int));

          where x is the index position in the array. It's your responsibility to release the memory when you are done

          free(r);

          In vino veritas

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          See the code ...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups