Q: Exercise: Implement a function FibonacciArray(n) that takes an integer n as input and returns an array of length n wh

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Q: Exercise: Implement a function FibonacciArray(n) that takes an integer n as input and returns an array of length n wh

Post by answerhappygod »

Q: Exercise: Implement a function FibonacciArray(n) that takesan integer n as input and returns an array of length n whose k-thelement is the k-th Fibonacci number.
This is the code I used but I'm not sure what I'm doingwrong.
func FibonacciArray(n int) []int {
finalSlice := make([]int, n+1)
finalSlice[0] = 1 f
or i := 1; i <= n; i++ {
if i <= 2 { finalSlice[i-1] = 1
} else { var sumSlice int
sumSlice += finalSlice[i-3]
sumSlice += finalSlice[i-2]
finalSlice[i-1] = sumSlice
}
}
return finalSlice
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply