Help with exercise

I have this exercise from Brian Overland's book c++ without fear and I cant even start, I mean I really don't know what to do, so please give me some suggestion :)

Here is the original code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <cstdlib>
#include <iostream>

using namespace std;
void move_rings(int n, int src, int dest, int other);

int main()
{
  int n = 3;  // Stack is 3 rings high

  move_rings(n, 1, 3, 2); // Move stack 1 to stack 3
  system("PAUSE");
  return 0;
}

void move_rings(int n, int src, int dest, int other) {
  if (n == 1) {
    cout << "Move from " << src << " to " << dest
         << endl;
  } else {
    move_rings(n - 1, src, other, dest);
    cout << "Move from " << src << " to " << dest
         << endl;
    move_rings(n - 1, other, dest, src);
  }
}


and here is the exercise:

Instead of printing the “Move” message directly on the screen, have the move_ring function call yet another function, which you give the name exec_move. The exec_move function should take a source and destination stack number as its two arguments. Because this is a separate function, you can use as many lines of code as you need to print a message. You can print a more informative message:

Move the top ring from stack 1 to stack 3.
Last edited on
Why don't you start by creating the exec_move() function and have it just print the "Move from..." messages that are already in move_rings(). Once you get that working, you can modify exec_move() to print more elaborate stuff.
You need to create a new function called "exec_move". It is not necessary for this function to return a value so the return type can be 'void'. It takes two arguments, both of them are integers so the argument list must account for those. Taking those three things into consideration, we have your function definition:
void exec_move(int src, int dest);
This should be placed above any functions that might call it. Most people place forward declarations just underneath their headers. The function definition will basically be you cut and pasting Lines 22 and 23 into the body of your new function.

EDIT: By the way does this sentence, I assume it's from OP's book, piss anyone else off?:
Because this is a separate function, you can use as many lines of code as you need to print a message.

Is this implying that there is some limit imposed on how much text a stream can handle? Or that functions have some kind of capacity limit?
Last edited on
yes that sentence is a bit retarded, even for a total begginer like me, anyway thank you so much for help i solved this exercise :)
Topic archived. No new replies allowed.