Tower of hanoi help !

Tower of hanoi help !!

Page 1 of 1

3 Replies - 1002 Views - Last Post: 04 September 2010 - 06:01 PM Rate Topic: -----

#1 fatbrown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-September 10

Tower of hanoi help !

Posted 03 September 2010 - 06:36 PM

Hi all

i hvae this projecti need help with. We need to make a recursive tower of hanoi.

I have started on it and its going good so far. We have also been given the following code.

printf("Moving disc %d from Tower %d to Tower %d\n",
		*(int*)dataPtr,
		source == tower1 ? 1 : source == tower2 ? 2 : source == tower3 ? 3 : 0,
		dest == tower1 ? 1 : dest == tower2 ? 2 : dest == tower3 ? 3 : 0);
	pushStack(dest, dataPtr);			
printTowers();				}


i undertstand all of that except the following

source == tower1 ? 1 : source == tower2 ? 2 : source == tower3 ? 3 : 0,

dest == tower1 ? 1 : dest == tower2 ? 2 : dest == tower3 ? 3 : 0);


can someone help ??

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 04 September 2010 - 04:17 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Tower of hanoi help !

#2 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Tower of hanoi help !

Posted 03 September 2010 - 08:39 PM

Quote

Conditional operator ( ? )

The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is:
condition ? result1 : result2

If condition is true the expression will return result1, if it is not it will return result2.
7==5 ? 4 : 3     // returns 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3   // returns 4, since 7 is equal to 5+2.
5>3 ? a : b      // returns the value of a, since 5 is greater than 3.
a>b ? a : b      // returns whichever is greater, a or b. 


// conditional operator

#include <iostream>
using namespace std;

int main ()
{
  int a,b,c;

  a=2;
  b=7;
  c = (a>B)/> ? a : b;

  cout << c;

  return 0;
}


Output: 7

In this example a was 2 and b was 7, so the expression being evaluated ( a > B ) was not true, thus the first value specified after the question mark was discarded in favor of the second value (the one after the colon) which was b, with a value of 7.


From http://www.cplusplus...rial/operators/ with format modification. I suggest you visit the link to brush up on operators

Hope this helps at all

This post has been edited by Splatocaster: 03 September 2010 - 08:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 amy2389  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 23-June 10

Re: Tower of hanoi help !

Posted 03 September 2010 - 11:40 PM

conditional operators are a simpler version of if-else

the condition before the ? is checked, if it is true the statement returns the value after the ?, but if the condition is false it will return the value after :

in your case if the condition checked is false it will not return a value it will check another condition. If all three conditions checked for the value of source and dest are false then it will return 0.

source==tower1?1:source==tower2?2:source==tower3?3:0;
it will check the value of source,
if the value is equal to tower1 it will return 1
else it will check whether the value is equal to tower2
if it is it will return 2
else if the value is equal to tower3 it will return 3,
if not equal to tower3 also then it will return 0.

the value it returns will be printed at the place of second %d in the printf statement.
similar is the case with the value of dest. Its returned value will be printed at the place of third %d in the printf statement.

I hope this will help you understand the code. Best of luck.
Was This Post Helpful? 2
  • +
  • -

#4 fatbrown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-September 10

Re: Tower of hanoi help !

Posted 04 September 2010 - 06:01 PM

Thanks a lot guys that was a big help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1