From the course: Introducing Functional Programming in C++

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Fibonacci generator

Solution: Fibonacci generator - C++ Tutorial

From the course: Introducing Functional Programming in C++

Start my 1-month free trial

Solution: Fibonacci generator

(bright tone) - [Instructor] So, how did you do with challenge? Hopefully, it went well for you. Let's take a look at my solution. The code for this is very similar to that of the factorial program. We need to create two template specializations. One for zero and one for one, and the base case is going to recursively call itself. So, let's take a look at it in code. So here, I have my include file and my namespace set up. I have some code out here to render everything, and let's go ahead and write the code, and we're going to start off with our keyword template, and we're going to say int n, then, we're going to have a struct and this is going to be called fibonacci. Then, inside of here, we'll have our enum, and a value and it's going to be equal to fibonacci and n - 1, and then, use the scope resolution on the value and it's + fibonacci n - 2, and once again, with the value, then, we need a semicolon and another semicolon. Now, let's do the first of our specializations, and so, our…

Contents