Incrementing time with a time limit?

Can anyone help with writing a program that has three variables?

N,M,T are the variables.

N is time for one task, M is time the next task will take, T is total time available to complete tasks.

Ex: First task (N) takes 5 minutes, each new task (M) takes 10 minutes. How many tasks can be completed in (T) amount of time?
If T = 45, N = 5 and M = 10, then 5 tasks can be completed.
(N(5)+M(10)+M(10)+M(10)+M(10)=45)

I have a portion of code written, I can upload later. I am not sure whether to use a void or return, and I currently have a for loop within the function.
Last edited on
what is the loop for?
is the answer (T-N)/M + 1? (45-5)/10 + 1 = 5
so pretty much gigo the inputs and write the output, no function or loop required?
Here is the piece I am struggling with, it is not returning the correct cin time or the correct 'S' (number of songs in time allotted):

I think I may also need to incorporate 'floor'

1
2
3
4
5
6
7
8
int practice(int& firstSong, int& nextSong, int& time, int& songs)
{
    int S = songs;
    S = (time - firstSong)/nextSong;
    return S;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int calcNumberOfTasks(int firstTask, int nextTask, int time) {
    if (time < firstTask) return 0;
    return 1 + (time - firstTask) / nextTask;
}

int main() {
    int n = 5, m = 10, t = 45;
    cout << calcNumberOfTasks(n, m, t) << '\n';
}

I got it!
But my math is still wrong apparently.....
Its supposed to by like :
It takes N minutes for first task, and each of following tasks takes M minutes longer than the previous. There are T minutes for all the tasks. For example, if the first task takes N = 15 minutes and M = 10, then the second task will take 25 minutes, the third task will take 35 minutes, and so on. If there are 150 minutes for tasks, then four tasks can be completed because 15 + 25 + 35 + 45 = 120.

1
2
3
4
5
6
void practice(int& firstTask, int& nextTask, int& time, int& tasks)
{

    tasks = ((firstTask + nextTask) + nextTask) / time;

}
@Fayezilla

You do realise that you have now stated two completely different problems:
First task (N) takes 5 minutes, each new task (M) takes 10 minutes.

or
It takes N minutes for first task, and each of following tasks takes M minutes longer than the previous


If you keep changing the problem people will get annoyed.



@dutch has given you an answer to the first.

The second is just an arithmetic series:
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 <cmath>
using namespace std;

int tasks( int a, int d, int S )    // first term a, common difference d, sum S
{
   int n = 1, term = a, sum = term;
   while ( sum <= S )
   {
      n++;
      term += d;
      sum += term;
   }
   return n - 1;
}

int main()
{
   int N, M, t;
   cout << "Input first term  (N): ";   cin >> N;
   cout << "Common difference (M): ";   cin >> M;
   cout << "Total time        (t): ";   cin >> t;

   cout << "Number of completed tasks = " << tasks( N, M, t );
}
Input first term  (N): 15
Common difference (M): 10
Total time (t):        150
Number of completed tasks = 4 



In principle, you could also do it explicitly, rather than by repeated trial, but you would have to watch out for round-off and the floating-point multiplies probably take much longer than the original integer additions.
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
#include <iostream>
#include <cmath>
using namespace std;

double arithmetic_series( double a, double d, int n )     // first term a, common difference d, n terms
{
   return ( 2 * a + (n - 1) * d ) * n / 2;
}

double number_of_terms( double a, double d, double S )    // first term a, common difference d, sum S
{
   double ad = 2 * a - d;
   return 4 * S / ( sqrt( ad * ad + 8 * d * S ) + ad );
}

int main()
{
   int N, M, t, tasks;
   cout << "Input first term  (N): ";   cin >> N;
   cout << "Common difference (M): ";   cin >> M;
   cout << "Total time        (t): ";   cin >> t;

   tasks = number_of_terms( N, M, t );
   if ( arithmetic_series( N, M, tasks+1 ) <= t ) tasks++;    // take care over round-off

   cout << "Number of completed tasks = " << tasks;
}

Last edited on
I apologize for changing the problem. I wrote my first question at work, from my understanding of the problem. I re-read and realized that my math was all wrong, hence, the second problem.
The second problem is word for word from the teacher, and it doesn't make any sense to me how the math is supposed to work...
Fayezilla wrote:
it doesn't make any sense to me how the math is supposed to work


The second problem is the sum of an arithmetic series (you can google that). Each new term is a fixed amount (the "common difference") bigger that the previous term.

In your example,
- the first term is 15, sum is 15
- the second term is 15+10=25, sum is 15+25
- the third term is 25+10=35, sum is 15+25+35

So the sum (i.e. the arithmetic series) is 15+25+35+45+55+...
You can complete tasks where this sum will stay below or equal to the allowed amount of time.

For each new task:
- increment the counter by 1
- increment the term size by the common difference (10 in your example)
- add to the sum
Thank you! Again I apologize for posting a question when I was unclear of the instructions.
Topic archived. No new replies allowed.