Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'ve posted variations of this question three times over the last few we

ID: 3621250 • Letter: H

Question

Hello, I've posted variations of this question three times over the last few weeks. I've gotten some helpful answers, but my knowledge in C is not that great to make use of the answers.

I would appreciate somebody editing this code so that it compiles - and if you do, I will definitely rate "Lifesaver".

Here are two of the problems (there are probably lots more):

1.
When I try compiling and running,in the main function, the towersofHanoi(n, 1, 2, 3) shows "[Warning] passing arg 2 (and 3,4) of 'towersofHanoi' makes pointer from integer without a cast

2.
For all the functions after the main function, I use "return 0" for "return false", and "return 1" for "return true". However, I know I can't just replace "true" with "1" and need to state something else.

If somebody could edit the code so that at least these two thing are fixed, I would really appreciate it.


Here is the code:
//////////////////////////////////////////////////////////////////////////


#include // Includes the standard input/output library
#include // Includes the standard library

#define MAX_SIZE 7 // Number of disks
struct STACK{ // Stack structure
int top;
int elements[MAX_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();

void towersofHanoi(int n, stack *from, stack *aux, stack *to);

//Function prototype to print the state of the game

//Main function*************************************************************************

int main(void) {
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);
}




//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 0;
}
s->elements[s->top] = i;
s->top++;
return 1;
}

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 1;
else
return 0;
}

int isfull(stack s){ //Function to notify that stack is full
if(s.top < MAX_SIZE - 1){
return 0;
}
else
return 1;
}



Explanation / Answer

I tried my best removed the errors but dont know if your algorithm is correct. I dont know how it works, Best of luck #include #include using namespace std; #define MAX_SIZE 7 // Number of disks struct STACK{ // Stack structure int top; int elements[MAX_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(int n, stack *from, stack *aux, stack *to); void towersofHanoi(int n, stack *from, stack *aux, stack *to); //Function prototype to print the state of the game //Main function************************************************************************* int _tmain(int argc, _TCHAR* argv[]){ const int n =7; // Number of disks that are going to be used. But isn't that already defined at the start? //stack one, two, three; stack *one = (stack *)malloc(sizeof(int)*8); stack *two = (stack *)malloc(sizeof(int)*8); stack *three = (stack *)malloc(sizeof(int)*8); init_stack(*one,"One"); init_stack(*two,"two"); init_stack(*three,"three"); towersofHanoi(n, one, two, three); } //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(n, from,aux,to); 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 0; } s->elements[s->top] = i; s->top++; return 1; } 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 1; else return 0; } int isfull(stack s){ //Function to notify that stack is full if(s.top
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote