<< Chapter < Page | Chapter >> Page > |
For output, a set of shipping instructions is to be generated. The instructions must contain the first four input items, the total cost of the order, and the type of billing. The total cost is obtained as the number of bicycles ordered (input item 3) times the appropriate cost per bicycle, and the type of billing is determined by the creditworthiness of the customer (input item 5). If the customer is creditworthy, a bill be sent; otherwise requires cash payment on delivery. The customer record layout is as follows.
8) The Hanoi Tower is a puzzle consisting of a number of disks placed on three columns.
The disks all have different diameters and holes in the middle so they will fit over the columns (see Figure 1). All the disks start out on column A. The object of the puzzle is to transfer all the disks from column A to column C. Only one disk can be moved at a time, and no disk can be placed on a disk that is smaller that itself.
The solution to this puzzle is easily expressed as a recursive procedure where each n disk solution is defined in terms of an n-1 disk solution. To see how this works, first consider a one-disk puzzle. Clearly, this has a simple solution, where we move the disk from column A to column C.
Now consider the n-disk problem. Moving n disks can be viewed in terms of moving only n –1 disks (hence, the recursion) as follows:
a) Move n-1 disks from column A to column B, using column C as a temporary holding area.
b) Move the last disk (the largest) from A to from C.
c) Move the n-1 disks from column B to column C, using column A as a temporary holding area.
The process ends when the last task involving n =1 disk, i.e., the base case. This is accomplished by trivially moving the disk.
Write a program to solve the Hanoi Tower puzzle. Use a recursive function with four parameters:
a) The number of disks to be moved
b) The column on which these disks are initially threaded.
c) The column to which this stack of disks is to be moved
d) The column to be used as a temporary holding area.
Your program should print the precise instructions it will take to move the disks from a starting column to the destination column. For example, to move a stack of three disks from column A to column C, your program should print the following series of moves:
9) a. Create a class named Fractions having two integer data members named for a fraction’s numerator and denominator. The class’s default constructor should provide both data members with default values of 1 if no explicit user initialization is provided. Additionally, provide member functions for displaying an object’s data values. Also provide the class with the member functions that are capable of adding, subtracting, multiplying, and dividing two Fraction objects according to the following formulas:
Sum of two fractions: a/b + c/d = (ad +cb)/bd
Difference of two fractions: a/b – c/d = (ad – cb)/bd
Product of two fractions: (a/b)X (c/d) = ac/bd
Division of two fractions: (a/b)/(c/d) = ad/cb
b. Include the class written for a) in a working C++ program that tests each of the class’s member functions.
10) A stack is an ordered collection of data items in which access is possible only at one end, called the top of the stack with the following basic operations:
1. Push: add an element to the top of the stack.
2. Pop: remove and return the top element of the stack.
3. Check if the stack is empty
4. Check if the stack is full.
It would be nice to have a stack class, because we could then use it to easily develop some applications which need stack data structure.
a. For the moment, assume that we need to define an integer stack class. Since a stack must stores a collection of integers, we can use an integer array to model the stack and a “pointer” named Top to indicate the location of the top of the stack. The array should have a fixed size. So we can begin the declaration of the class by selection two data members:
b. After defining the stack class, write a main() function which does the following tasks:
c. Next, apply the stack data structure in solving the following problem: write a program that accepts a string from the user and prints the string backward. (Hint: Use a character stack)
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?