I am new to this C++ language. This is my first time here. i just need help for the problem pasted below. i will gladly accept any suggestions or code example presented to me. thank you for helping me.
INSTRUCTIONS
Write a program that neatly displays the first 21 numbers in the Fibonacci sequence in a table. Recall the definition for the Fibonacci sequence:
F0 = 0
F1 = 1
F2 = 1
Fn = Fn - 1 + Fn - 2
The program should calculate the numbers in the sequence using two different functions. In one display, use an iterative function and in the other use a recursive strategy. The driver code in main should do no calculations; instead, it should just draw the tables and populate them with well-formed calls to the two functions you write. Your functions should take a single integer argument and return the value of the appropriate term in the Fibonacci sequence using the argument as an index. For example Fibonacci(5) should return 5 because 5 is the fifth term in the Fibonacci sequence (it is really the 6th, but zero counts). Fibonacci(7) should return 13 because 13 is the 7th term in the Fibonacci sequence. Start your program with the following shell:
// INCLUDES AND NAMESPACES
#include <iostream>
#include <iomanip>
usingnamespace std;
// PROTOTYPES
int FibonacciIterative(int);
int FibonacciRecursive(int);
// MAIN
int main() {
int i = 0;
// YOUR DRIVER CODE GOES HERE
return 0;
}
// FUNCTION IMPLEMENTATIONS
// returns F_i of the Fibonacci sequence (iterative)
int FibonacciIterative(int i) {
// YOUR ITERATIVE IMPLEMENTATION GOES HERE
}
// returns F_i of the Fibonacci sequence (recursive)
int FibonacciRecursive(int i) {
// YOUR RECURSIVE IMPLEMENTATION GOES HERE
}
Here is my code. i need to know if this is the right way to do the problem above. also i am not getting the same output what i am supposed to get.
the output shoould be like this:
element iterative recursive
0 0 0
1 1 1
2 1 1
3 2 2
...and so on
First, you shouldn't be outputting in the function themselves. In particular, the output in Fibonacci_I isn't very helpful. You need to know what the function returns for a given value of n, not what every term past the 2nd one is.
If I modify your main to get the desired output format and comment out line 19 in your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main(void)
{
int a;
cout << "a = ";
cin>>a;
std::cout << "element|iterative|recursive\n""---------------------------\n" ;
for ( int i = 1 ; i <= a ; ++i )
{
std::cout << std::setw(7) << i << '|'
<< std::setw(9) << Fibonacci_R(i) << '|'
<< std::setw(9) << Fibonacci_I(i) << '\n' ;
}
}
std::setw comes from #include <iomanip> and is just used to prettify the output.
We can definitely see that the pattern is right, we just seem to be shifted one term further in the series than expected.
F0 = 0
F1 = 1
F2 = 1
Fn = Fn - 1 + Fn - 2
Technically the "first" term we're after here is the zeroth term, and that is, in fact, exactly how your functions work. All that's required to get it to work is to change the range of values we're looping on:
Line 10 becomes for (int i = 0 ; i<a ; ++i ) And then change the output in the loop to reflect the term number: the i on line 12 becomes i + 1.
I am new to this C++ language. This is my first time here. i just need help for the problem pasted below. i will gladly accept any suggestions or code example presented to me. thank you for helping me.
INSTRUCTIONS
Write a program that neatly displays the first 21 numbers in the Fibonacci sequence in a table. Recall the definition for the Fibonacci sequence:
F0 = 0
F1 = 1
F2 = 1
Fn = Fn - 1 + Fn - 2
The program should calculate the numbers in the sequence using two different functions. In one display, use an iterative function and in the other use a recursive strategy. The driver code in main should do no calculations; instead, it should just draw the tables and populate them with well-formed calls to the two functions you write. Your functions should take a single integer argument and return the value of the appropriate term in the Fibonacci sequence using the argument as an index. For example Fibonacci(5) should return 5 because 5 is the fifth term in the Fibonacci sequence (it is really the 6th, but zero counts). Fibonacci(7) should return 13 because 13 is the 7th term in the Fibonacci sequence. Start your program with the following shell:
// INCLUDES AND NAMESPACES
#include <iostream>
#include <iomanip>
usingnamespace std;
// PROTOTYPES
int FibonacciIterative(int);
int FibonacciRecursive(int);
// MAIN
int main() {
int i = 0;
// YOUR DRIVER CODE GOES HERE
return 0;
}
// FUNCTION IMPLEMENTATIONS
// returns F_i of the Fibonacci sequence (iterative)
int FibonacciIterative(int i) {
// YOUR ITERATIVE IMPLEMENTATION GOES HERE
}
// returns F_i of the Fibonacci sequence (recursive)
int FibonacciRecursive(int i) {
// YOUR RECURSIVE IMPLEMENTATION GOES HERE
}
Here is my code. i need to know if this is the right way to do the problem above. also i am not getting the same output what i am supposed to get.
the output shoould be like this:
element iterative recursive
0 0 0
1 1 1
2 1 1
3 2 2
...and so on