Question

In: Computer Science

(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive...

(C++ Programming)

Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS)

·In “stackfib.cpp”, write a non-recursive function fib() which:

·Takes a single int parameter n

·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2)

·To compute this without using recursion, we use a stack to store the numbers we need to compute

(1)   Initialize your result to be 0

(2)   Push the parameter n onto the stack

(3)   While the stack is not empty,

(a)   Get the top value on the stack (use top()) and pop it off

(b)   If this value is less than 2, add 1 to the result. Do not push anything onto the stack

(c)   Otherwise, push the top value - 1 and the top value – 2. Do not modify the result

(4)   When the stack is finally empty, return the result

·Write main() to test this function – output the values of fib(0) through fib(9)

·Here is some sample final output:

Computing Fibonacci numbers without recursion

fib(0) = 1

fib(1) = 1

fib(2) = 2

fib(3) = 3

fib(4) = 5

fib(5) = 8

fib(6) = 13

fib(7) = 21

fib(8) = 34

fib(9) = 55

Solutions

Expert Solution

  1. I have implemented only the stack Fibonacci portion. Hope it will help.
#include <bits/stdc++.h>
using namespace std;


class FibStack
{
public:
    stack <int> fibStack; // stack intialization

    void getFib(int n)    {
        int res = 0; // store result
        int topElement;
        fibStack.push(n); 
        while(!fibStack.empty()) // looping while stack is not Empty
        {
            topElement = fibStack.top(); // get top element
            fibStack.pop(); // remove top element from the stack
            if(topElement<2)
                res += 1;
            else
            {
                fibStack.push(topElement-1);
                fibStack.push(topElement-2);
            }
        }
        cout<<"fib("<<n<<") = "<<res<<endl; // print output (return res will also work, if output should be printed in main function)
    }
};


int main ()
{
    FibStack fs;
    for(int i=0; i<10; i++) // to get 0-9 fibonacci series
        fs.getFib(i);
    return 0;
}

i hope it helps..

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME


Related Solutions

C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked list-based implementations for the Dequeue ADT. Your implementation must support generic data types using C++ templates. Develop Adapter Files to provide Stack and Queue functionality for the Deques. Definitions: You should implement the ADTs precisely as described in the following partial header files. Deque.h template class Deque { public:         Deque();                    //constructor         ~Deque();                 //destructor         void insertFront(const E& e);...
ASSEMBLY X86, using VISUAL STUDIOS 2019 Please follow ALL directions! Write a program that calculates and...
ASSEMBLY X86, using VISUAL STUDIOS 2019 Please follow ALL directions! Write a program that calculates and printout the first 6 Fibonacci numbers.   Fibonacci sequence is described by the following formula: Fib(0) = 0, Fib(1) = 1, Fib(2) = Fib(0)+ Fib(1), Fib(n) = Fib(n-1) + Fib(n-2). (sequence 0 1 1 2 3 5 8 13 21 34 ...) Have your program print out "The first six numbers in the Fibonacci Sequence are". Then the numbers should be neatly printed out, with...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251 1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. 2. Add the following operations to your program: a. Return the first person in the queue...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
Please follow the directions and answer all questions properly. Type the answers please. NO hand written...
Please follow the directions and answer all questions properly. Type the answers please. NO hand written answer. This is a Marketing class. Thanks Please write no more than 100 words for each question. The questions are about fast food chain “Chick-Fil-A. These questions were asked by my professor. Please make sure that answers are good. I would really appreciate. Thanks again. I’m having a hard time understanding differences among needs, wants and demands of “Chick-Fil-A” customers. How would you explain...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following Binary Search Tree Operations FIND (item) INSERT (item) DELETE (item) DELETE_TREE (delete all nodes - be careful with the traversal!)
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT