I am trying to fix this bug but keep having issues. Here is the problem in its original form. #include using namespace s
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
I am trying to fix this bug but keep having issues. Here is the problem in its original form. #include using namespace s
I am trying to fix this bug but keep having issues. Here is theproblem in its original form. #include using namespace std; #defineMAX_LEVELS 10 // Declare three pole positions, or rather, stacks.// Each stack is an object containing ring values. // stacks[3] isan array three of these objects. class Cstack { //Error #1 public:int rings[MAX_LEVELS]; // Array of ring values. int *rings; inttos; // Top-of-stack index. void populate(int size); // Initializestack. void clear(int size); // Clear the stack. void push(int n);int pop(void); ~Cstack(){ cout <<"\nDeleting *rings \n";delete [] rings; } } stacks[3]; void Cstack::populate(int size) {//Error #2 create(size); int i =1, *p = rings; while(size-- ){ *p++= i++; } tos = -1; } void Cstack::clear(int size) { create (size);int *p =rings; tos = size - 1; while (size--) *p++ =0; { } voidCstack::push(int n) { rings=rings + tos--; *(rings+ tos--) = n; }int Cstack::pop(void) { int *p = rings + (++tos), n = *p; *p =0;return n; } void move_stacks(int src, int dest, int other, int n);//Error #3 void move_a_ring(int source, int dest); voidprint_stacks(void); //Error #4 void pr_chars(int ch, int n); intstack_size = 4; //Error #5 int main() {stacks[0].populate(stack_size); stacks[1].clear(stack_size);stacks[2].clear(stack_size); print_stacks();move_stacks(stack_size, 0, 2, 1); return 0; } // Move stacks: solveproblem recursively... // move N stacks by assuming problem solvedfor N-1. // src = source stack, dest = destination stack. // voidmove_stacks(int n, int src, int dest, int other){ if (n == 1) {move_a_ring(src, dest); } else { move_stacks(n-1, src, other,dest); move_a_ring(src, dest); move_stacks(n-1, other, dest, src);} } // Move a Ring: Pop off a ring from source (src) stack, //place it on destination stack, and print new state. // voidmove_a_ring(int source, int dest) { int n = stacks[source].pop();// Pop off source. stacks[dest].push(n); // Push onto dest.print_stacks(); // Show new state. } // Print Stacks: For eachphysical level, print the // ring for each of the three stacks. //void print_stacks(void) { int n = 0; for (int i = 0; i <stack_size; i++) { for (int j = 0; j < 3; j++) { n =stacks[j].rings; pr_chars(' ', 12 - n); pr_chars('*', 2 * n);pr_chars(' ', 12 - n); } cout << endl; } system("PAUSE"); //A pause is needed here; use } // another method if you need to.void pr_chars(int ch, int n) { for (int i = 0; i < n; i++) {cout << (char) ch; } }