From 85fc4918931d9cd3fa401b81189a3bc1067e7cd6 Mon Sep 17 00:00:00 2001 From: CaptainDaVinci Date: Sun, 25 Feb 2018 22:00:39 +0530 Subject: [PATCH 1/2] Refactored Tower of Hanoi --- DataStructureLabPrograms/TowerOfHanoi.c | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/DataStructureLabPrograms/TowerOfHanoi.c b/DataStructureLabPrograms/TowerOfHanoi.c index 59f57b1..6e5f424 100644 --- a/DataStructureLabPrograms/TowerOfHanoi.c +++ b/DataStructureLabPrograms/TowerOfHanoi.c @@ -1,24 +1,24 @@ #define _CRT_SECURE_NO_WARNINGS #include -void TowerOfHanoi(int a, char from, char aux, char to) + +void towerOfHanoi(int a, char from, char aux, char to) { if (a == 1) { - printf("\nMove disc %d from %c to %c \n", a, from, to); + printf("Move disc %d from %c to %c \n", a, from, to); return; } - else - { - TowerOfHanoi(a - 1, from, to, aux); - printf("\nMove disc %d from %c to %c \n", a, from, to); - TowerOfHanoi(a - 1, aux, from, to); - } + + towerOfHanoi(a - 1, from, to, aux); + printf("Move disc %d from %c to %c \n", a, from, to); + towerOfHanoi(a - 1, aux, from, to); } -void main() + +int main() { int n; - printf("\nTower of Hanoi\n"); - printf("\nEnter number of discs: "); + printf("Tower of Hanoi\n"); + printf("Enter number of discs: "); scanf("%d", &n); - TowerOfHanoi(n, 'S', 'A', 'D'); -} \ No newline at end of file + towerOfHanoi(n, 'S', 'A', 'D'); +} From 4580095b8eda9e7569af90fbdbae0fc686d499f0 Mon Sep 17 00:00:00 2001 From: CaptainDaVinci Date: Sun, 25 Feb 2018 22:11:24 +0530 Subject: [PATCH 2/2] Refactored Tower of Hanoi. 1. Removed #define _CRT_SECURE_NO_WARNINGS, as it is specific to Visual studio code. 2. Changed indentation. (subjective) --- DataStructureLabPrograms/TowerOfHanoi.c | 40 ++++++++++++++----------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/DataStructureLabPrograms/TowerOfHanoi.c b/DataStructureLabPrograms/TowerOfHanoi.c index 59f57b1..af637eb 100644 --- a/DataStructureLabPrograms/TowerOfHanoi.c +++ b/DataStructureLabPrograms/TowerOfHanoi.c @@ -1,24 +1,28 @@ -#define _CRT_SECURE_NO_WARNINGS +/* + * C program to simulate the tower of hanoi. + * Takes as input the number of disks, and generates + * the sequence of steps to move the disks from 'S' to 'D'. + * + */ + #include -void TowerOfHanoi(int a, char from, char aux, char to) -{ - if (a == 1) - { - printf("\nMove disc %d from %c to %c \n", a, from, to); + +void towerOfHanoi(int a, char from, char aux, char to) { + if (a == 1) { + printf("Move disc %d from %c to %c\n", a, from, to); return; } - else - { - TowerOfHanoi(a - 1, from, to, aux); - printf("\nMove disc %d from %c to %c \n", a, from, to); - TowerOfHanoi(a - 1, aux, from, to); - } + + towerOfHanoi(a - 1, from, to, aux); + printf("Move disc %d from %c to %c\n", a, from, to); + towerOfHanoi(a - 1, aux, from, to); } -void main() -{ + +int main() { int n; - printf("\nTower of Hanoi\n"); - printf("\nEnter number of discs: "); + + printf("Tower of Hanoi\n"); + printf("Enter number of discs: "); scanf("%d", &n); - TowerOfHanoi(n, 'S', 'A', 'D'); -} \ No newline at end of file + towerOfHanoi(n, 'S', 'A', 'D'); +}