URGENT! c++ "Multinacci" series

Pages: 12
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 :))
Last edited on
Hi there,

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:

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.

All the best,
but, by doing so, i will get the sum of all inputs. right? but, i only need the sum of x numbers......

if i want 5 numbers: i will input:

then the outputs will be:
1st: 0 + 1 + 2 + 3 + 4 = 10
2nd: 1 + 2 + 3 + 4 + 10 = 20
3rd: 2 + 3 + 4 + 10 + 20 = 39
4th: 3 + 4 + 10 + 20 + 39 = 76
5th: 4 + 10 + 20 + 39 + 76 = 149
... and so on...

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.

All the best,
were only allowed to use looping statements. thanks, by the ways. rest assured, i will post her solution afterwards... thanks..
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.

Use aoop to continuously take input and have a variable to update the total.
Last edited on
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.

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
Last edited on
i'm sorry but i cant understand.. what if the numbers the user gave has no pattern at all?? dont i need to store them all?
you're making this harder than it is, you said you can only use loops.

step1. create a for loop that makes 10 passes.

step2. ask the user how many inputs he wants.

step3. create a for loop for the step above

step4. except the inputs

step5. total them up

6. The Inner loop ends

7. display the total

8. now this will run nine more times because of the outer for Loop

@ GKneeus - That will mean the user has to input how many numbers he wants 10 times.

The outset is that the user enters this once, and then, taking the OP's explanation:

if i want 5 numbers: i will input:

then the outputs will be:
1st: 0 + 1 + 2 + 3 + 4 = 10
2nd: 1 + 2 + 3 + 4 + 10 = 20
3rd: 2 + 3 + 4 + 10 + 20 = 39
4th: 3 + 4 + 10 + 20 + 39 = 76
5th: 4 + 10 + 20 + 39 + 76 = 149

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.

All the best,

Last edited on

It only outputs 10 numbers (1st - 10th), not in specific format (1st: 0 + 1 + 2 + 3 + 4 = 10). But you can enter any numbers, 100, 5, 2, 13, ...
that I misunderstood even simpler than that.

total * 2 -i;

loop it.
Last edited on
@tntxtnt - Nice solution, but it's limited to 10 numbers, the OP stated the amount of numbers is arbitrary.

@Gkneeus - That would only work if the numbers are always sequential, if the input is: 100, 40, 20, 60:

100 + 40 + 20 + 60 = 220
40 + 20 + 60 + 220 = 340
20 + 60 + 220 + 340 = 640

Sorry to burst the bubbles :(

All the best,
edit: i get ur point nm.
Last edited on
Hi there,

I'm not a math wiz either xD.
By sequential I mean 1,2,3,4 as opposed to random, like 100, 40, 20, 60.

All the best,
@NwN: it's can calculate 20 input numbers as well. Try forking it @.@

since the formula is M(k+1) = 2*M(k) - M(k-n), and you only need to output 10 numbers,
sugoidesu wrote:
until the 10th term.

you only need at most 10 variables to store the "M(k-n)". Thus, no array, but a lot of if else for those 10 vars.

for example with input
1 3 6 5 3 2 4 5 4 6 7 2 3

it will output
51 101 199 392 779 1555 3108 6212 12419 24834
Last edited on
Ah yes, I apologize, I misinterpreted the code. That's pretty nifty, well done :)

As the OP is a beginner programmer, I find it hard to believe that this is the solution the teacher wants, but at least you got it working.

Pages: 12