Thread: Three Stack array:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Three Stack array:

    I bought this book with different programming idea. I am trying to solve one:

    Make a three stack array. Basically push numbers into a stack and when it is full move on to the next stack.

    This is what I have so far but on lines 29 and 32 my compiler is saying it is out of bounds which is rather confusing.

    Code:
    #include <iostream>
    
    #define row 10
    #define col 2
    
    int stack[row][col];
    
    //place numbers into the first row of array
    void push(int num)
    {
        
        //check first column if it is full
        for(int i=0; i<row;i++)
        {
            if(stack[i][col-2]==0)
            {
                stack[i][col-2]=num;
                break;
            }
            //checks if first stack is empty and if second stack
            //is empty
            else if(stack[i][col-1]==0 && stack[i][col-2] > 0)
            {
                stack[i][col-1]=num;
                break;
            }
            //checks if first stack is empty and if second stack
            //is empty then check third stack
            else if(stack[i][col]==0 && stack[i][col-2] > 0
                    && stack[i][col-1]>0)
            {
                stack[i][col]=num;
                break;
            }
            else
            {
                std::cout<<"ERROR"<<"\n";
            }
        }
    }
    
    void pop()
    {
        int count=1;
        
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                if(count==3)
                {
                    //makes a line every third iteration
                    std::cout<<"\n";
                    count=1;//setting count back to 0
                }
                std::cout<<stack[i][j];
            }
            count++;
        }
    }
    int main(int argc, const char * argv[])
    {
        
                for(int i=0;i<30;i++)
                {
                    push(i);
                }
                pop();
    
    }
    Output:
    Error outputs many times before any numbers:
    ERROR
    ERROR
    ERROR
    1234
    5678
    9101112
    13141516
    17181920
    Last edited by jocdrew21; 05-17-2014 at 12:56 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You should start looking into arrays in C. Specifically, you should research how arrays in C are sized/indexed.

    That said, you should write a "one stack array" before trying a "three stack array".

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    You say I am iterating through the array incorrectly? This is what I am doing with the above code:

    Code:
    //for loop here
    stack[iterate rows][static col]; //iterate column one
    
    stack[iterate rows][static col + 1]; //iterate column two
    
    stack[iterate rows][static col + 2]; //iterate column three
    I use if statements to check if each column is completely empty, if not the inputed number is placed into that location in the multi array. Once the number is placed a break statement is executed and the loop ends.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    //iterate column three
    As already pointed out you need to study up a little more on arrays. You created your array with a size of 2 so you only have two columns, not 3. Also in your code you are trying to access your array out of bounds when you use an array index of col. Remember arrays start at zero.

    Also why do you assume that zero is the same as an empty Stack? Since this is a C++ program why not make your Stack a class?

    Jim

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <stack>
    #include <array>
    
    using namespace std;
    
    struct node
    {
        stack<int> myStack;
    };
    
    class ArrayStack
    {
    private:
        int count;
        array<node, 3> myArray;
       
    public:
        ArrayStack();
        void pushOntoArray(int);
        void popOffArray();
    };
    
    ArrayStack::ArrayStack()
    {
        count=0;
    }
    
    void ArrayStack::pushOntoArray(int num)
    {
        node n;
        
        while (n.myStack.size()!=10)
        {
            n.myStack.push(num);
        }
        if (myArray.size()!= 3 && n.myStack.size()==10)
        {
           //jumps out of while loop and assigned node to array
           myArray[count++]=n;
            
            //creates new stack
            n.myStack= *new stack<int>;
            
            //pushes onto new stack
            n.myStack.push(num);
        }
         
    }
    
    void ArrayStack::popOffArray()
    {
        node n;
        for (int i=0; i<myArray.size(); i++)
        {
            for (int j=0; j<n.myStack.size(); j++)
            {
                int value = myArray[i].myStack.pop();  //here is the error
                
                cout<<value<<"\n";
            }
        }
    }
    
    int main(int argc, const char * argv[])
    {
        ArrayStack AStack;
        
        for (int i =0; i<30; i++)
        {
            AStack.pushOntoArray(i);
        }
        
        AStack.popOffArray();
        
        return 0;
    }
    line 61 is the error. I am trying a different approach to the three stack array problem. This one is failing too
    Last edited by jocdrew21; 05-18-2014 at 10:24 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you would bother to read the documentation for std::stack you should see that pop() doesn't return anything, hence the void return type.


    Jim

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    top()

    Sorry...

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    This is odd, now when I debug the program this function is only emplacing 0's?
    I am not getting any output from the program?

    Code:
    int main(int argc, const char * argv[])
    {
        ArrayStack AStack;
        
        for (int i=0; i<30; i++)
        {
            AStack.pushOntoArray(i);
        }
        
        AStack.popOffArray();
        
        return 0;
    }

    this is the update to the other code issue
    Code:
    void ArrayStack::popOffArray()
    {
        node n;
        for (int i=0; i<myArray.size(); i++)
        {
            for (int j=0; j<n.myStack.size(); j++)
            {
                int value = myArray[i].myStack.top();
                myArray[i].myStack.pop();
                
                cout<<value<<"\n";
            }
        }
    }
    Last edited by jocdrew21; 05-18-2014 at 10:26 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are doing very weird things such as constructing new stacks when there already exists one, assigning to a temporary variable, etc.
    Why don't you just do a simple exercise? Push 5 ints to a std::stack, then pop those 5 and print them?
    Take it baby steps.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    its a question in a book I found. I thought it was tricky and interesting so I thought I would give it a shot.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nothing wrong with that, but seeing as you don't seem to grasp how to basic things right, how about doing it in small steps to get the hang of it first?
    After that you can put it together to create the complete exercise.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by jocdrew21 View Post
    its a question in a book I found. I thought it was tricky and interesting so I thought I would give it a shot.
    I don't necessarily know what question you are trying to answer or what book it's from. Elysia's advice isn't bad. At the same time, even if you leave, learn about stacks, and come back, I still won't understand what you're really doing.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I understand stacks and it is a programming interview book. The question asked for an array of three stacks. I know there is no real use for the program, I just wanted to see if I could do it.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you overworked it then. I still don't know what the question is, but std::array<std::stack<int>, 3> is an array of three stacks. The stacks handle ints in this case, but there you go.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you still fail to even use the stacks correctly. So again, why not do something simple, get it working and build from there instead of starting from complexity?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack array
    By rafay_07 in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2011, 05:00 AM
  2. stack and array..
    By zackysue in forum C Programming
    Replies: 6
    Last Post: 11-05-2009, 08:19 AM
  3. Dynamic array in Stack ?
    By janaka in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 12:19 AM
  4. Stack Based Array?
    By suckerpunch678 in forum C++ Programming
    Replies: 11
    Last Post: 04-01-2005, 02:23 AM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM