Question

In: Computer Science

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

·In a different file “sched_sim.cpp”, define classes Process and Sched to simulate an operating system alternating among processes, giving each a “time slice”. You will use a queue to manage the processes.

·The Process class should include: (you can add more functions)

(1)   Integer fields for id and time left (number of cycles left to finish execution) – these must be private

(2)   A constructor to initialize the id and time left

(3)   runPeriod() to “run” the process for 5 cycles. We only care about the scheduling, so output the process being run and how many cycles are left after the 5 cycles are over

(4)   done() to return true if the process has nothing left to do (0 time left)

·The Sched class should include:

(1)   A private queue of Process’s – you can make it a queue of pointers to Process if you prefer

(2)   A function addProcess() that takes a Process parameter and adds it to the queue

(3)   A function exec() that:

(a)   If the queue is empty, output a message for that

(b)   Otherwise, take the first process off the queue and

(i)    call runPeriod() for that process

(ii)  if the process is not done, add it back to the queue

·Write a main() to test your code. Sample output for the final program (process 0 needs 14 cycles, process 1 needs 3 cycles, process 2 needs 7 cycles. Processes 0 and 1 are added first, then the Sched exec()’s once before process 2 is added.)

Simulating timesharing among processes

Running process 0

        9 cycles left

Running process 1

        0 cycles left

Running process 0

        4 cycles left

Running process 2

        2 cycles left

Running process 0

        0 cycles left

Running process 2

        0 cycles left

No waiting processes

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;
}

Do like if it helps. Stay safe


Related Solutions

(C++) All programs will be included! This lab gives you a little practice with linked lists...
(C++) All programs will be included! This lab gives you a little practice with linked lists ·Debug insertSorted() and implement sorted() in numberlist.cpp ·Hint: insertSorted() is just missing a few parts. What is in the function can work fine once you add the right code, though you are free to rewrite it ·You need to have main.cpp, numberlist.h and numberlist.cpp in this project, and if you are using Code::Blocks, remember to set the Build Options to indicate you want to...
(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...
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...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the Queue class: getLength(): returns the number of elements in the queue isEmpty(): returns true if the queue is empty, false otherwise peek(): returns the value at the front of the queue without removing it. Assumes queue is not empty (no need to check) peekRear(): returns the value at the end of the queue without removing it. Assumes queue is not empty (no need to...
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...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Internal implementations of stacks and queues require the tracking of more information than simply the data...
Internal implementations of stacks and queues require the tracking of more information than simply the data being stored in the stack or queue. What is the nature of this information and why does it need to be stored? Does it differ between the objects? If so, how and why does it differ? If not, why can it be the same across object types?
C++ program to read line comments. This assignment will give you a little bit of practice...
C++ program to read line comments. This assignment will give you a little bit of practice with string parsing. Your task is to write a program that reads in a .cpp file line-by-line, and prints out only the text that's after the // characters that indicate a single-line comment. You do not have to worry about /* multiline comments */ though there will be a small amount of extra credit for programs that correctly extract these comments as well.
word level palindrome program with stacks and queues I have a good portion of my program...
word level palindrome program with stacks and queues I have a good portion of my program finished, I am just trying to figure out how to do some final things to it. I want the program to only check for letters a through z and A = Z, treating uppercase and lower case as the same thing. I want it to ignore any other characters and to just realize word level palindromes, not sentence level, so for example a%345b@a c24&c8d)9cc...
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);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT