answer in C++. There are two recursive functions in this assignment. Is there a way to…
- User
answer in C++. There are two recursive functions in this assignment.
Is there a way to turn them into loops? Why or why not?
If it is possible to turn a recursive function into a loop, implement the code and submit to Canvas.
void head(int n)
{
std::cout << "Call: Head" << std::endl; if (n == 0) return; else head(n - 1); std::cout << "Backtrack: " << n << std::endl;}
void tail(int n)
{
std::cout << "Call: Tail "; if (n == 0) return; else std::cout << n << std::endl; tail(n - 1);}
- Assistant
Yes, it is possible to turn recursive functions into loops using iteration. Here are the implementations of the given recursive functions using loops:
#include <iostream> void head(int n) { while (n != 0) { std::cout << "Call: Head" << std::endl; n--; std::cout << "Backtrack: " << n+1 << std::endl; } } void tail(int n) { while (n != 0) { std::cout << "Call: Tail " << n << std::endl; n--; } } int main() { head(3); std::cout << std::endl; tail(3); return 0; }In the above code, the recursive calls are replaced with loops. The
headfunction uses awhileloop that starts fromnand decrements it by 1 in each iteration untilnbecomes 0. Similarly, thetailfunction also uses awhileloop that starts fromnand decrements it by 1 in each iteration untilnbecomes 0. - User
answer in c++. substitute the recursive code for loop, both head() and tail(). There are two recursive functions in this assignment.
Is there a way to turn them into loops? Why or why not?
If it is possible to turn a recursive function into a loop, implement the code and submit to Canvas.
void head(int n)
{
std::cout << "Call: Head" << std::endl; if (n == 0) return; else head(n - 1); std::cout << "Backtrack: " << n << std::endl;}
void tail(int n)
{
std::cout << "Call: Tail "; if (n == 0) return; else std::cout << n << std::endl; tail(n - 1);}
- Assistant
Here is the modified code where the recursive functions
head()andtail()are implemented using loops instead:#include <iostream> void head(int n) { while (n != 0) { std::cout << "Call: Head" << std::endl; n--; } std::cout << "Backtrack: " << n << std::endl; } void tail(int n) { while (n != 0) { std::cout << "Call: Tail "; std::cout << n << std::endl; n--; } } int main() { head(3); std::cout << std::endl; tail(3); return 0; }In this code, the recursive calls in
head()andtail()have been replaced with loops. Thehead()function uses awhileloop that starts withnand decrements it by 1 in each iteration untilnbecomes 0. Thebacktrackstatement is placed outside the loop to ensure it is executed once after the loop terminates. Similarly, thetail()function uses awhileloop that starts withnand decrements it by 1 in each iteration untilnbecomes 0. Thecoutstatement is placed inside the loop before the decrement statement to print the value ofnin each iteration. - User
This assignment will help you get practice with:
Abstract Data Types Inheritance Polymorphism C++ pointers C++ Classes C++ virtual and pure virtual functions C++ Class Templates C++ Arrays Task:
A container class is a data type that is capable of holding a collection of items and provides functions to access them. Bag is an example of a container class. It is an unordered collection of items that may have duplicates. In this assignment, you are asked to design and develop an abstract Bag class, called BagInterface, with the following fixed collection of operations: Insert an item of any type into a bag Query the bag contents: two queries Is an item in the bag? How many copies of an item is in the bag? Remove an item from the bag clear the bag Get the size of the bag How many items are there in the bag? Check if the bag is empty Check if the bag is full Assume the bag capacity is 20 In addition to BagInterface, you are asked to implement two classes of bag implementations: PlainBag and MagicChangeBag.
PlainBag is a simple container that holds items of any type using an array implementation.
MagicChangeBag is also very similar to PlainBag, with two exceptions. When an item is inserted into a magic change bag, it’ll magically disappear, and bag looks empty. Whenever a remove operation is invoked, all the items, except the one is removed will appear.
You are given the following [AssignmentBag.cpp] Download AssignmentBag.cpp] (contains the main function) diver application together with a sample run.
To do:
Create BagInterface abstract class Implement PlainBag and MagicChangeBag classes Use templates Use virtual and pure virtual functions Use arrays Provide UML class diagram for your solution Important Notes:
You are not allowed to modify main.cpp implementation. Please use it as it is. Before you submit your work, please make sure the entire folder works. You are not required to handle the edge cases in this assignment.
- Assistant
Here is a possible implementation of the abstract class
BagInterface, along with the derived classesPlainBagandMagicChangeBag:#include <iostream> template <typename T> class BagInterface { public: virtual void insert(const T& item) = 0; virtual bool contains(const T& item) const = 0; virtual int count(const T& item) const = 0; virtual void remove(const T& item) = 0; virtual void clear() = 0; virtual int size() const = 0; virtual bool isEmpty() const = 0; virtual bool isFull() const = 0; virtual ~BagInterface() {} }; template <typename T> class PlainBag : public BagInterface<T> { private: static const int capacity = 20; T items[capacity]; int itemCount; public: PlainBag() : itemCount(0) {} void insert(const T& item) { if (itemCount >= capacity) { std::cout << "Error: Bag is full" << std::endl; return; } items[itemCount] = item; itemCount++; } bool contains(const T& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; } int count(const T& item) const { int itemOccurrences = 0; for (int i = 0; i < itemCount; i++) { if (items[i] == item) { itemOccurrences++; } } return itemOccurrences; } void remove(const T& item) { int index = -1; for (int i = 0; i < itemCount; i++) { if (items[i] == item) { index = i; break; } } if (index == -1) { std::cout << "Error: Item not found in the bag" << std::endl; return; } for (int i = index; i < itemCount - 1; i++) { items[i] = items[i + 1]; } itemCount--; } void clear() { itemCount = 0; } int size() const { return itemCount; } bool isEmpty() const { return (itemCount == 0); } bool isFull() const { return (itemCount >= capacity); } }; template <typename T> class MagicChangeBag : public PlainBag<T> { public: void insert(const T& item) { this->clear(); } }; int main() { BagInterface<int>* bag = new PlainBag<int>(); bag->insert(10); bag->insert(20); bag->insert(30); std::cout << "Bag contains 20? " << bag->contains(20) << std::endl; std::cout << "Count of 30 in the bag: " << bag->count(30) << std::endl; bag->remove(20); std::cout << "Size of the bag: " << bag->size() << std::endl; std::cout << "Is bag empty? " << bag->isEmpty() << std::endl; bag->clear(); delete bag; BagInterface<double>* magicBag = new MagicChangeBag<double>(); magicBag->insert(3.14); std::cout << "Count of 3.14 in the magic bag: " << magicBag->count(3.14) << std::endl; magicBag->remove(3.14); std::cout << "Size of the magic bag: " << magicBag->size() << std::endl; delete magicBag; return 0; }In this implementation, the
BagInterfaceclass is an abstract class that declares the common operations for bothPlainBagandMagicChangeBagclasses. It is templated to allow for any type of item to be stored in the bag.The
PlainBagclass uses an array to store the items, with a maximum capacity of 20. It provides the required implementations for the operations defined in theBagInterfaceclass.The
MagicChangeBagclass is derived fromPlainBag, and it overrides theinsertfunction. This override clears the bag whenever an item is inserted.You can provide the UML class diagram for this solution as part of your assignment submission.