Connecting Tech Pros Worldwide Forums | Help | Site Map

in c++ how to compute the value of Pn by this formula

Newbie
 
Join Date: Jun 2009
Posts: 6
#1: Jun 6 '09
so can anyone tell me how to proceed on writing a function that uses bottom-up dynamic programming approach to compute the value of Pn defined by the recurrence:
P_n= floor(n/2) + P floor(n/2) + P ceil(n/2) for n>=1 with P_1=1

floor() is the function that rounds a number down.
ceil() is the function that rounds a number up.

sample manual calculation of the sequence would be:

P_2=floor(2/2) + P floor(2/2) + P ceil(2/2)= 1 + P1 + P1= (1+1+1) = 3

I really don't know how to proceed writing this function. Thanks in advance.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#2: Jun 7 '09

re: in c++ how to compute the value of Pn by this formula


Look up recursion/recursive functions. have an attempt at the problem yourself, the maths library has ceiling and floor functions look up math.h.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Jun 7 '09

re: in c++ how to compute the value of Pn by this formula


Also note than if n is even then floor(n/2) == ceil(n/2); otherwise ceil(n/2) == floor(n/2)+1. This allows for a simple interval definition of P:

P(n) == if n even then n/2+P(n/2)+P(n/2)
else n/2+P(n/2)+P(n/2+1)

where '/' denotes integer division, i.e.

P(1) == 1
P(2) == 1+P(1)+P(1)
P(3) == 1+P(1)+P(2)
P(4) == 2+P(2)+P(2)
P(5) == 2+P(2)+P(3)
P(6) == 3+P(3)+P(3)
P(7) == 3+P(3)+P(4)
P(8) == 4+P(4)+P(4)
etc.

kind regards,

Jos
Newbie
 
Join Date: Jun 2009
Posts: 6
#4: Jun 8 '09

re: in c++ how to compute the value of Pn by this formula


#include<cmath>
#include<iostream>
using namespace std;
int main()
{
int p[100];
p[0]=0;
p[1]=1;

int n=0;
for(int i=2; i<=n;i++)
{
if(n % 2 == 0)
{
return n + floor(n/2) + ceil(n/2);
}
else
{
return n + (floor(n/2) + 1) + ceil(n/2);
}
}

return 0;
}
could anyone tell me is this the right logic? thanks for the help. I am getting an error that says floor anbiguos call to overloaded function.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Jun 8 '09

re: in c++ how to compute the value of Pn by this formula


Did you read my previous reply? You don't need the floor() and ceil() functions. The integer division operator will do.

kind regards,

Jos
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 381
#6: Jun 8 '09

re: in c++ how to compute the value of Pn by this formula


The logic is wrong, because it immediately returns from main and stops calculation on the first iteration. Let alone it doesn't compile.
Newbie
 
Join Date: Jun 2009
Posts: 6
#7: Jun 8 '09

re: in c++ how to compute the value of Pn by this formula


Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int p[100];
  7.     p[0]=0;
  8.     p[1]=1;
  9.  
  10.     int n=0;
  11.     for(int i=2; i<=n;i++)
  12.     {
  13.  
  14.         if(n % 2 == 0)
  15.         {
  16.             n=(n/2) + p(n/2) + p(n/2);
  17.  
  18.         }
  19.         else
  20.         {
  21.             n=(n/2) + p(n/2) + p((n/2)+1);
  22.  
  23.         }
  24.     }
  25.     system("pause");
  26.     return 0;
  27. }
  28.  
so using integer division operator. what am I missing? any suggestions. Thank
for the help
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 830
#8: Jun 8 '09

re: in c++ how to compute the value of Pn by this formula


Look closely at your 'for' loop. How many times will the body of the loop be executed? How many times do you want it to execute?

Do you care whether you end up with an iterative or a recursive function? A 'for' loop suggests that you're aiming for an iterative function.

By the way, it would be helpful if you enclosed your source code in CODE tags.
Reply