473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

fibonacci series

Hi guys,
please tell me how to write the iterative version of fibonacci series using a stack.
Thanks
Sep 20 '06 #1
4 4868
D_C
293 100+
Why would you want to use a stack to to do the Fibonacci series? It takes two variables.

The idea is to push 1 and 1 on to the stack to begin. Then, for each iteration, pop A, then B off the stack (B >= A). Push B+A, then B back on to the stack. Repeat that loop N times or so.

The stack is entirely worthless, it could be just done using the two variables...
Sep 20 '06 #2
I know the stack is entirely worthless but that's the assignment i have been given by my professor and I don't have a choice..
I wrote this program:
#include <cstdio>
#include<iostream>
using namespace std;
int main(int nNumberofArgs, char*pszArgs[])
{
int n;
cout<<"n";
cin>>n;
int b=1, a=0, c=0;
if (n == 0) cout <<a;
else if (n==1)
cout<<a<<b;
else
{
//cout<<c=0;
for (int i=1; i<=n; i++) {
cout<<c;
c=a+b;
a=b;
b=c;
}
}
system("PAUSE");
return 0;
}


but I don't know how to write it using a stack, I don't even know how to use push or pop functions...
I would really appreciate ur help
Sep 20 '06 #3
tyreld
144 100+
I'm guessing you are probably supposed to define a stack class. Here is a one possible class definition for a stack (minus the actual member function implementations).

Expand|Select|Wrap|Line Numbers
  1. #define STACK_SIZE 10
  2.  
  3. Class Stack {
  4. public:
    Stack();
  5.  
  6. void push(int value);
  7. int pop();
  8. private:
    int size = STACK_SIZE;
  9. int count = 0;
  10. int stack[STACK_SIZE];
  11. }
  12.  
Sep 21 '06 #4
D_C
293 100+
The stack is first in, last out. Suppose you push X, then Y.
(last to come off stack) X Y (first to come off stack).

Make a stack, say s. Before you start your loop, push 1 then 0, or 0 then 0, depending on how the numbers line up.

For each iteration of your loop,
Y = s.pop();
X = s.pop();
s.push(X+Y);
s.push(X);

When you get to N, you simply print s.pop(), assuming it lines up properly.
Sep 21 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
4
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or...
12
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another...
11
by: MARQUITOS51 | last post by:
Hey guys this is the fibonacci series. But Im having this huge problem. First of all I want to do the simplest code possible so then I can use user defined functions and arrays. But this one didnt...
22
by: kookai | last post by:
hi I need help with this problem. I have tried writting the program, i just can not get it to run properly. Please Write The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, ... begins with the...
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
3
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
6
by: deepner | last post by:
plz help to find why the program showing fibonacci(non-recursively) is running infinitely #include<iostream> #include<conio.h> using namespace std; unsigned int fibo(int a,int b) { unsigned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.