5 Replies - 315 Views - Last Post: 29 June 2013 - 04:27 PM Rate Topic: -----

#1 jeffries7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-November 12

Fibonacci - Iterative & Recursive

Posted 24 June 2013 - 08:41 AM

I need to write a program that takes a users input and prints the fibonacci sequence then prints the sum of them.

It is printing the sequence fine but when it comes to the sum it's off. Any sort of help would be great. Not expecting anyone to give me answers or do it for me, just a little help :)/>

If you require any more details/info let me know.

n is the users input

#include <iostream>
using namespace std;


int fib(int n)
{	
	if (n == 0)
		return 0;
	if (n == 1)
		return 1;

	return (fib(n-1)+fib(n-2));	
}

int fib2(int n)
{
	int a = 0, b = 1, c = 0;
	for (int i = 0; i < n; i++)
	{
		cout << a << " ";
		c = a + b;
		a = b;
		b = c;			
	}

	return b;	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Fibonacci - Iterative & Recursive

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: Fibonacci - Iterative & Recursive

Posted 24 June 2013 - 09:00 AM

Debugging 101: Find the spot where the error becomes manifest. Figure out which variable is wrong, and track back until you find the spot where you set it incorrectly.
Ask yourself: "Which value is wrong, and how did it get that way?" Repeat as needed.

Questions 101: If you have a problem, describe it. Particularly, if you have a wrong value, tell us what it is!

This post has been edited by jon.kiparsky: 24 June 2013 - 09:14 AM

Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Fibonacci - Iterative & Recursive

Posted 24 June 2013 - 01:54 PM

Where is your main function?
Was This Post Helpful? 0
  • +
  • -

#4 jeffries7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-November 12

Re: Fibonacci - Iterative & Recursive

Posted 25 June 2013 - 03:45 AM

Thank you Jon I will continue to look into it.

jjl - I did not post it because it only contains the GUI, therefore not needed here
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Fibonacci - Iterative & Recursive

Posted 25 June 2013 - 04:16 AM

Quote

It is printing the sequence fine but when it comes to the sum it's off.

You need to post the code that calls these functions and sums the values.

Also show a sample of your input, the output of the program, and what you expect the output to be based on that input.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Fibonacci - Iterative & Recursive

Posted 29 June 2013 - 04:27 PM

The two functions seem to have the same objective namely to print the Fibonacci series. The fib () function seems OK but fib2() has errors if its to be used for this purpose. Are you saying that fib2 () is meant to be summing the series? If so line 21 needs to go below line 23 and a further statement like sum+=c;needs to be added.
Hope this helps as I have not tested this suggestion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1