Maximize Profit formulas producing an incorrect output

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;
}

In line 19 you are using the variable asum without initiliazing it first.
Hi Nalle!

Thanks for responding - I'm not entirely certain what you mean though. I think you mean I should put in "asum=0;" before I start the while loop? like this:

1
2
3
4
5
6
7
    int i, t;
    
    double sum, asum;

    asum=0;

    while((b/(b+l)) > asum)


But when I try that the output with the same specs as above is 2. I also tried throwing in "sum=0;" right after "asum=0;" and it still spits out 2.

Did I misunderstand?

Thanks!
I didn't look into your formulas. I only saw that in line 19 asum is used without initialization. Same happens in line 22 with the variable i.
Adding "i=0;" seems to have fixed it.

Thanks so much for your help!
Well, Lines 23 and 24 don't make any sense together like that.
You initialize i to 0 and then post increment i to 1.

That means it will always be 1 by the time you hit Line 26.

Is that what you really want?
If I understand your problem correctly it should be something like this:
1
2
3
4
5
6
7
int i =0;
double asum = 0;

while(asum < b / (b + 1)){
asum += p * pow(1 - p, i);
i++;
}

Then i should be the integer you are looking for.
NallePul1983, hmm...

...when I plug in b=1, l=2, and p=.01 to your program, I get 70 products rather than the expected 40.
Here's my updated code.

i=0 did end up moving outside of the while loop I can see why it would need to.

With this I checked the one the professor gave us as well as two others (we're to give in 3 outputs) and I checked those against my graphing calculator and they seemed correct:

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
35
36
37
#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 a value for p such that p(1-p)^i is the probabilty that i products will be sold ";
    cin >> p;

    int i, t;
    
    double sum, asum;

    sum=0;
    asum=0;
    i=0;
    t=0;
    
    while((b/(b+l)) > asum)
    {
        sum = p*pow((1-p),i);
        i++;
        
        for (t; t<=i; t++)
            asum += p*pow((1-p),t);
    }
    
    cout << "\nTo maximize your profit you should order " << i << " products." << endl;
    
    system("PAUSE");
    return 0;
}



outputs:
b=1, l=2, p=.01 output=40
b=18, l=2, p=.05 output=44
b=123543, l=1, p=.09 output=124
gj!
Topic archived. No new replies allowed.