I am writing a stack code, where there are two stacks that reads an array of 40 numbers and puts them in ascending and descending order. I put my code on the bottom. The requirements of the code was to write the stack functions using arrays, but whenever i run the code it does not work. However, if use stack in built function the code works. Help me please. PS: I Attached the code requirements. struct Stack1 { int *arr; int NextIndex; int capacity; Stack1() { capacity = 20; arr = new int[capacity]; NextIndex = 0; } int size () { return NextIndex; } bool isEmpty() { if (NextIndex == 0) { return true; } else return false; } void push(int ele) { if (NextIndex == capacity) { int *newArr = new int[2*capacity]; for(int i = 0; i < capacity; i++) { newArr[i] = arr[i]; } delete []arr; arr = newArr; capacity = 2*capacity; } arr[NextIndex] = ele; NextIndex++; } void pop() { if (isEmpty()) { cout << "The stack is in underflow"<< endl; return; } NextIndex--; } int top() { if (isEmpty()) { cout << "The stack is in underflow"<< endl; return -1; } return arr[NextIndex - 1]; } void printStack() { if (isEmpty()) return; else { for (int i = 0; i < capacity; i ++) { cout << arr[i]; } } } }; struct Stack2 { int *arr; int NextIndex; int capacity; //using dynamic array here Stack2() { capacity = 20; arr = new int[capacity];// if the user does not define the size, the code will give a size of 4 automatically NextIndex = 0; } int size () { return NextIndex; } bool isEmpty() { if (NextIndex == 0) { return true; } else return false; } void push(int ele) { if (NextIndex == capacity) { int *newArr = new int[2*capacity]; for(int i = 0; i < capacity; i++) { newArr[i] = arr[i]; } delete []arr; arr = newArr; capacity = 2*capacity; } arr[NextIndex] = ele; NextIndex++; } void pop() { if (isEmpty()) { cout << "The stack is in underflow"<< endl; return; } NextIndex--; } int top() { if (isEmpty()) { cout << "The stack is in underflow"<< endl; return -1; } return arr[NextIndex - 1]; } }; void printsortedArray() { Stack1 s1; Stack2 s2; if(s1.isEmpty()) { cout << "\n Sorted values in ascending order:"; while (!s2.isEmpty()) { cout << s2.top() << " "; s1.push(s2.top()); s2.pop(); } cout << "\n Sorted values in descending order: "; while (!s1.isEmpty()) { cout << s1.top() << " "; s1.pop(); } } else { cout << "\n Sorted values in descending order: "; while (!s1.isEmpty()) { cout << s1.top()<< " "; s2.push(s1.top()); s1.pop(); } cout << "\n Sorted values in ascending order: "; while (!s2.isEmpty()) { cout << s2.top() << " "; s2.pop(); } } } void s1Add(int n) { Stack1 s1; Stack2 s2; cout << "\n Content of working stack: "; int flag = 0; while (!s2.isEmpty()) { int curr = s2.top(); cout << curr << " "; if (curr < n || flag == 1) s1.push(curr); else { flag = 1; s1.push(n); s1.push(curr); } s2.pop(); } if (flag == 0) s1.push(n); } void s2Add(int n) { Stack1 s1; Stack2 s2; cout << "\n Content of the non-working stack : "; int flag = 0; while (!s1.isEmpty()) { int curr = s1.top(); cout << curr << " "; if (curr > n || flag == 1) s2.push(curr); else { flag = 1; s2.push(n); s2.push(curr); } s1.pop(); } if (flag == 0) s2.push(n); } int main () { int A[40]; int i; Stack1 s1; Stack2 s2; ifstream numbers ("Data_FIle.txt"); for (i = 0; i < 40; i++) { numbers >> A[i]; } for (int i = 0; i < 40; i++) { int ele = A[i]; if (i == 0) s1.push(ele); else if (s2.isEmpty()) s2Add(ele); else s1Add(ele); } printsortedArray(); }
I am writing a stack code, where there are two stacks that reads an array of 40 numbers and puts them in ascending and descending order. I put my code on the bottom. The requirements of the code was to write the stack functions using arrays, but whenever i run the code it does not work. However, if use stack in built function the code works. Help me please.
PS: I Attached the code requirements.
struct Stack1
{
int *arr;
int NextIndex;
int capacity;
Stack1()
{
capacity = 20;
arr = new int[capacity];
NextIndex = 0;
}
int size ()
{
return NextIndex;
}
bool isEmpty()
{
if (NextIndex == 0)
{
return true;
}
else
return false;
}
void push(int ele)
{
if (NextIndex == capacity)
{
int *newArr = new int[2*capacity];
for(int i = 0; i < capacity; i++)
{
newArr[i] = arr[i];
}
delete []arr;
arr = newArr;
capacity = 2*capacity;
}
arr[NextIndex] = ele;
NextIndex++;
}
void pop()
{
if (isEmpty())
{
cout << "The stack is in underflow"<< endl;
return;
}
NextIndex--;
}
int top()
{
if (isEmpty())
{
cout << "The stack is in underflow"<< endl;
return -1;
}
return arr[NextIndex - 1];
}
void printStack()
{
if (isEmpty())
return;
else
{
for (int i = 0; i < capacity; i ++)
{
cout << arr[i];
}
}
}
};
struct Stack2
{
int *arr;
int NextIndex;
int capacity;
//using dynamic array here
Stack2()
{
capacity = 20;
arr = new int[capacity];// if the user does not define the size, the code will give a size of 4 automatically
NextIndex = 0;
}
int size ()
{
return NextIndex;
}
bool isEmpty()
{
if (NextIndex == 0)
{
return true;
}
else
return false;
}
void push(int ele)
{
if (NextIndex == capacity)
{
int *newArr = new int[2*capacity];
for(int i = 0; i < capacity; i++)
{
newArr[i] = arr[i];
}
delete []arr;
arr = newArr;
capacity = 2*capacity;
}
arr[NextIndex] = ele;
NextIndex++;
}
void pop()
{
if (isEmpty())
{
cout << "The stack is in underflow"<< endl;
return;
}
NextIndex--;
}
int top()
{
if (isEmpty())
{
cout << "The stack is in underflow"<< endl;
return -1;
}
return arr[NextIndex - 1];
}
};
void printsortedArray()
{
Stack1 s1;
Stack2 s2;
if(s1.isEmpty())
{
cout << "\n Sorted values in ascending order:";
while (!s2.isEmpty())
{
cout << s2.top() << " ";
s1.push(s2.top());
s2.pop();
}
cout << "\n Sorted values in descending order: ";
while (!s1.isEmpty())
{
cout << s1.top() << " ";
s1.pop();
}
}
else
{
cout << "\n Sorted values in descending order: ";
while (!s1.isEmpty())
{
cout << s1.top()<< " ";
s2.push(s1.top());
s1.pop();
}
cout << "\n Sorted values in ascending order: ";
while (!s2.isEmpty())
{
cout << s2.top() << " ";
s2.pop();
}
}
}
void s1Add(int n)
{
Stack1 s1;
Stack2 s2;
cout << "\n Content of working stack: ";
int flag = 0;
while (!s2.isEmpty())
{
int curr = s2.top();
cout << curr << " ";
if (curr < n || flag == 1)
s1.push(curr);
else
{
flag = 1;
s1.push(n);
s1.push(curr);
}
s2.pop();
}
if (flag == 0)
s1.push(n);
}
void s2Add(int n)
{
Stack1 s1;
Stack2 s2;
cout << "\n Content of the non-working stack : ";
int flag = 0;
while (!s1.isEmpty())
{
int curr = s1.top();
cout << curr << " ";
if (curr > n || flag == 1)
s2.push(curr);
else
{
flag = 1;
s2.push(n);
s2.push(curr);
}
s1.pop();
}
if (flag == 0)
s2.push(n);
}
int main ()
{
int A[40];
int i;
Stack1 s1;
Stack2 s2;
ifstream numbers ("Data_FIle.txt");
for (i = 0; i < 40; i++)
{
numbers >> A[i];
}
for (int i = 0; i < 40; i++)
{
int ele = A[i];
if (i == 0)
s1.push(ele);
else if (s2.isEmpty())
s2Add(ele);
else
s1Add(ele);
}
printsortedArray();
}
![Write a program that will sort a set of numbers. The program will use two array-based stacks.
The stack that is being used at a given instant is called the working stack. When a new number is
read, the non-working stack will be empty. One of the stacks will be arranged with the smallest
element of the stack on the bottom, the other with the smallest element at the top.
To process a number, test it against the top element of the "current" working stack. If it fits there,
push it. If it does not fit, pop the top element of the working stack, push it on to the other stack,
and continue testing. When the number "fits", push it on the other stack and empty the working
stack onto the other stack. When the working stack is empty, the other stack becomes the new
working stack.
Use the stack definitions and functions discussed in class. Do not manipulate the stacks except
by calling the stack functions. Your main function should contain mostly function calls. Do not
duplicate logic. Follow all good programming techniques.
INPUT:
1. Assume that each stack holds at most 40 values.
2. Fill in your own data file containing at least 30 non-sorted integers.
3. Your program should be able to handle any data file of integers.
OUTPUT:
Print the contents of the working stack after each number is added.
NOTE: All output should have appropriate headers and be completely formatted.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F936674a8-46b2-4872-b00b-38bd88e95e72%2F8bb9a455-0a37-4354-af80-3d52a88f3fec%2Fhab5ii6_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)