Hello, I posted this question yesterday and got an answer that I thought could a
ID: 3621129 • Letter: H
Question
Hello, I posted this question yesterday and got an answer that I thought could allow me to finish this problem, but alas, I don't know how to program well enough...
This Tower of Hanoi problem uses stacks (I have the code for recursive and non-recursive solutions). However, I've been spending time here and there for the past few weeks and I'm still not getting it. I found the code for most of the problem a few days ago.
Although some of the questions are in the comments in the code, here are my main ones that I need answered:
- I quickly wrote the "main function" and "print_state" function (the rest of the code is from class notes I found on the internet). I'm not correctly calling the "towersofHanoi" function, and the print_state function is probably wrong too.
-For all the functions that follow the main function, there are "return false" and "return true". "false" and "true" are not recognized by my compiler so is there other code to use? Can you use "return 0" for false and "return 1" for true?
Anyways, here is the code. I would really appreciate anyone that takes the time to make this code work so that it least prints out the steps (even if the steps are wrong).
_____________________________________________________________________________
#include // Includes the standard input/output library
#include // Includes the standard library
#define DEFAULT_SIZE 7 // Number of disks
struct stack{ // Stack structure
int top;
int elements[DEFAULT_SIZE];
};
typedef struct stack stack;
//Function prototypes******************************************************************
stack init_stack(stack s, char *name); // Function prototype to initialize stack
int push(stack *s,unsigned int i); //Function prototype for push
void dispose(stack s); //Function prototype to dispose stack
unsigned int pop(stack *s); //Function prototype for pop
int isempty(stack s); //Function prototype for empty stack
int isfull(stack s); //Function prototype for full stack
void print_state(); //Function prototype to print the state of the game
//Main function*************************************************************************
void main() // Can "main" be used even without putting int or
//something in front?
{
const n =7; // Number of disks that are going to be used. But isn't
//that already defined at the start?
towersofHanoi(n, 1,2,3); // I looked at a recursive solution for this - why
} // can 1,2,3 be used instead of from, to, aux?
//When I run the program, it says there are "conflicting
// types for "towersofHanoi"
//Functions******************************************************************************
void towersofHanoi(int n, stack *from, stack *aux, stack *to) {
if( n > 0 ) {
towersofHanoi(n-1,from,to,aux);
push(to, pop(from));
print_state();
towersofHanoi(n-1,aux,from,to);
}
}
void print_state(int n, stack *from, stack *aux, stack *to) //Function to print current
// state of game
{
printf("Move disk %d from %d to %d ", n, from, to, aux); //I'm not sure this is
// correct, but I can't check it
}
stack init_stack(stack s, char *name) { //Function to initialize stack
s.top = 0;
return s;
}
void dispose(stack s){ //Function to dispose stack
free(&s.elements);
free(&s);
}
int push(stack *s, unsigned int i) { //Function for push
if(isfull(*s)){
printf("Warning: Push on full stack ");
return false;
}
s->elements[s->top] = i;
s->top++;
return true;
}
unsigned int pop(stack *s) { //Function for pop
if(isempty(*s))
printf("Warning: Pop from empty stack ");
s->top--;
return s->elements[s->top];
}
int isempty(stack s){ //Function to notify that stack is empty
if(s.top == 0)
return true;
else
return false;
}
int isfull(stack s){ //Function to notify that stack is full
if(s.top < DEFAULT_SIZE - 1){
return false;
}
else
return true;
}
_____________________________________________________________________
Please help me:)
Thanks
Explanation / Answer
Instead of return false and return true, use return 1 for true and return 0 for false. Then, in your main, whenever you check for return values, use if(x==1) instead of if(x) for if it would be true, and if(x==0) for if it would be false. If you are writing in C, you can't use true and false as those are booleans and do not exist in C. Also, change your main declaration to int main(void) //it is good practice to make sure (void) instead of () for main, Your towersofhannoi function is like this (n,1,2,3) n represents the towers that are being manipulated. The reason you don't use the global declaration up top is because it is an unchanging variable that you don't want touched. Usually that variable will be used as a ceiling for variables, but it is not necessary in this program. 1,2,3 are all passed by reference into the function, meaning in your function you are manipulating them as local variables that will change the initial passed in parameters. You don't have declared variables from, to, etc. so therefore you can't pass them in. They don't exist. From, to, and aux are names you are giving to 1,2,3 in the order they are passed into the function. === It seems you are new to programming in general and don't have a clear concept of functions or how to use them. I would advise you to write a new source file where you translate this code into your own writing, changing return true, return false to return 1, return 0 and keeping it simple. This program you are taking is actually overcomplicated for a simple problem. Since it seems you must use a recursive function, I would say use only the recursive function as you really don't need more functions to free data or the such. Remember when setting up your recursive function to have the initial check be to see if it is ground level, in which case it will return a value of 1 or 0 instead of doing the step you are repeating, in this case moving a stone from one stack to another.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.