Skip to content
Advertisement

Java how to return an array with fibonacci values starting at 1?

I’m writing a void function fibFill which fills an array with Fibonacci numbers. It doesn’t have to return anything.

Here’s what I have so far:

JavaScript

For example, if I pass an array of length 5 to the method, it will override the contents of the passed array like this: [1, 1, 2, 3, 5]

Advertisement

Answer

Your fibFill method shouldn’t have a pos method embedded in it; and I would make it static (so it can be called without an instance), like

JavaScript

Then you can test it with something like

JavaScript

Which outputs (as requested) the fibonacci values starting at 1

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement