HELP TOPIC: Known multi-dimensional array used in function

Hello, I'm fairly new to the c++ programming language, and I have come across a slight problem while working on a question for my programming class. I'm going a bit beyond what the question is asking for, but that is only for my own benefit. This is not asking anyone to do my homework, as any answer I may obtain from this inquiry will not effect the simpler program being given to my instructor.

Now that that's out of the way, the problem is this: we need to construct a program to solve a 4 ring tower of Hanoi. I decided that I want to have it so the user can choose the number of rings instead. I started by making an array with an indeterminate amount of rows, and 3 columns. I then proceed to fill the array in with numbers corresponding to the size of the ring at each point, plus one row on top for aesthetics, using a function. That's fine and dandy. The problem arises when I try to take that array, and put it into a function to display what the 'tower' looks like.

Everything else seems to work, except anything to do with the array going into the function.

I have searched many forum posts and tutorials, but they all seem to skirt around my particular problem.

Any help would be appreciated, I can post what I have written of the program here if you would like to see how I've been going about it so far.

Oh, and I'm using DevC++ as my compiler, if that makes a difference at all.
Last edited on

Any help would be appreciated, I can post what I have written of the program here if you would like to see how I've been going about it so far.

Yes, that would be VERY helpful to anyone that wishes to lend a 'programming' hand.
Don't bother. The Towers of Hanoi is a basic beginning programmer's problem that the beginner needs to think about himself, instead of plagarizing another's work. Don't give your efforts to others.

As for the array thing, you can pass one in if you know the specific dimensions, by passing the size of the major dimension as argument:

1
2
3
4
5
6
7
void foo( int towers[][ 3 ], int rings )
  {
  for (int ring = 0; ring < rings; ring++)
    {
    ... towers[ ring ][ 0 ] ...
    }
  }

Hope this helps.
I think I see what you're getting at here...what I had written in as of posting (only the specific parts that i'm trying to fix, mind you)

int display ( int posts[][3], int z );

The part just before the main function (with z being the number of rings; and does it matter whether I use 'int' or 'void'? I've never been told the difference)

but then when I try to call it in the main section:

display ( posts[z][3] );

It keeps giving me the error: "invalid conversion from `int' to `int (*)[3]' " and as of yet, this looks like complete gobbledygook. Is the '*' supposed to mean it's a pointer? That, and I think I may be inputting the variables incorrectly.

Also, clarifying point, I realize later that I'm only going to need this while I'm testing the program, as you won't be able to keep up with the changes at the speed that the computer makes the calculations and displays each move. As such it will only be used for troubleshooting purposes.


 | | |
1 | |
2 | |
3 | |
4 | |
. . .
. . .
z | |
===

^kinda what I'm trying to get at here.
Last edited on
Here are some examples of passing arrays with different major dimensions:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

void parray( int a[][ 3 ], int rows )
  {
  for (int row = 0; row < rows; row++)
    {
    for (int col = 0; col < 3; col++)
      cout << a[ row ][ col ] << "\t";
    cout << "\n";
    }
  }

int main()
  {
  // These first two examples are fixed arrays,
  // whose dimensions are all known at compile-time.
  int one[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
  int two[ 3 ][ 3 ] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };

  cout << "one\n";  parray( one, 2 );
  cout << "two\n";  parray( two, 3 );

  // This last example is for a variable-sized (dynamic)
  // array whose major dimension may be supplied by the user
  // (or simply specified programmatically as we do here).
  int var_size = 5;

  typedef int int3[ 3 ];
  int3* three = new int3[ var_size ];
  for (int n = 0; n < var_size; n++)
  for (int m = 0; m < 3; m++)
    three[ n ][ m ] = (n == m);

  cout << "three\n";  parray( three, var_size );

  delete[] three;

  return 0;
  }

Hope this helps.
Ok, just to let people know, I figured out what I was doing wrong.

When inputting an array into the arguments of a function, don't include the dimensions, just the name.

So, what I should have had there is:

display ( posts, z );

(with z being the number of rings) That way, it will take the memory location of the array, and pull it from there. Or at least that's how it was explained to me.

Thanks to everybody for your help.
Topic archived. No new replies allowed.