Don’t know how to do this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Don’t know how to do this

I would like to do the challenge below recursivly but im stuck. If anybody can help without giving me the code. Thanx :) The challenge : N-bonacci numbers The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number is equal to the sum of the preceding two numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... We can generalize this to an N-bonacci sequence, where the sum of the preceding N numbers form the next term. For example, a 3-bonacci sequence is the following: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, ... Write a program which takes two inputs: N and M, and gets the M-th term of any N-bonacci sequence. For example: Input: (N, M) = (4, 13) Output: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208 here is my try : https://code.sololearn.com/c7yN5weVwSce/#cpp

14th Aug 2018, 4:06 PM
Zbouby
Zbouby - avatar
27 Answers
0
Zbouby also here is mine https://code.sololearn.com/cBaB9oS9eDXw/?ref=app the commented out code at the bottem is the one i modifyed the variables i sent to the function before you showed me that the if statments should be < and = n
15th Aug 2018, 8:42 PM
Mooaholic
Mooaholic - avatar
+ 1
You will need a array to store your numbers, i would suggest a vector so you can change the size as you want to but if you dont know much about vector and dont want to look up more about it you can use a normal array and set it to the size of M. You then know that if your going for 3-bonacci it will start with 0, 0 , 1 or whatever amount you are going with there will be N-1 0's and then a 1. so you can set the first N variables in your array. or use a loop to do that. Then you have to start adding a for loop works good for this. Hope this is enough to get you started. If you want a little more detail let me know. Also when you start to code it you can link your code here if you get stuck or run into a error.
14th Aug 2018, 5:20 PM
Mooaholic
Mooaholic - avatar
+ 1
Mooaholic i've already made a try i just forgot to write it here it is : https://code.sololearn.com/c7yN5weVwSce/#cpp I dont know if i am doing good but it look pretty correct do you know which arguments should i pass ?
14th Aug 2018, 5:51 PM
Zbouby
Zbouby - avatar
+ 1
Zbouby im not sure what you are doing with the int x because you dont give it a value and are comparing it to n. not sure whats going on there but as for your loop with the question in it. you should have an array to store the values in up till you get to m then you can return that value.
14th Aug 2018, 6:10 PM
Mooaholic
Mooaholic - avatar
+ 1
mooaholic the question was for you lol and thx i ll try the solution with an array
14th Aug 2018, 6:16 PM
Zbouby
Zbouby - avatar
+ 1
Mooaholic i think array will work but not if i do it with a recursive function.
14th Aug 2018, 7:29 PM
Zbouby
Zbouby - avatar
+ 1
Zbouby i didnt know you where trying to use a recursive function, im not sure how you would do what you want to do or if you can do it with a recursive function i did it with just a normal function or without a function using an array and for loops.
14th Aug 2018, 7:39 PM
Mooaholic
Mooaholic - avatar
+ 1
Mooaholic i know this is possible (im trying to improve myself with recursiv function but its hatd af) thank you for your answers ;)
14th Aug 2018, 7:41 PM
Zbouby
Zbouby - avatar
+ 1
i think i ll give up for the moment its setting me up
14th Aug 2018, 7:46 PM
Zbouby
Zbouby - avatar
+ 1
Zbouby sorry i wasnt more help if i figure it out ill let you know im not a big fan of recursive functions, the extra variable being passed in is screwing with my head. i can do it if its a set pattern lol
14th Aug 2018, 8:28 PM
Mooaholic
Mooaholic - avatar
+ 1
Mooaholic no you were very helpful ;) i’ll tell you if I succeed !
14th Aug 2018, 8:46 PM
Zbouby
Zbouby - avatar
+ 1
Zbouby I did it!!!! you can do it with a for loop, 2 if statements, and 1 extra int. well thats what i used and lots of thinking lol.
14th Aug 2018, 9:33 PM
Mooaholic
Mooaholic - avatar
+ 1
mooaholic how can i have the previous result of the calculation?
15th Aug 2018, 9:26 AM
Zbouby
Zbouby - avatar
+ 1
Zbouby for your code you have to remove where you have x everywhere, remove your first for loop, your 2 if statments are correct but should be looking for m is equal to 0 or 1, in your last for loop you are using m you need to use n, then you need to make a int and set it to 0 and in your forloop you have to add one of your variables to that new variable but pass it back threw your function. also it will not give you the correct answer at first you do have to change what you pass to the function some. hope this helps some. hard to give hints without giving it away.
15th Aug 2018, 10:04 AM
Mooaholic
Mooaholic - avatar
+ 1
here is what i've done https://code.sololearn.com/c7yN5weVwSce/#cpp on my compiler its printing "0 0 1" for an input of n=3 and m=10 i think i underestand but i still stuck on the recursive return :/
15th Aug 2018, 12:37 PM
Zbouby
Zbouby - avatar
+ 1
OMG I think I've almost suceed ! i've found that if we put "m-i" in the argument of the fonction (in the for) we get the previous result ! but its not printing that at all :'c A good advancement is that all the number are printed. I know its not the easiest way to start in recursiv x) and its my first program using recursivity.
15th Aug 2018, 4:37 PM
Zbouby
Zbouby - avatar
+ 1
with the actual code changing the n as you tell me to do in the if print 0 1 1 1 1 1 1 1 1 1 isnt it censed to print 0 0 1 ? (with n=3 and m=10)
15th Aug 2018, 4:53 PM
Zbouby
Zbouby - avatar
+ 1
done. but its just printing the same output :/ is it working for you?
15th Aug 2018, 5:23 PM
Zbouby
Zbouby - avatar
+ 1
IT WORKS THANK YOU
15th Aug 2018, 8:42 PM
Zbouby
Zbouby - avatar
+ 1
the principal thing is that I ve understand all the code thank you very much for your time
15th Aug 2018, 8:43 PM
Zbouby
Zbouby - avatar