Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Any help with this coding would be much appreciated. I apologize if it does not seem very organized I tried my best. Thank you in

Any help with this coding would be much appreciated. I apologize if it does not seem very organized I tried my best. Thank you in advance!

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Tutorial I: Recursion in C You have learned in your lectures how to describe and code (in C) several algorithms that are recursive. We re-examine the classic Towers of Hanoi algorithm here. Specifications for executable The specs are: Towers are identified with a single integer: the left, middle and right towers are named as '1', '2' and '3' respectively. The program should print (to stdout) the moves required to solve the problem as a sequence of lines in the format: FROM ID SPACE DESTID For example, to move 3 disks from Tower 1 to Tower 2, the stdout output should be: 1 2 13 23 1 2 31 3 2 1 2 NOTHING ELSE should be written to stdout. The algorithm can be expressed in C as: #include #include "towers.h" void towers (unsigned int n, unsigned int from, unsigned int dest) { unsigned int spare 6 - from - dest; if (n != 0) { towers (n-1, from, spare); printf("%d %d ", from, dest); towers (n-1, spare, dest); } In order to see how the algorithm runs, it is useful to display additional information as the program runs. For example, more insight might be gained if the output was something like: towers (3, 1, 2) .. towers (2, 1, 3) Initial invocation Recurse (indentation indicates depth) Recurse . . . . towers (1, 1, 2) .. towers (0, 1, 3) Recurse (base case, returns immediately) .Move #1: From Tower 1 to Tower 2 First move (by towers (1, 1, 2) ..... towers (0, 3, 2) ....Move #2: From Tower 1 to Tower 3 .... towers (1, 2, 3) ...... towers (0, 2, 1) ......Move #3: From Tower 2 to Tower 3 towers (0, 1, 3) ..Move #4: From Tower 1 to Tower 2 .. towers (2, 3, 2) .... towers (1, 3, 1) .. towers (0, 3, 2) .....Move #5: From Tower 3 to Tower 1 towers (0, 2, 1) ....Move #6: From Tower 3 to Tower 2 .... towers (1, 1, 2) ......towers (0, 1, 3) ....Move #7: From Tower 1 to Tower 2 towers (0, 3, 2) The specs require a precise format for the stdout output. Thus we are not allowed to print this additional information to stdout. We opt to use stderr. The C code The C source code to do this is shown here: * Author: kelowes */ /* Description: Solves "Towers of Hanoi" problem. Prints sequence of moves to stdout. Prints other information tracing the algorithm's progress to stderr. #include #include "towers.h" static void showRecursionDepth (void); static unsigned int depth = 0; static unsigned int moveNumber = 0; void towers (unsigned int n, unsigned int from, unsigned int dest) unsigned int spare = 6 - from - dest; showRecursionDepth(); fprintf(stderr, "towers ($d, $d, ta) ", n, from, dest); depth++; if (n != 0) towers(n-1, from, spare); showRecursion Depth(); fprintf(stderr, "Move #id: From Tower &d to Tower %d ", ++moveNumber, from, dest); printf("%d %d ", from, dest); towers (n-1, spare, dest); 1 depth--; static void showRecursionDepth () int i; for (i = 0; i . For example, we can do the opposite of the previous example (seeing stdout on the screen and re-directing siderr to a file) with the command: towers >> junk2 You can also redirect both stdout and stderr at the same time in this case, each will be written to a separate file and nothing will appear on the screen. Try the following command towers 2> details > purestdout Requirement 1 Answer these questions in your README file. Question: Suppose that towers(5, 2, 3) is invoked. 1. How will the first recursive call to towers() be invoked? Answer this question in the form: towers(x, y, z) where you give the actual values to the three parameters. 2. How many recursive calls to towers() will be made before this first recursive call actually returns to the initial invocation? 3. Once towers(5, 2, 3) has invoked its first recursive call to towers() and this invocation has returned, what will be printed to stdoul? (i.e. What actual move will be made by towers(5, 2, 3) ?) 4. How will the second recursive call to towers() be invoked? Answer this question in the form: towers(x, y, z) where you give the actual values to the three parameters. Question: Suppose that towers(8, 1, 2) is invoked. How many lines will be printed to stdout? Note You should note (or try to convince yourself) that the number of lines printed to stdout is precisely equal to the number of moves required to solve the problem. You can use the theoretical analysis of the problem to determine the number of moves. The towers command behaves somewhat differently than what has been described so far. In particular, if it is invoked with an argument, it will move the specified number of disks from Tower 1 to Tower 2. For example, towers 10 will output the actions to move 10 disks. Of course, you can redirect the moves to a file and then count the number of lines in the file, allowing you to use the software to verify your theoretical answer. Instead of counting the lines manually, you can use the Unix command wc-1 some File to do the work for you. Tutorial II: The main() function Specifications for the towers command NAME towerstowers of Hanoi solver SYNOPSIS towers number Disks from Tower destTower DESCRIPTION Solves the Towers of Hanoi problem and prints the required moves to stdout. One line of text in the format From TowerID ToTowerID is written for each move. The TowerIDs are 'l' (for left tower), 2 (for middle tower) or "3" (for right Tower). The program behaves as follows depending on the arguments given on the command line: No arguments If no arguments are given, the problem is solved for moving 3 disks from Tower 1 to Tower 2. One argument (numberDisks) If only one argument is given, the problem is solved for moving number Disks disks from Tower 1 to Tower 2. Three arguments (number Disks fromID toID) If all three arguments are given, the problem is solved for moving number Disks disks from Tower fromID to Tower told. The tower IDs must be either '1', '2' or '3' and the two IDs must be different. EXIT CODE If the command is invoked correctly, the moves are output and the exit status is 0 (zero). Otherwise, an incorrect invocation produces no output. A message is displayed on stderr and the exit status is non-zero. BUGS The program does not yet behave as specified. The command line args to identify the two towers are not recognized. Furthermore, incorrect invocation is not detected. Initial version of main() You are provided with an initial version of the main() function in the file towers Main.c and the listing is shown below. However, the bugs identified in the previous section can be fixed in the main() function. #include #include "towers.h" int main(int arge, char **argv) { int n = 3; int from = 1; int dest = 2; 2 if (argc > 1) { n = atoi (argv[1]); 1 towers (n, from, dest); exit(0); 1 Requirement 2 You have to modify the main() function so that the bugs identified are fixed. The file containing the main function must still be calledtowersMain.c; i.e. you edit the existing file to fix the bugs. Lab Submission README file Your README file should contain: 1. A brief description of what you did and did not) achieve in the Lab. 2. Your answers to the Questions in Requirement 1. Tutorial I: Recursion in C You have learned in your lectures how to describe and code (in C) several algorithms that are recursive. We re-examine the classic Towers of Hanoi algorithm here. Specifications for executable The specs are: Towers are identified with a single integer: the left, middle and right towers are named as '1', '2' and '3' respectively. The program should print (to stdout) the moves required to solve the problem as a sequence of lines in the format: FROM ID SPACE DESTID For example, to move 3 disks from Tower 1 to Tower 2, the stdout output should be: 1 2 13 23 1 2 31 3 2 1 2 NOTHING ELSE should be written to stdout. The algorithm can be expressed in C as: #include #include "towers.h" void towers (unsigned int n, unsigned int from, unsigned int dest) { unsigned int spare 6 - from - dest; if (n != 0) { towers (n-1, from, spare); printf("%d %d ", from, dest); towers (n-1, spare, dest); } In order to see how the algorithm runs, it is useful to display additional information as the program runs. For example, more insight might be gained if the output was something like: towers (3, 1, 2) .. towers (2, 1, 3) Initial invocation Recurse (indentation indicates depth) Recurse . . . . towers (1, 1, 2) .. towers (0, 1, 3) Recurse (base case, returns immediately) .Move #1: From Tower 1 to Tower 2 First move (by towers (1, 1, 2) ..... towers (0, 3, 2) ....Move #2: From Tower 1 to Tower 3 .... towers (1, 2, 3) ...... towers (0, 2, 1) ......Move #3: From Tower 2 to Tower 3 towers (0, 1, 3) ..Move #4: From Tower 1 to Tower 2 .. towers (2, 3, 2) .... towers (1, 3, 1) .. towers (0, 3, 2) .....Move #5: From Tower 3 to Tower 1 towers (0, 2, 1) ....Move #6: From Tower 3 to Tower 2 .... towers (1, 1, 2) ......towers (0, 1, 3) ....Move #7: From Tower 1 to Tower 2 towers (0, 3, 2) The specs require a precise format for the stdout output. Thus we are not allowed to print this additional information to stdout. We opt to use stderr. The C code The C source code to do this is shown here: * Author: kelowes */ /* Description: Solves "Towers of Hanoi" problem. Prints sequence of moves to stdout. Prints other information tracing the algorithm's progress to stderr. #include #include "towers.h" static void showRecursionDepth (void); static unsigned int depth = 0; static unsigned int moveNumber = 0; void towers (unsigned int n, unsigned int from, unsigned int dest) unsigned int spare = 6 - from - dest; showRecursionDepth(); fprintf(stderr, "towers ($d, $d, ta) ", n, from, dest); depth++; if (n != 0) towers(n-1, from, spare); showRecursion Depth(); fprintf(stderr, "Move #id: From Tower &d to Tower %d ", ++moveNumber, from, dest); printf("%d %d ", from, dest); towers (n-1, spare, dest); 1 depth--; static void showRecursionDepth () int i; for (i = 0; i . For example, we can do the opposite of the previous example (seeing stdout on the screen and re-directing siderr to a file) with the command: towers >> junk2 You can also redirect both stdout and stderr at the same time in this case, each will be written to a separate file and nothing will appear on the screen. Try the following command towers 2> details > purestdout Requirement 1 Answer these questions in your README file. Question: Suppose that towers(5, 2, 3) is invoked. 1. How will the first recursive call to towers() be invoked? Answer this question in the form: towers(x, y, z) where you give the actual values to the three parameters. 2. How many recursive calls to towers() will be made before this first recursive call actually returns to the initial invocation? 3. Once towers(5, 2, 3) has invoked its first recursive call to towers() and this invocation has returned, what will be printed to stdoul? (i.e. What actual move will be made by towers(5, 2, 3) ?) 4. How will the second recursive call to towers() be invoked? Answer this question in the form: towers(x, y, z) where you give the actual values to the three parameters. Question: Suppose that towers(8, 1, 2) is invoked. How many lines will be printed to stdout? Note You should note (or try to convince yourself) that the number of lines printed to stdout is precisely equal to the number of moves required to solve the problem. You can use the theoretical analysis of the problem to determine the number of moves. The towers command behaves somewhat differently than what has been described so far. In particular, if it is invoked with an argument, it will move the specified number of disks from Tower 1 to Tower 2. For example, towers 10 will output the actions to move 10 disks. Of course, you can redirect the moves to a file and then count the number of lines in the file, allowing you to use the software to verify your theoretical answer. Instead of counting the lines manually, you can use the Unix command wc-1 some File to do the work for you. Tutorial II: The main() function Specifications for the towers command NAME towerstowers of Hanoi solver SYNOPSIS towers number Disks from Tower destTower DESCRIPTION Solves the Towers of Hanoi problem and prints the required moves to stdout. One line of text in the format From TowerID ToTowerID is written for each move. The TowerIDs are 'l' (for left tower), 2 (for middle tower) or "3" (for right Tower). The program behaves as follows depending on the arguments given on the command line: No arguments If no arguments are given, the problem is solved for moving 3 disks from Tower 1 to Tower 2. One argument (numberDisks) If only one argument is given, the problem is solved for moving number Disks disks from Tower 1 to Tower 2. Three arguments (number Disks fromID toID) If all three arguments are given, the problem is solved for moving number Disks disks from Tower fromID to Tower told. The tower IDs must be either '1', '2' or '3' and the two IDs must be different. EXIT CODE If the command is invoked correctly, the moves are output and the exit status is 0 (zero). Otherwise, an incorrect invocation produces no output. A message is displayed on stderr and the exit status is non-zero. BUGS The program does not yet behave as specified. The command line args to identify the two towers are not recognized. Furthermore, incorrect invocation is not detected. Initial version of main() You are provided with an initial version of the main() function in the file towers Main.c and the listing is shown below. However, the bugs identified in the previous section can be fixed in the main() function. #include #include "towers.h" int main(int arge, char **argv) { int n = 3; int from = 1; int dest = 2; 2 if (argc > 1) { n = atoi (argv[1]); 1 towers (n, from, dest); exit(0); 1 Requirement 2 You have to modify the main() function so that the bugs identified are fixed. The file containing the main function must still be calledtowersMain.c; i.e. you edit the existing file to fix the bugs. Lab Submission README file Your README file should contain: 1. A brief description of what you did and did not) achieve in the Lab. 2. Your answers to the Questions in Requirement 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

Describe briefly the A-B-C classification.

Answered: 1 week ago

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago