Redesign C++ the program below so that it contains a Grow function that will cre
ID: 3598292 • Letter: R
Question
Redesign C++ the program below so that it contains a Grow function that will create a new array twice the size of the current one and copy the contents from the original array to the new one. Then, make the new array have the original array's name.
#include<iostream>
using namespace std;
class circularQueue
{
public:
int a[100],n,f,r,flag;
void display()
{
if(flag==0)
{
cout << " Showing the Queue & F,R ";
flag=1;
}
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << " F=" << f << " & R=" << r<<endl<<endl;
}
void qinsert(int item)
{
if ((r == n&&f == 1) || (f == r + 1))
{
cout << "Overflow Error Queue is already full ";
return;
}
if (f == 0 || r == 0)
{
f = 1;
r = 1;
}
else if (r == n)
{
r = 1;
}
else
r = r + 1;
a[r-1] = item;
}
int qout()
{
int item;
if (f == 0)
{
cout << "----->Error Queue is empty ";
return 100000;
}
else
{
item = a[f-1];
if (f == r)
{
f = 0;
r = 0;
}
else
{
f = f + 1;
if (f > n)
f = 1;
}
}
return item;
}
}q;
int main()
{
cout << "Enter the size of the queue ";
cin >> q.n;
if(q.n>100)
{
cout<<"Enter size smaller than 100";
cin>>q.n;
}
cout<<"----------------------------------------------------------------------------------------------------------";
cout << "To Insert, Press I and then after a space the element To Delete, just press D Press ; to exit ";
char what='}';
int item,flag=0;
do
{
cin >> what;
if (what == 'I'||what=='i')
{
cin >> item;
q.qinsert(item);
}
else if (what == 'D'||what=='d')
{
if(flag==0)
item = q.qout();
if(item!=100000)
cout << "-----> Element popped out is: " << item << endl;
flag = 1;
}
q.display();
} while (what != ';');
cout << "Thank You, Have a nice Day ";
return 0;
}
#include using namespace std; class circularQueue { public: int a[100],n,f,r,flag; void display() { if(flag==0) { cout << " Showing the Queue & F,R "; flag=1; } for (int i = 0; i < n; i++) cout << a[i] << " "; cout << " F=" << f << " & R=" << r<error ="" queue="" is="" empty ";="" return="" 100000;="" }="" else="" {="" item="a[f-1];" if="" (f="=" r)="" f="0;" r="0;" +="" 1;=""> n) f = 1; } } return item; } }q; int main() { cout << "Enter the size of the queue "; cin >> q.n; if(q.n>100) { cout<<"Enter size smaller than 100"; cin>>q.n; } cout<<"----------------------------------------------------------------------------------------------------------"; cout << "To Insert, Press I and then after a space the element To Delete, just press D Press ; to exit "; char what='}'; int item,flag=0; do { cin >> what; if (what == 'I'||what=='i') { cin >> item; q.qinsert(item); } else if (what == 'D'||what=='d') { if(flag==0) item = q.qout(); if(item!=100000) cout << "-----> Element popped out is: " << item << endl; flag = 1; } q.display(); } while (what != ';'); cout << "Thank You, Have a nice Day "; return 0; }
Explanation / Answer
Here is the redesigned code with the grow function
#include<iostream>
using namespace std;
class circularQueue
{
public:
int n, f, r, flag;
int* a = NULL;
void setSize() {
a = new int[n];
}
circularQueue() {}
~circularQueue() {
delete[] a;
}
void grow() {
int new_size = n * 2;
int* b = new int[n];
// copy elements from a to b
for (int i = 0; i < n; i++)
{
b[i] = a[i];
}
delete[] a;
a = new int[new_size];
//again copy the old elements of a into a from b
for (int i = 0; i < n; i++)
{
a[i] = b[i];
}
delete[] b;
}
void display()
{
if (flag == 0)
{
cout << " Showing the Queue & F,R ";
flag = 1;
}
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << " F=" << f << " & R=" << r << endl << endl;
}
void qinsert(int item)
{
if ((r == n&&f == 1) || (f == r + 1))
{
cout << "Overflow Error Queue is already full ";
return;
}
if (f == 0 || r == 0)
{
f = 1;
r = 1;
}
else if (r == n)
{
r = 1;
}
else
r = r + 1;
a[r - 1] = item;
}
int qout()
{
int item;
if (f == 0)
{
cout << "----->Error Queue is empty ";
return 100000;
}
else
{
item = a[f - 1];
if (f == r)
{
f = 0;
r = 0;
}
else
{
f = f + 1;
if (f > n)
f = 1;
}
}
return item;
}
};
int main()
{
circularQueue q;
cout << "Enter the size of the queue ";
cin >> q.n;
if (q.n <= 0)
{
cout << "Enter a positive number greater than 0";
cin >> q.n;
}
q.setSize();
cout << "----------------------------------------------------------------------------------------------------------";
cout << "To Insert, Press I and then after a space the element To grow press G To Delete, just press D Press ; to exit ";
char what = '}';
int item, flag = 0;
do
{
cin >> what;
if (what == 'I' || what == 'i')
{
cin >> item;
q.qinsert(item);
}
else if (what == 'D' || what == 'd')
{
if (flag == 0)
item = q.qout();
if (item != 100000)
cout << "-----> Element popped out is: " << item << endl;
flag = 1;
}
else if (what == 'G' || what == 'g')
{
q.grow();
}
q.display();
} while (what != ';');
cout << "Thank You, Have a nice Day ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.