Here is the instructions: 1. Modify the printQueue() function from your queue im
ID: 3621494 • Letter: H
Question
Here is the instructions:1. Modify the printQueue() function from your queue implementation in HW1 so that it uses recursion. Remember, a recursive function has a base (or ground) case and a recursive case.
2. The greatest common denominator is mathematically defined by the following function:
m if m <= n and n mod m =0
gcd(n,m) = { gcd(m,n) if n < m
gcd(m, n mod m) otherwise
Provide the implementation details for the gcd()function provided in the gcd.cpp file. Test your implementation using the provided main()function.
Note: The mod function in C++ is the percent sign. For example, 10 mod 2 would be
written as 10 % 2 in your program.
The output to this program should appear as follows:
gcd(2, 4) is 2
gcd(10, 3) is 1
gcd(516, 24) is 12
gcd(4, 64) is 4
gcd(64, 832) is 64
Here is the code that I have:
This is gcd.cpp
#include <iostream>
using namespace std;
int gcd(int n, int m)
{
// TODO: provide the implementation for the gcd()
// function here. Your function should implement the
// math definition provided in the handout.
}
/*
* Function: main()
* Description: Test driver for the h() functoin.
*/
int main(int argc, char **argv)
{
cout << "gcd(2, 4) is " << gcd(2, 4) << endl;
cout << "gcd(10, 3) is " << gcd(10, 3) << endl;
cout << "gcd(516, 24) is " << gcd(516, 24) << endl;
cout << "gcd(4, 64) is " << gcd(4, 64) << endl;
cout << "gcd(64, 832) is " << gcd(64, 832) << endl;
return 0;
}
This is the driver.cpp for Homework 1
#include <iostream>
#include "Queue.h"
using namespace std;
int main(int argc, char **argv)
{
Queue queue(10);
queue.properties();
// Add some nodes to the queue
queue.enqueue("The");
queue.enqueue("quick");
queue.enqueue("brown");
queue.enqueue("fox");
queue.enqueue("ran");
queue.dequeue();
queue.enqueue("jumped");
queue.properties();
queue.print();
// Clear the queue
queue.clear();
queue.properties();
queue.print();
// Add more nodes than the queue can hold
queue.enqueue("over");
queue.enqueue("the");
queue.enqueue("lazy");
queue.enqueue("dog");
queue.properties();
queue.print();
return 0;
}
This is my queue.h file from Homework 1:
#include <string>
using namespace std;
class Queue
{
public:
Queue(int size);
~Queue();
void clear();
bool isEmpty();
void enqueue(string element);
string dequeue();
string getFirstElement();
int getLength();
int getSize();
void properties();
void print();
private:
int size; // The total number of items the queue can hold
int length; // The current number of items in the queue
string *queue; // Holds a queue of strings
};
/*
* The Queue class implementatoin begins here.
*/
Queue::Queue(int size)
{
this->size= size;
queue = new string[size];
clear();
}
Queue::~Queue()
{
size = -1;
length = -1;
delete [] queue;
}
void Queue::clear()
{
// Set every element in the queue memory to the
// empty string
for (int i=0; i < size; i++)
{
queue[i] = "";
}
// Set the length to -1, which means empty
length = -1;
return;
}
bool Queue::isEmpty()
{
// Return true when the length is at least equal to -1. For safety
// also assume queue is empty when it is less than -1.
if (length <= -1)
return true;
else
return false;
}
void Queue::enqueue(string element)
{
// Add the element to the queue only if the next length value
// does not exceed the size of the queue.
if (length+1 < size)
{
queue[++length] = element;
}
return;
}
string Queue::dequeue()
{
// Temporary integer to hold the dequeued element
string element = "";
// If the queue is not empty dequeue the first item in the queue.
if (!isEmpty())
{
// Set the temporary integer equal to the first item in the
// queue.
element = queue[0];
// Move the proceeding elements in the queue down by 1 position
// in the queue. This dequeues the element.
for (int i=0; i < length+1; i++)
{
queue[i] = queue[i+1];
}
length--;
}
return element;
}
string Queue::getFirstElement()
{
string element = "[None]";
// Return the first element in the queue without dequeuing it,
// if the queue is not empty. If it the queue is empty
// return the string "[None]".
if (!isEmpty())
{
element = queue[0];
}
return element;
}
int Queue::getLength()
{
return (length+1);
}
int Queue::getSize()
{
return size;
}
void Queue::properties()
{
cout << "Queue properties:" << endl;
cout << " isEmpty = " << isEmpty() << endl;
cout << " length = " << getLength() << endl;
cout << " size = " << getSize() << endl;
cout << " first element = " << getFirstElement() << endl;
cout << endl;
return;
}
void Queue::print()
{
cout << "Queue elements:" << endl;
if (isEmpty())
{
cout << " The queue is empty!" << endl;
cout << endl;
return;
}
while (isEmpty() == false)
{
string element = dequeue();
cout << " " << element << endl;
if (element.length() == 0)
break;
}
cout << endl;
return;
}
Help please! I am so lost with this one.