Answer these 3 questions using the C++ program below. 1. Explain the logical error you have encountered? 2. What's causi

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Answer these 3 questions using the C++ program below. 1. Explain the logical error you have encountered? 2. What's causi

Post by answerhappygod »

Answer these 3 questions using the C++ program below.
1. Explain the logical error you have encountered?
2. What's causing the logical error?
3. How did you debug the program to correct the logic?
#include<iostream>
using namespace std;
template<typename T>
class Stack
{
private:
T *stackArray;
int top;
int size;
bool isEmpty();
bool isFull();
void updateStack();
public:
Stack(int);
~Stack();
int getSize();
void pushIt(T);
void popIt();
void printStack();
};
template<typename T>
bool Stack<T>::isEmpty()
{
return (top==-1);
}
template<typename T>
bool Stack<T>::isFull()
{
return (top==size-1);
}
template<typename T>
int Stack<T>::getSize()
{
return (top+1);
}
template<typename T>
void Stack<T>::updateStack()
{
this->size=getSize()-1;
T *temp=new T[size];
for(int i=0;i<size;i++)
temp=stackArray;
delete[] stackArray;
stackArray=new T[size];
for(int i=0;i<size;i++)
stackArray=temp;
delete[] temp;
}
template<typename T>
Stack<T>::Stack(int size)
{
this->stackArray=new T[size];
this->size=size;
this->top=-1;
}
template<typename T>
Stack<T>::~Stack()
{
delete[] stackArray;
}
template<typename T>
void Stack<T>::pushIt(T item)
{
if(!isFull())
{
cout<<"Pushing"<<item<<"!"<<endl;
stackArray[top++]=item;
}
else
{
cout<<"Stack isfull!"<<endl;
}
}
template<typename T>
void Stack<T>::popIt()
{
if(!isEmpty())
{
cout<<"Popping"<<stackArray[top]<<"!"<<endl;
//stackArray[top--];
updateStack();
top--;
}
else
{
cout<<"Stack isempty!"<<endl;
}
}
template<typename T>
void Stack<T>::printStack()
{
if(!isEmpty())
{
cout<<"Stack contents: |";
for(int i=0;i<size;i++)
{
cout<<stackArray<<" | ";
}
cout<<endl;
}
else
{
cout<<"Stack isempty!"<<endl;
}
}
int main()
{
int size, ctr=0, option;
double item;
bool exitFlag=false;
cout<<"Specify the size of the stack: ";
cin>>size;
Stack<double> myStack(size);
do
{
cout<<"\n[1] Push anitem."<<endl
<<"[2] Pop anitem."<<endl
<<"[3] Printstack."<<endl
<<"[4]Exit."<<endl
<<"Option:";
cin>>option;
switch(option)
{
case 1:
do
{
cout<<"Enter an item to push: ";
cin>>item;
myStack.pushIt(item);
ctr++;
}while(ctr<myStack.getSize());
break;
case 2:
myStack.popIt();
break;
case 3:
myStack.printStack();
break;
case 4:
exitFlag=true;
break;
default:
cout<<"Invalid selection! Try again!"<<endl;
}
}while(exitFlag!=true);
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply