I am struggling to write the push and pop functions for a stack of arrays. I know how to write the code to push single items unto a stack and pop them out. Some have suggested that I use an array of arrays (two-dimensional array) to load the array of integers unto the stack. The array is of length 7 and there are 25 of these arrays that need to be pushed onto a stack and then popped out.

Usually, in my fillStack function, I read an element from a file and pass it to the push function. This time around, I read every element in the push function. I wrote the code below for the push function. It looks like an ordinary two dimensional array code. I am not sure if that will give me the results I want. When I began to consider how to revise the code to write the pop function, I realized I may be way off. I'll appreciate any assistance. Please include any important points I should take a note of when implementing a stack of arrays. Thanks.

void Stack::pushStack()
{ 
     int i, elem;
     
     if (!isFullStack())
     {
        for (i = 0; i < numRows; i++)
        {
            for (j = 0; j < numCols; j++)
            {
                infile >> elem;
                stkArray[i][j] = elem;        
            }
            //Just to check to make sure it's the right size/length 
            //of array is being loaded into the stack
            outfile << "Stack row: " << stkArray << endl;
        }
     }
    
}//end pushStack

I am struggling to write the push and pop functions for a stack of arrays. I know how to write the code to push single items unto a stack and pop them out. Some have suggested that I use an array of arrays (two-dimensional array) to load the array of integers unto the stack. The array is of length 7 and there are 25 of these arrays that need to be pushed onto a stack and then popped out.

Usually, in my fillStack function, I read an element from a file and pass it to the push function. This time around, I read every element in the push function. I wrote the code below for the push function. It looks like an ordinary two dimensional array code. I am not sure if that will give me the results I want. When I began to consider how to revise the code to write the pop function, I realized I may be way off. I'll appreciate any assistance. Please include any important points I should take a note of when implementing a stack of arrays. Thanks.

void Stack::pushStack()
{ 
     int i, elem;
     
     if (!isFullStack())
     {
        for (i = 0; i < numRows; i++)
        {
            for (j = 0; j < numCols; j++)
            {
                infile >> elem;
                stkArray[i][j] = elem;        
            }
            //Just to check to make sure it's the right size/length 
            //of array is being loaded into the stack
            outfile << "Stack row: " << stkArray << endl;
        }
     }
    
}//end pushStack

So you are using a 2d dynamic array and are wondering how to use free or delete to pop_back?

So you are using a 2d dynamic array and are wondering how to use free or delete to pop_back?

I am creating a stack of arrays using a 2dimensional array. I don't want it to be dynamic with pointers. We were told not to use pointers. So I would like to know how to write the correct code to implement the push and pop functions using 2d arrays

The value_type of your stack (the type of elements it contains) is a fixed-size array. Define a member in your stack class to hold up to some max number of such elements, and keep track of the current logical size programmatically.

Something like:

template< typename T, std::size_t ARRAY_SIZE, std::size_t MAX_STACK_SIZE = 1024 >
struct stack_of_arrays
{
    typedef T value_type[ ARRAY_SIZE ] ;

    stack_of_arrays() : curr_size(0) {} // initialize to empty stack

    void push( const value_type& a ) // push an array T[ARRAY_SIZE]
    {
        if( curr_size == MAX_STACK_SIZE ) throw "stack is full" ;
        for( std::size_t i = 0 ; i < ARRAY_SIZE ; ++i ) stk[curr_size][i] = a[i] ;
        ++curr_size ;
    }

    void pop( const value_type& a ) // pop the array on top of the stack
    {
        if( curr_size == 0 ) throw "stack is empty" ;
        --curr_size ;
        //for( std::size_t i = 0 ; i < ARRAY_SIZE ; ++i ) stk[curr_size][i] = T() ; // if required
    }

    value_type stk[MAX_STACK_SIZE] ;
    std::size_t curr_size /* = 0 */ ;

    // TODO: add other stack operations like size, empty, top etc.
};

The value_type of your stack (the type of elements it contains) is a fixed-size array. Define a member in your stack class to hold up to some max number of such elements, and keep track of the current logical size programmatically.

Something like:

template< typename T, std::size_t ARRAY_SIZE, std::size_t MAX_STACK_SIZE = 1024 >
struct stack_of_arrays
{
    typedef T value_type[ ARRAY_SIZE ] ;

    stack_of_arrays() : curr_size(0) {} // initialize to empty stack

    void push( const value_type& a ) // push an array T[ARRAY_SIZE]
    {
        if( curr_size == MAX_STACK_SIZE ) throw "stack is full" ;
        for( std::size_t i = 0 ; i < ARRAY_SIZE ; ++i ) stk[curr_size][i] = a[i] ;
        ++curr_size ;
    }

    void pop( const value_type& a ) // pop the array on top of the stack
    {
        if( curr_size == 0 ) throw "stack is empty" ;
        --curr_size ;
        //for( std::size_t i = 0 ; i < ARRAY_SIZE ; ++i ) stk[curr_size][i] = T() ; // if required
    }

    value_type stk[MAX_STACK_SIZE] ;
    std::size_t curr_size /* = 0 */ ;

    // TODO: add other stack operations like size, empty, top etc.
};

I translated your code into the following:

//Function to add a new item to the stack
void Stack::pushStack(int item[])
{ 
     int i;

    if(isFullStack())
        StackError("Stack is full \n");
    else
    {
            for(i = 0; i < arraySize; i++)
            {
                  stkArray[stackTop][i] = item[i];
            }
            stackTop++;
    }
     
    
}//end pushStack



//Function to remove the top element of the stack
void Stack::popStack(int item[])
{
     int i;
     
     if(isEmptyStack())
          StackError("Stack is Empty \n");
     else
     {
            stackTop--;
            for(i = 0; i < arraySize; i++)
            {
                  item[i] = stkArray[stackTop][i];
            }
     }
     
}// end popStack

It's working just that it's not printing the array elements the right way. It's printing more array elements (more than 25 elements which is the limit) at a time. It's also not printing the array elements in the right order. It's printing some random array elements also. Please take a look at my fillStack (enterData) function and my print function to see if I'm doing something wrong. Thanks.

//Fills a stack
void enterData(Stack &stk)
{
     int i, j, array[arraySize];
     
     for (i = 0; i < 25; i++)
     {
         for (j = 0; j < 7; j++)
         {
             infile >> array[j];
         } 

         if (!stk.isFullStack())
         {
             stk.pushStack(array);
             
         }
        
     }
}//end enterData
//Prints stack
void printStack(Stack &stk)
{
     Stack tempStk;
     int elemArray[arraySize], tempArray[arraySize];
     
     while (!stk.isEmptyStack())
     {
           stk.popStack(elemArray);
           tempStk.pushStack(elemArray);
   
              for (int j = 0; j < 25; j++)
              {
                   outfile << elemArray[j] << endl;
              }
         
     }

     while (!tempStk.isEmptyStack())
     {
           tempStk.popStack(tempArray);
           stk.pushStack(tempArray);         
     }
}//end printStack
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.