Final output is one lower than it should be.

Heya this is the issue that I am solving:

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

It takes her a minutes 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.

Here is my code.

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
38
  #include <iostream>

using namespace std;

int main() {
    // Write your main here

    int Initial_Dish_Time; //Initial time required to make a dish.
    int Incremental_Dish_Time; //The ratio by which the time increases.
    int Added_Dish_Time; //An increasing value which increases by the incremental dish time. 
    int Time_Limit;
    int Possible_Dishes = 0;

    

    cout << "Enter the time used to create the first dish: ";
    cin >> Initial_Dish_Time;
    cout << "Enter the extra time needed to create the second dish: ";
    cin >> Incremental_Dish_Time;
    cout << "Enter the available time: ";
    cin >> Time_Limit;

    Added_Dish_Time = Incremental_Dish_Time;

    for(int i = Initial_Dish_Time; i <= Time_Limit; i += Added_Dish_Time + Initial_Dish_Time)
      {
        Added_Dish_Time += Incremental_Dish_Time;

        if(i <= Time_Limit)
          Possible_Dishes++;      
      }
    
    cout << "Possible number of dishes in given time: " << Possible_Dishes;



    return 0;
}


The issue I have is that is the math seems to fall short by 1 on certain inputs.

Example:
2
5
9999999
Expected Result: 2000
Current Result: 1999

I don't quote know what I am looking for here to solve this issue.
Last edited on
> i += Added_Dish_Time + Initial_Dish_Time
You seem to be double counting the initial time.

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.


You could verify the simple cast by putting this in your loop.
1
2
3
4
5
6
7
8
    for(int i = Initial_Dish_Time; i <= Time_Limit; i += Added_Dish_Time + Initial_Dish_Time)
      {
        Added_Dish_Time += Incremental_Dish_Time;

        if(i <= Time_Limit)
          Possible_Dishes++;      
cout << "DEBUG:" << i << " " << Added_Dish_Time << " " << Possible_Dishes << endl;
      }

When you're happy it works, then you can take the debug line out.
@salem c

I had actually added the Incremental_Dish_Time twice with:

 
Added_Dish_Time = Incremental_Dish_Time;


It took me a bit to understand the numbers the debug was throwing at me but once I understood it I was able to fix the problem. Partially because a few of the compilers I was using were not giving me the initial values, but I found one that did and saw that the starting 'Added_Dish_Time' number was 10 when it should have been 5.

I did not know that you could debug it in that manner either, thanks for that bit of knowledge.

I appreciate the help, thank you!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double a, d, t, temp;
   cout << "Enter the time used to create the first dish: ";   cin >> a;
   cout << "Enter the extra time needed to create the second dish: ";   cin >> d;
   cout << "Enter the available time: ";   cin >> t;
   temp = 2 * a - d;
   cout << "Possible number of dishes in given time: " << (int)( (-temp + sqrt( temp * temp + 8 * d * t ) ) / ( 2.0 * d ) );
}
That looks a lot more compact than my solution, but I don't really understand how it works just yet.
but I don't really understand how it works just yet.


Sum of an arithmetic series. It gives a quadratic equation for n. You need the positive root (or the next integer below it).
Topic archived. No new replies allowed.