To make an interacting user game on *Tower of hanoi* .
-
I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.
/****************************************** TOWER OF HANOI **********************************************************/
#include
#include
#includeint find(int name);
int check(int temp[], int i);
void del(int name);
int repetetion(int temp[] , int name);
int indexing(int temp[]);int m[3], r[3], l[3];
int mp=0, rp=0, lp=3 ; //indicates the latest position occuipiedint main()
{
int i=0;
int temp=1, name;
char position, answer;//initializing to 0 while(i < 4) { l\[i\] = 4-i; m\[i\] = 0; r\[i\] = 0; } printf("\\nWelcome to the towers of hanoi. . . \\n\\n"); //looping until the left most tower have all the blocks in decending order. while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y') { //taking name of the block to move. printf("\\nEnter the block name:\\n"); scanf("%d", &name); find(name); //looping until correct block is moved. while(find(name) == 1){ printf("This block cannot be moved! Please chose again. . .\\n"); scanf("%d", &name); find(name); } //taking the position of the tower to move the block in. printf("\\n\\nEnter the position you want to move it in..\\n"); scanf("%1s", &position); while(temp=1) { if(toupper(position)=='M'){ if(find(name)==0 && (temp=repetetion(m, name))==0){ //if the block is movable and if the block is moved to different tower than the one it is in. del(name); //del the block from that tower m\[mp\] = name; //move that block to the prescripted one mp++; //filling the block-space of that tower, hence 4-mp more blocks can be inserted. continue; } } else if(toupper(position) == 'L'){ if(find(name)==0 && (temp =repetetion(l, name))==0){ del(name); l\[lp\] = name; lp++; continue; } } else if(toupper(position) == 'R'){ if(find(name)==0 && (temp = repetetion(r, name))==0){ del(name); r\[rp\] = name; rp++; continue; } } printf("Invalid entry! Please enter again. . ."); //if the desired position is already occupied scanf("%1s", &position); } if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1) { printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . ."); printf("\\n\\nWant to play again? (Y/N) "); scanf("%1s", &answ
-
I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.
/****************************************** TOWER OF HANOI **********************************************************/
#include
#include
#includeint find(int name);
int check(int temp[], int i);
void del(int name);
int repetetion(int temp[] , int name);
int indexing(int temp[]);int m[3], r[3], l[3];
int mp=0, rp=0, lp=3 ; //indicates the latest position occuipiedint main()
{
int i=0;
int temp=1, name;
char position, answer;//initializing to 0 while(i < 4) { l\[i\] = 4-i; m\[i\] = 0; r\[i\] = 0; } printf("\\nWelcome to the towers of hanoi. . . \\n\\n"); //looping until the left most tower have all the blocks in decending order. while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y') { //taking name of the block to move. printf("\\nEnter the block name:\\n"); scanf("%d", &name); find(name); //looping until correct block is moved. while(find(name) == 1){ printf("This block cannot be moved! Please chose again. . .\\n"); scanf("%d", &name); find(name); } //taking the position of the tower to move the block in. printf("\\n\\nEnter the position you want to move it in..\\n"); scanf("%1s", &position); while(temp=1) { if(toupper(position)=='M'){ if(find(name)==0 && (temp=repetetion(m, name))==0){ //if the block is movable and if the block is moved to different tower than the one it is in. del(name); //del the block from that tower m\[mp\] = name; //move that block to the prescripted one mp++; //filling the block-space of that tower, hence 4-mp more blocks can be inserted. continue; } } else if(toupper(position) == 'L'){ if(find(name)==0 && (temp =repetetion(l, name))==0){ del(name); l\[lp\] = name; lp++; continue; } } else if(toupper(position) == 'R'){ if(find(name)==0 && (temp = repetetion(r, name))==0){ del(name); r\[rp\] = name; rp++; continue; } } printf("Invalid entry! Please enter again. . ."); //if the desired position is already occupied scanf("%1s", &position); } if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1) { printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . ."); printf("\\n\\nWant to play again? (Y/N) "); scanf("%1s", &answ
If you got compiler errors and warnings, inspect them or add them to your question so that we know them. They contain the line number where the error occured. Inspect that line and the previous one(s) (some errors are sourced on previous lines and detected later). Read the error message. If you do not understand them, do some web research first. However, there is one big beginner's mistake in your code: The size of the arrays and the access to the array elements. With C/C++, arrays are accessed by zero based indexes. So the allowed indexes for a size of three are 0, 1, and 2. But your code uses also the index 3. As a result, your code will never work as expected or even crash. So change the array sizes to
int m[4], r[4], l[4];
before handling any other errors.
-
If you got compiler errors and warnings, inspect them or add them to your question so that we know them. They contain the line number where the error occured. Inspect that line and the previous one(s) (some errors are sourced on previous lines and detected later). Read the error message. If you do not understand them, do some web research first. However, there is one big beginner's mistake in your code: The size of the arrays and the access to the array elements. With C/C++, arrays are accessed by zero based indexes. So the allowed indexes for a size of three are 0, 1, and 2. But your code uses also the index 3. As a result, your code will never work as expected or even crash. So change the array sizes to
int m[4], r[4], l[4];
before handling any other errors.
-
I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.
/****************************************** TOWER OF HANOI **********************************************************/
#include
#include
#includeint find(int name);
int check(int temp[], int i);
void del(int name);
int repetetion(int temp[] , int name);
int indexing(int temp[]);int m[3], r[3], l[3];
int mp=0, rp=0, lp=3 ; //indicates the latest position occuipiedint main()
{
int i=0;
int temp=1, name;
char position, answer;//initializing to 0 while(i < 4) { l\[i\] = 4-i; m\[i\] = 0; r\[i\] = 0; } printf("\\nWelcome to the towers of hanoi. . . \\n\\n"); //looping until the left most tower have all the blocks in decending order. while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y') { //taking name of the block to move. printf("\\nEnter the block name:\\n"); scanf("%d", &name); find(name); //looping until correct block is moved. while(find(name) == 1){ printf("This block cannot be moved! Please chose again. . .\\n"); scanf("%d", &name); find(name); } //taking the position of the tower to move the block in. printf("\\n\\nEnter the position you want to move it in..\\n"); scanf("%1s", &position); while(temp=1) { if(toupper(position)=='M'){ if(find(name)==0 && (temp=repetetion(m, name))==0){ //if the block is movable and if the block is moved to different tower than the one it is in. del(name); //del the block from that tower m\[mp\] = name; //move that block to the prescripted one mp++; //filling the block-space of that tower, hence 4-mp more blocks can be inserted. continue; } } else if(toupper(position) == 'L'){ if(find(name)==0 && (temp =repetetion(l, name))==0){ del(name); l\[lp\] = name; lp++; continue; } } else if(toupper(position) == 'R'){ if(find(name)==0 && (temp = repetetion(r, name))==0){ del(name); r\[rp\] = name; rp++; continue; } } printf("Invalid entry! Please enter again. . ."); //if the desired position is already occupied scanf("%1s", &position); } if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1) { printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . ."); printf("\\n\\nWant to play again? (Y/N) "); scanf("%1s", &answ
-
That is not the compiler, but your program when executed after compiling. The solution to that problem is provided below by Richard. But my answer is still valid and important. And there might be more errors.
-
while(i < 4)
{
l[i] = 4-i;
m[i] = 0;
r[i] = 0;
}You never increment variable
i
so this loop will run for ever. -
I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.
/****************************************** TOWER OF HANOI **********************************************************/
#include
#include
#includeint find(int name);
int check(int temp[], int i);
void del(int name);
int repetetion(int temp[] , int name);
int indexing(int temp[]);int m[3], r[3], l[3];
int mp=0, rp=0, lp=3 ; //indicates the latest position occuipiedint main()
{
int i=0;
int temp=1, name;
char position, answer;//initializing to 0 while(i < 4) { l\[i\] = 4-i; m\[i\] = 0; r\[i\] = 0; } printf("\\nWelcome to the towers of hanoi. . . \\n\\n"); //looping until the left most tower have all the blocks in decending order. while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y') { //taking name of the block to move. printf("\\nEnter the block name:\\n"); scanf("%d", &name); find(name); //looping until correct block is moved. while(find(name) == 1){ printf("This block cannot be moved! Please chose again. . .\\n"); scanf("%d", &name); find(name); } //taking the position of the tower to move the block in. printf("\\n\\nEnter the position you want to move it in..\\n"); scanf("%1s", &position); while(temp=1) { if(toupper(position)=='M'){ if(find(name)==0 && (temp=repetetion(m, name))==0){ //if the block is movable and if the block is moved to different tower than the one it is in. del(name); //del the block from that tower m\[mp\] = name; //move that block to the prescripted one mp++; //filling the block-space of that tower, hence 4-mp more blocks can be inserted. continue; } } else if(toupper(position) == 'L'){ if(find(name)==0 && (temp =repetetion(l, name))==0){ del(name); l\[lp\] = name; lp++; continue; } } else if(toupper(position) == 'R'){ if(find(name)==0 && (temp = repetetion(r, name))==0){ del(name); r\[rp\] = name; rp++; continue; } } printf("Invalid entry! Please enter again. . ."); //if the desired position is already occupied scanf("%1s", &position); } if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1) { printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . ."); printf("\\n\\nWant to play again? (Y/N) "); scanf("%1s", &answ
Tarun Jha wrote:
...the compiler is showing logic error.
Highly unlikely. If anything, it is showing you syntax errors (e.g., endless loop).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Tarun Jha wrote:
...the compiler is showing logic error.
Highly unlikely. If anything, it is showing you syntax errors (e.g., endless loop).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles