I'm having trouble

I don't know what's wrong. I think it is something with the loop and the formula I have in the loop. Please help.


Bianca is preparing special dishes for her daughter’s birthday.

It takes her a minute to prepare the first dish, and each following dish takes b minutes longer than the previous dish. She has t minutes to prepare the dishes.

For example, if the first dish takes a = 10 minutes and b = 5, then the second dish will take 15 minutes, the third dish will take 20 minutes, and so on.

If she has 80 minutes to prepare the dishes, then she can prepare four dishes because 10 + 15 + 20 + 25 = 70.

Write a program that prompts the user to enter the values of a, b, and t, and outputs the number of dishes Bianca can prepare.

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
 #include <iostream>
#include <iomanip>
using namespace std;

int main() {
     int a;
     int b;
     int t;
     int c;
    
    cout << "Enter the number of minutes to prepare the first dish: " << endl;
    cin >> a;
    cout << "Enter the minutes longer than the previous dish: " << endl;
    cin >> b;
    cout << "Enter the number of minutes to prepare the dish: " << endl;
    cin >> t;
    c = 0;
    while ( a <= t)
    { a = a + b;
     c ++;
    }
    cout << "You can make " << c 
        << " dishes";
    return 0;
}
yes the logic is wrong.
if a = 10 and b = 5
the loop says
a = 15
a = 20
a = 25

and I think you need a running total of the a's
you are getting each a correctly but not adding them up
i think it wanted
15+20+25 as a total, that is, not counting by 5s.

for fun, do you see a way to do this without a loop?
70 = x*a+ ((x-1)*x)/2*b unless you can only make 1 dish? I think..
simplify to 5x*x+15x = 160, x = 4.3.. x = 4... etc...
or 80 = that and take the whole number part of x?

which has nothing to do with your code, really, just silly math tricks.
Last edited on
It often helps to “trace” through your code.

Start by listing your values:

  a = 10
  b = 5
  t = 80
  c = 0

Now, follow the logic of lines 18-21 and update each value as the code does it.

  (a=10 <= t=80)
  a=10 15
  b=5
  t=80
  c=0 1

As you do this you will see where you are missing something.

BTW, t isn’t the time to prepare any single dish; it is the time to prepare all dishes with your a and b constraints. (I think you know this. Look at your addition.)

Hope this helps.


[edit]
PS. Bat Juan rocks! (He cannot be tres-passing. :D )
Last edited on
Registered users can post here. Sign in or register to post.