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.
#include <iostream>
usingnamespace 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.
#include <iostream>
#include <cmath>
usingnamespace 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.
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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...
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