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.
Output: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(); }
Error outputs many times before any numbers:
ERROR
ERROR
ERROR
1234
5678
9101112
13141516
17181920