<< Chapter < Page Chapter >> Page >
  1. Customer’s name
  2. Customer’s address
  3. Number of bicycles ordered
  4. Type of bicycle (mountain or street)
  5. Creditworthiness of the customer (good or bad)

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.

Layout of result

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:

Hanoi-Tower puzzle

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:

  • Provide an integer array data member to hold the stack elements (the size of the array is a constant).
  • Provide an integer data member to indicate the top of the stack.As for the member functions of the stack class, we have to define 5 member functions: the constructor which creates an empty stack and four other member functions (push, pop, check if the stack is empty, check if the stack is full).

b. After defining the stack class, write a main() function which does the following tasks:

  • Creating one stack object.
  • Pushing into the stack object ten integer elements which take values from 1 to 10 with the increment of 1.
  • Popping one by one element from the stack object and displaying it out, repeating this action until the stack becomes empty.

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)

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask