Click here to Skip to main content
15,887,442 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: convert feet and inches to centimeters Pin
David Crow19-Jan-18 3:16
David Crow19-Jan-18 3:16 
QuestionStart app without taskbar icon Pin
_Flaviu17-Jan-18 1:56
_Flaviu17-Jan-18 1:56 
AnswerRe: Start app without taskbar icon Pin
Richard MacCutchan17-Jan-18 2:11
mveRichard MacCutchan17-Jan-18 2:11 
AnswerRe: Start app without taskbar icon Pin
Jochen Arndt17-Jan-18 2:20
professionalJochen Arndt17-Jan-18 2:20 
GeneralRe: Start app without taskbar icon Pin
_Flaviu18-Jan-18 1:09
_Flaviu18-Jan-18 1:09 
GeneralRe: Start app without taskbar icon Pin
leon de boer18-Jan-18 1:40
leon de boer18-Jan-18 1:40 
GeneralRe: Start app without taskbar icon Pin
Jochen Arndt18-Jan-18 2:07
professionalJochen Arndt18-Jan-18 2:07 
QuestionTo make an interacting user game on *Tower of hanoi* . Pin
Tarun Jha14-Jan-18 23:58
Tarun Jha14-Jan-18 23:58 
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.

C++
/******************************************	TOWER OF HANOI	**********************************************************/
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int 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 occuipied

int 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", &answer);
		}
	}

	printf("\t\tTHANK YOU !");
	return 0;
}

//it's function is to fins the block position and if it is movable.
int find(int name)
{
	int i, temp;

	for(i=0; i<4; i++)
	{
		if(m[i]==name){
			temp = check(m, i);		//checks if it is movable
		}
		else if(l[i]==name){
			temp = check(l, i);
		}
		else if(r[i]==name){
			temp = check(r, i);
		}
	}

	return (temp);
}

//checks if the block is obstructed or not.
int check(int temp[], int i)
{
	if(i != indexing(temp))
		return(1) ;
	else if(i == indexing(temp))
		return(0) ;
}

//finds the size of the array
int indexing(int temp[])
{
	int count=0;
	while(temp[count]!= 0)
	{
		count++;
	}
	return(count) ;
}

//finds if the element to be moved is already at the required posotion
int repetetion(int temp[] , int name)
{
	int i, flag;
	for(i=0; i<4; i++)
	{
		if(temp[i] == name)
		{
			flag = 1;
			//printf("It's already there ")
			break;
		}
		else
		{
			flag = 0;
		}
	}

	return(flag);
}

//del the name from it's initial position.
void del(int name)
{
	int i;
	for(i=0; i<4; i++)
	{
		if(m[i]==name){
			m[i] = 0;
			mp--;
		}
		else if(l[i]==name){
			l[i] = 0;
			lp--;
		}
		else if(r[i]==name){
			r[i] = 0;
			rp--;
		}
	}
	return ;
}



Thank you
AnswerRe: To make an interacting user game on *Tower of hanoi* . Pin
Jochen Arndt15-Jan-18 0:33
professionalJochen Arndt15-Jan-18 0:33 
GeneralRe: To make an interacting user game on *Tower of hanoi* . Pin
Tarun Jha15-Jan-18 1:01
Tarun Jha15-Jan-18 1:01 
GeneralRe: To make an interacting user game on *Tower of hanoi* . Pin
Jochen Arndt15-Jan-18 1:11
professionalJochen Arndt15-Jan-18 1:11 
AnswerRe: To make an interacting user game on *Tower of hanoi* . Pin
Richard MacCutchan15-Jan-18 1:06
mveRichard MacCutchan15-Jan-18 1:06 
GeneralRe: To make an interacting user game on *Tower of hanoi* . Pin
Tarun Jha15-Jan-18 1:21
Tarun Jha15-Jan-18 1:21 
GeneralRe: To make an interacting user game on *Tower of hanoi* . Pin
David Crow15-Jan-18 2:42
David Crow15-Jan-18 2:42 
GeneralRe: To make an interacting user game on *Tower of hanoi* . Pin
Richard MacCutchan15-Jan-18 5:00
mveRichard MacCutchan15-Jan-18 5:00 
QuestionSort digits of number Pin
Anonygeeker14-Jan-18 22:41
Anonygeeker14-Jan-18 22:41 
AnswerRe: Sort digits of number Pin
CPallini14-Jan-18 23:00
mveCPallini14-Jan-18 23:00 
AnswerRe: Sort digits of number Pin
Richard MacCutchan14-Jan-18 23:06
mveRichard MacCutchan14-Jan-18 23:06 
Questionconversion error Pin
Anonygeeker14-Jan-18 20:28
Anonygeeker14-Jan-18 20:28 
AnswerRe: conversion error Pin
Jochen Arndt14-Jan-18 21:19
professionalJochen Arndt14-Jan-18 21:19 
AnswerRe: conversion error Pin
Richard MacCutchan14-Jan-18 21:36
mveRichard MacCutchan14-Jan-18 21:36 
QuestionI couldn't think a good title for this.Simple question Pin
Emrah Duatepe14-Jan-18 7:37
Emrah Duatepe14-Jan-18 7:37 
AnswerRe: I couldn't think a good title for this.Simple question Pin
Jochen Arndt14-Jan-18 21:37
professionalJochen Arndt14-Jan-18 21:37 
GeneralRe: I couldn't think a good title for this.Simple question Pin
Emrah Duatepe14-Jan-18 21:44
Emrah Duatepe14-Jan-18 21:44 
GeneralRe: I couldn't think a good title for this.Simple question Pin
Jochen Arndt14-Jan-18 22:18
professionalJochen Arndt14-Jan-18 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.