please DO NOT give me the algorithm of the program.. I just need some help and tips. thank you in advance :))
we were supposed to create a c++ program.
ex.: How many numbers do you want to enter? [input], 5
Enter 5 numbers.: 2, 5, 7, 8, 11
then, we were supposed to output the sum of the x numbers:
output: 33. and do this for 10 times.. thus the output for my example will be:
33, 64, 123, ... until the 10th term.
what function should I use? I used arrays, but my teacher said that were not allowed to use such. any helps please??
i was able to make a fibonacci and tribonacci without using arrays, however, i used 3 and 4 variables, respectively. how can i deal with multinacci with the least possible variables?
please DO NOT give me the algorithm of the program.. I just need some help and tips. thank you in advance :))
I understand your initial thought to use arrays, but you don't really need them for this program.
When the user enters five numbers, you just have to keep the sum of those numbers, so you need one temporary variable to hold the input and one for the sum:
1 2 3 4 5
int tmp_input;
int sum=0;
std::cin >> tmp_input;
sum += tmp_input;
Hope that makes sense to you, please do let us know if you require any more help.
well, if i understood clearly, then i suppose that code wont work on me.. what should i do? i need to finish this in a short period f time. thank you very much
At least you must have an array of n element to store the first n input numbers. If the teacher doesn't allow any array at all then I surrender :/ When you get the solution from him please post it here I want to see @.@
Ah all right, I'm sorry, I didn't understand your question in that way.
I'm not sure how you would do this without arrays, as far as I can tell you need a way to store those numbers.
Strictly speaking using STL containers like std::vector is not using an array, but that's probably not what your teacher meant?
It might be possible using recursion, but I can't immediately figure that one out I'm afraid.
Maybe someone can come up with an idea.
Well, M(k+1) = 2*M(k) - first element of the array = 2*M(k) - M(k-n)
The most natural would be to push the first n values into a queue, and for each new value push it in and pop the oldest out. The queue lenght remains n.
Of course, if one could somehow calculate any M(j) directly without recursion, like you can for Fibonacci, then no array would be necessary.
but, if i'm gonna use what i did in fibonacci, its gonna require a lot of variables. what if the user wants to input 100 numbers? then i'll be needing 101 variables... :(
please someone help me.. our grade here is either a perfect 100 or a failing 70. please...
oh since you only need to output 10 numbers, then you only need ~10 variables to do this.
Ex:
How many numbers: 5
Enter 5 numbers: 0 1 2 3 4
Let's call them a, b, c, d, e, f, g, h, i, j.
assign 0 to b, assign 0 to a
assign 1 to c, add 1 to a
assign 2 to d, add 2 to a
assign 3 to e, add 3 to a
assign 4 to f, add 4 to a
now a will be the next term.
b next to a is calculated as: b = 2*a - b
c next to b is calculated as: c = 2*b - c
d next to c is calculated as: d = 2*c - d
e next to d is calculated as: e = 2*d - e
f next to e is calculated as: f = 2*e - f
g next to f is calculated as: g = 2*f - a //critical point here
h next to g is calculated as: h = 2*g - b
i next to h is calculated as: i = 2*h - c
j next to i is calculated as: j = 2*i - d
output 10 numbers from a to j
for 100 numbers you only need to store first 9 numbers, and you don't have to look for critical point
So the user inputs numbers once, then for as many numbers were entered, as many lines of output and sums are produced. Especially if you need to accept input like this (comma separated and a variable amount of values), I don't see any way to do this without arrays and only using for loops.