"Tower of honie without recursion in c" Code Answer's

You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "Tower of honie without recursion in c" answered properly. Developers are finding an appropriate answer about Tower of honie without recursion in c related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like Tower of honie without recursion in c. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on Tower of honie without recursion in c. 

Tower of honie without recursion in c

By DragneelDragneel on Apr 26, 2021
/* tower.c

   Tower of Hanoi -- mechanical solution

   Arrange the three rods to form a triangle.

   Alternate between moving the smallest disk and making the only valid move
   which does not involve the smallest disk.

   The following rule will make sure that the tower of disks end up on the
   destination rod: if the number of disks in the puzzle is an odd number
   always move the smallest disk counter-clockwise around the triangle; if
   the number of disks in the puzzle is an even number always move the
   smallest disk clockwise around the triangle.
*/

#include <stdio.h>
#include <stdbool.h>

int destinationCount (int array[], int numberOfElements)
{
    int count = 0, i;

    for ( i = 1; i <= numberOfElements; ++i )
        if ( array[i] == 3 )
            ++count;

    return count;
}

int main (void)
{ 
    int numberOfDisks, i, smallestDir, moveCount = 0;  
    bool everyOtherMove = false;
    int rodFrom, rodTo, disk;
    int topDisk[4];
    int temp;

    printf ("\nTower of Hanoi puzzle\n");

    printf ("\nnumber of disks? ");
    scanf ("%i", &numberOfDisks);

    int rod[numberOfDisks + 1];

    // all disks start on rod 1

    for ( i = 1; i <= numberOfDisks; ++i )
        rod[i] = 1;

    // set direction to move smallest disk

    if ( (numberOfDisks & 1) == 0 )
        smallestDir = 1;
    else
        smallestDir = -1;    

    printf("\nsolution\n\n");

    do {

        ++moveCount;

        if ( ! everyOtherMove ) {

            // move smallest disk

            rodFrom = rod[1];

            rodTo = rodFrom + smallestDir;
            if ( rodTo > 3 )
                rodTo = 1;
            if ( rodTo < 1 )
                rodTo = 3;

            disk = 1;

        }
        else {

            // make only valid move not involving the smallest disk

            // find disk at the top of each rod

            for ( i = 1; i <= 3; ++i )
                topDisk[i] = numberOfDisks + 1;

            for ( i = numberOfDisks; i >= 1; --i )
                topDisk[rod[i]] = i;            

            // find which disk to move

            switch ( rod[1] )
            {
                case 1:
                    rodFrom = 2;
                    rodTo = 3;
                    break;
                case 2:
                    rodFrom = 1;
                    rodTo = 3;
                    break;
                case 3:
                    rodFrom = 1;
                    rodTo = 2;
                   break;
                default:
                    printf ("error");
                    break;
            }            

            if ( topDisk[rodFrom] > topDisk[rodTo] ) {
                // swap values
                temp = rodFrom;
                rodFrom = rodTo;
                rodTo = temp;            
            }

            disk = topDisk[rodFrom]; 

        }    

        printf ("%i: disk %i rod %c to rod %c\n", moveCount, disk, 
                 rodFrom + 64, rodTo + 64);

        // move disk

        rod[disk] = rodTo;

        // toggle everyOtherMove

        everyOtherMove = ! everyOtherMove;

    }
    while ( destinationCount (rod, numberOfDisks) != numberOfDisks );

    return 0;
}   

Source: codereview.stackexchange.com

Add Comment

0

All those coders who are working on the Whatever based application and are stuck on Tower of honie without recursion in c can get a collection of related answers to their query. Programmers need to enter their query on Tower of honie without recursion in c related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Tower of honie without recursion in c for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of Tower of honie without recursion in c while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "Tower of honie without recursion in c". Visit this developer's friendly online web community, CodeProZone, and get your queries like Tower of honie without recursion in c resolved professionally and stay updated to the latest Whatever updates. 

Whatever answers related to "Tower of honie without recursion in c"

View All Whatever queries

Whatever queries related to "Tower of honie without recursion in c"

Tower of honie without recursion in c tower of hanoi worst case time complexity how tall is the eiffel tower what is recursion bst traversal with recursion c recursion practice problems beginners recursion function r merge sort recursion java c program to print odd numbers between specified ranges recursion Write a program to check whether a grammar is left recursive or not, if it is remove left recursion inorder traversal recursion last index of a number using recursion Python Program to Count Number of Digits in a Number Using Recursion golang factorial recursion palindrome recursion explained in ruby c code to calculate power using recursion c code to calculate multiplication using recursion how to align custom icon without font awesome run chrome without cors can a computer run without a graphics card latex section without number but in table of contents flutter provider without context display modal without button click How can I group by date time column without taking time into consideration why are we receiving the payload without quotes mule how to sort the arraylist without changing the original arraylist Is multiprogramming possible without interrupts? read csv without index markdown table without header view pdf file online without downloading using codeigniter infinite scroll wordpress without plugin ascii without numpad without refresh update url in js question and answer woocommerce product page without plugin power bi button click without ctrl run app without port excel open file without running macro update the same custom field without duplicates insert into without column names how to turn off ps4 controller without console webrtc screen sharing without extension autohotkey display image without a background crud operation without entity framework in mvc fullcalendar display 12 months without dates postifx add user without create system user account how to check tables without schema jq get value without quotes get url without query string solidworks macro get pathname without filename selenium press enter without element how to enter text without using sendkeys() in selenium sort without repitition R ng serve without reload open config file without dependency injection Return a sorted array without mutating the original array JS Javascript Free Code Camp FCC print string without semicolon ng serve without reload in prod mode woocommerce create client account without email Performing a build. Maven plugin allow you to set the specific version of the artifact to be built without manually modifying the pom.xml file: test case without object repository count number of lines in csv without opening it pythonpreventing an import from executing without call can we script test case without object repository decode jwt token without library get hostname without subdomain in nginx config vim delete word without yank filmora 10 crack download without watermark MAYA to Set object scale values to 1,1,1 without changing the object size, command: how to save docker image to another machine without repo systemerror: error return without exception set Input without pressing enter python how to hack without getting caught in minecraft even number without mod can you run javascript without html running rabbitmq without Console.ReadLine(); excel edit cells without deleting content Return the string without any whitespace at the beginning or the end pihole update whitelist without gravity fix screen tearing without vsync can use jobs without ecs ? debian without gui copy paste without formatting in mac TypeError: Class constructor Home cannot be invoked without 'new' capacitor build android without android studio Write a C program to check whether the string is a palindrome without using string functions. bottomNavigationBar without label flutter how to buy stuff online without any money Swap two numbers without using a third variable ( All possible ways ). polylang integratred site show all blogs without language differnce bit operation loop without loop Write a function that encodes a string into 1337 without using strlen cursing words api without api key ometv without login

Browse Other Code Languages

CodeProZone