C++ hard topic for me

closed account (NbMiz8AR)
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:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// INCLUDES AND NAMESPACES

#include <iostream>

#include <iomanip>

using namespace 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
	if(n<=0) return 0;
	else if(n==1) return 1;
	else return Fibonacci_R(n-1)+Fibonacci_R(n-2);	
}

// iterative version
int Fibonacci_I(int n)
{
	int fib[] = {0,1,1};
	for(int i=2; i<=n; i++)
	{
		fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3];
		cout << "fib(" << i << ") = " << fib[i%3] << endl;
	}
	return fib[n%3];
}

int main(void)
{		
	int a;
	cout << "a = ";
	cin>>a;

	// calculate the fib(i) from scratch for each i <= a using your recursive function
	cout << endl << "From recursive function" << endl;
	for(int i=1; i<=a; ++i)
	        cout << "fib(" << i << ") = " << Fibonacci_R(i) << endl;
	cout << endl;

	// or calculate fib(a) once and output the intermediate results from the looping version
	cout << "From iterative function" << endl;
	Fibonacci_I(a);

	cout << endl;
	return 0;
}
closed account (NbMiz8AR)
any one that can help me?
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' ;
    }
}
a = 4
element|iterative|recursive
---------------------------
      1|        1|        1
      2|        1|        1
      3|        2|        2
      4|        3|        3


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.

closed account (NbMiz8AR)
ok i now have this as you said but it is still not working. the output keeps looping and does not stop. any suggestions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include <iomanip>

using namespace std;

// PROTOTYPES

int Fibonacci_I(int);

int Fibonacci_R(int);



int main(void)
{		
	int a;
	cout << "a = ";
	cin>>a;

    std::cout << "element|iterative|recursive\n"
                 "---------------------------\n" ;

    for ( int i = 0 ; i < a ; ++i )
    {
        std::cout << std::setw(7) << i << '|'
                  << std::setw(9) << Fibonacci_R(i) << '|'
                  << std::setw(9) << Fibonacci_I(i) << '\n' ;
    }
	cout << endl;
	system ("pause");
	return 0;
}
// iterative version
int Fibonacci_I(int n)
{
	int fib[] = {0,1,1};
	for(int i=2; i<=n; i++)
	{
		fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3];
		cout << "fib(" << i << ") = " << fib[i%3] << endl;
	}
	return fib[n%3];
}

/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
	if(n<=0) return 0;
	else if(n==1) return 1;
	else return Fibonacci_R(n-1)+Fibonacci_R(n-2);	
}
closed account (NbMiz8AR)
the output looks really messed up, i cant paste the output, but i can try to write how the output look


	
                 element|iterative|recursive
                    -----------------------
           1|                     1               1
fib(1) 11 
fib(2) 1 1
fib(3) 22



something like this. and it keeps doing the same thing
First, you shouldn't be outputting in the function themselves.


If I modify your main to get the desired output format and comment out line 19 in your code:


That would be line 40 in the latest incarnation.
Last edited on
OP for ref:


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:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// INCLUDES AND NAMESPACES

#include <iostream>

#include <iomanip>

using namespace 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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
	if(n<=0) return 0;
	else if(n==1) return 1;
	else return Fibonacci_R(n-1)+Fibonacci_R(n-2);	
}

// iterative version
int Fibonacci_I(int n)
{
	int fib[] = {0,1,1};
	for(int i=2; i<=n; i++)
	{
		fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3];
		cout << "fib(" << i << ") = " << fib[i%3] << endl;
	}
	return fib[n%3];
}

int main(void)
{		
	int a;
	cout << "a = ";
	cin>>a;

	// calculate the fib(i) from scratch for each i <= a using your recursive function
	cout << endl << "From recursive function" << endl;
	for(int i=1; i<=a; ++i)
	        cout << "fib(" << i << ") = " << Fibonacci_R(i) << endl;
	cout << endl;

	// or calculate fib(a) once and output the intermediate results from the looping version
	cout << "From iterative function" << endl;
	Fibonacci_I(a);

	cout << endl;
	return 0;
}

Topic archived. No new replies allowed.