Lab 7
Lab 7
Nov 9, 2022
Exercise 1
Click to edit Master title style
• Write a function show_digit(int x) that prints each digit of an
•integer
Clickusing recursion.
to edit Master text styles
• Second level
• Third level
• Fourth level
• Fifth level
2
Exercise 1
Click to edit
• Program output
Master title style
• Click to edit Master text styles
• Second level
• Third level
• Fourth level
• Fifth level
3
Exercise 2
Click to edit Master title style
• (Towers of Hanoi) Every budding computer scientist must grapple
with certain classic problems, and the Towers of Hanoi is one of the
•most
Click to edit
famous Master
of these. texthasstyles
Legend it that in a temple in the Far East,
• Second
priests are attempting
level to move a stack of disks from one peg to
another.• The
Thirdinitial
level stack had 64 disks threaded onto one peg and
arranged from bottom to top by decreasing size. The priests are
• Fourth level
attempting to move the stack from this peg to a second peg under the
• Fifth level
constraints that exactly one disk is moved at a time, and at no time
may a larger disk be placed above a smaller disk. A third peg is
available for temporarily holding the disks. Supposedly the world
will end when the priests complete their task, so there is little
incentive for us to facilitate their efforts.
4
Exercise 2
Click to edit Master title style
• Let us assume that the priests are attempting to move the disks from
peg 1 to peg 3. We wish to develop an algorithm that will print the
•precise
Clicksequence
to editofMaster textpeg
disk-to-disk styles
transfers.
• Second
• If we level
were to approach this problem with conventional methods, we
• Thirdfind
would rapidly levelourselves hopelessly knotted up in managing the
• Fourth
disks. Instead, if welevel
attack the problem with recursion in mind, it
• Fifth level
immediately becomes tractable. Moving n disks can be viewed in
terms of moving only n – 1 disks (and the recursion) as follows:
a) Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.
b) Move the last disk (the largest) from peg 1 to peg 3.
c) Move the n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding
area.
5
Exercise 2
Click to edit Master title style
• The process ends when the last task involves moving n = 1 disk, i.e., the base case.
This is accomplished by trivially moving the disk without the need for a temporary
•holding
Click area.
to edit Master text styles
• Write a program to solve the Towers of Hanoi problem. Use a recursive function
with •four
Second level
parameters:
• Thirdoflevel
a) The number disks to be moved
b) The peg on• which
Fourththese
level disks are initially threaded
c) The peg to which• Fifth level of disks is to be moved
this stack
d) The peg to be used as a temporary holding area
• Your program should print the precise instructions it will take to move the disks from
the starting peg to the destination peg. For example, to move a stack of three disks
from peg 1 to peg 3, your program should print the following series of moves:
1 → 3 (This means move one disk from peg 1 to peg 3.)
1→2
3→2
1→3
2→1
2→3
1→3
6
Exercise 2
Click to edit
• Program output
Master title style
• Click to edit Master text styles
• Second level
• Third level
• Fourth level
• Fifth level
7
Submission
Click to edit Master
• Submit to CSE server
title style
Lab # Class #
• Click to edit Master text styles
At the end of the Lab7, submit your C sources file by typing
• Second level
~gs1401/bin/submit Lab7_1 ex7_1.c ex7_2.c // class 1, Deadline: Friday by 10:50 am
• Third level
~gs1401/bin/submit Lab7_2 ex7_1.c ex7_2.c // class 2, Deadline: Friday by 11:50 am
• Fourth
~gs1401/bin/submit Lab7_3 ex7_1.c ex7_2.c // class 3, Deadline: Friday by 1:50 pm
level
• Fifthex7_1.c
~gs1401/bin/submit Lab7_4 level ex7_2.c // class 4, Deadline: Friday by 2:50 pm
~gs1401/bin/submit Lab7_5 ex7_1.c ex7_2.c // class 5, Deadline: Friday by 3:50 pm
~gs1401/bin/submit Lab7_6 ex7_1.c ex7_2.c // class 6, Deadline: Friday by 4:50 pm
You may check that you have submitted your source code correctly by typing
~gs1401/bin/submit -check