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(); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.
Transcribed Image Text: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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education