Hi y'all!
I'm taking a beginners C++ course and we were assigned the following problem as a project:
If you have a product that yields a profit of
b dollars if sold and a loss of
l if unsold and the probability of
i products will be sold is p(i) =p(1-p)^i.
To determine the number of products to be sold, add 1 to s, where s is the greatest value that satisfies:
b / (b+l) > Σ p(i), as i goes from 0 to s.
Have the program ask for b, l, and p.
I initially had a lot of problems just thinking through how to set up the formulas before I finally settled on a for nested inside of a while. The program compiles fine, unfortunately the output is incorrect. As a check of the program my professor told us the following:
with inputs b=1, l=2, and p=.01, s will equal 39 and the number of products that should be purchased is 40. unfortunately when I run the program with those specs I get:
"To maximize your profit you should order 4247751 products."
Here's the code that I've written. Any help on what I did wrong would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main( )
{
double b, l, p;
cout << "Enter the net profit, in dollars, for each unit sold ";
cin >> b;
cout << "Enter the net loss, in dollars, for each unit left unsold ";
cin >> l;
cout << "Enter the probabilty that i products will be sold ";
cin >> p;
int i, t;
double sum, asum;
while((b/(b+l)) > asum)
{
sum = p*pow((1-p),i);
i=0;
i++;
for (t=0; t<=i; t++)
asum += p*pow((1-p),t);
}
cout << "\nTo maximize your profit you should order " << i+1 << " products." << endl;
system("PAUSE");
return 0;
}
| |