Lori Keeland
Lori Keeland

Reputation: 11

I am struggling with 2D array parameters

I have to make user defined functions to randomize the values of a 2d matrix and have another user defined function print it out. I am having problems with it in the parameters and everywhere else is giving me confusing/opposing answers.

I've tried

-int antcolony[SIZE][SIZE]
-int antcolony[][SIZE]
-int antcolony[][]
-int antcolony[SIZE,SIZE]

and others I can't remember. Please help

#include <iostream>
#include <cstdlib>
#include <ctime>
 using namespace std;

/*  Function: colonizePhermones
 * Parameters: int Size, const int antColony[][SIZE]
 * Return: n/a
 * Description: randomize the values in antColony
 */
void colonizePhermones(int SIZE, int antColony[SIZE][SIZE]);

/*  Function: printPhermones
 * Parameters: const int antColony[][SIZE]
 * Return: n/a
 * Description: print the values in antColony
*/
void printPhermones(int SIZE, int antColony[SIZE][SIZE]);


int main() {
    //declaring variables
    const int SIZE = 10;
    int colonyPath[SIZE];
    int antColony[SIZE][SIZE];
    int pathCounter = 0;
    colonyPath[pathCounter]= 0;

    //test caling functions
    colonizePhermones(SIZE, antColony[SIZE][SIZE]);
    printPhermones(SIZE, antColony[SIZE][SIZE]);

    return 0;
}

void colonizePhermones(int SIZE, int antColony[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if ( i == j) {
                antColony[i][j] = 0;
            }
            else {
                antColony[i][j] = ((rand() % 19) +1);
            }
        }
    }
}

void printPhermones(int SIZE, int antColony[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            cout << antColony[i][j] << " ";
        }
        cout << endl;
    }
}

It doesn't do anything but give me error messages. I need it to print the array.

Upvotes: 1

Views: 89

Answers (2)

Andreas Wenzel
Andreas Wenzel

Reputation: 24846

The following line is not valid:

void colonizePhermones(int SIZE, int antColony[SIZE][SIZE]);

Generally, it is possible for C-style arrays to be multi-dimensional, by having an array of an array. This means that a pointer to an element of the outer array will be a pointer to an array.

However, in order to declare a pointer, the exact type of the pointer must be known, i.e. the type of the element the pointer is pointing to. In order to declare a pointer to an array data type, you must therefore also specify the size of the array the pointer is pointing to. This size must be a constant value that is known at compile time. However, in the case of your function declaration, it is not constant, because you specified the size of the array to be the value of the first function parameter ("SIZE"). Function parameters are always variable and never compile-time constants, because the compiler must assume that a function may be called with different parameters (even if that never happens in your program).

So, in order to fix your program, I suggest you change the line quoted above to the following:

void colonizePhermones(int array_size, int antColony[SIZE][SIZE]);

That way, the size of the array in the second parameter no longer references the first parameter. But now the variable SIZE is undeclared. Therefore, I suggest you now move the line

const int SIZE = 10;

from the function main to global scope, making it a global variable. After that, the size of the array will now be a compile-time constant and you will no longer get an error message.

Now the function colonizePheromones is fixed. To fix the function printPhermones, you must do the same thing.

However, there is one other bug in your program. The following lines are wrong:

colonizePhermones(SIZE, antColony[SIZE][SIZE]);
printPhermones(SIZE, antColony[SIZE][SIZE]);

As the second parameter in both functions, you must pass the array decayed to a pointer to the first element of the outer array. Instead, you are passing the value of a single element of the inner array. This element does not even exist, because you are accessing the multi-dimensional array out of bounds. If SIZE is 10, then you will be accessing the array the following way:

antColony[10][10]

However, valid indexes are 0-9, because the array has a size of 10 in both dimensions.

The two lines that I quoted above should therefore look like this:

colonizePhermones(SIZE, antColony);
printPhermones(SIZE, antColony);

The whole program will then look like this and compile without errors:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 10;

/*  Function: colonizePhermones
* Parameters: int Size, const int antColony[][SIZE]
* Return: n/a
* Description: randomize the values in antColony
*/
void colonizePhermones( int array_size, int antColony[SIZE][SIZE] );

/*  Function: printPhermones
* Parameters: const int antColony[][SIZE]
* Return: n/a
* Description: print the values in antColony
*/
void printPhermones( int array_size, int antColony[SIZE][SIZE] );



int main()
{
    //declaring variables

    int colonyPath[SIZE];
    int antColony[SIZE][SIZE];
    int pathCounter = 0;
    colonyPath[pathCounter] = 0;

    //test caling functions
    colonizePhermones( SIZE, antColony );
    printPhermones( SIZE, antColony );

    return 0;
}

void colonizePhermones( int array_size, int antColony[SIZE][SIZE] ) {
    for ( int i = 0; i < SIZE; i++ ) {
        for ( int j = 0; j < SIZE; j++ ) {
            if ( i == j )
            {
                antColony[i][j] = 0;
            }
            else
            {
                antColony[i][j] = ((rand() % 19) + 1);
            }
        }
    }
}

void printPhermones( int array_size, int antColony[SIZE][SIZE] ) {
    for ( int i = 0; i < SIZE; i++ ) {
        for ( int j = 0; j < SIZE; j++ )
        {
            cout << antColony[i][j] << " ";
        }
        cout << endl;
    }
}

An alternative to using these C-style arrays is to use std::vector instead, which has the advantage that the length does not have to be a compile-time constant, but can be changed dynamically at run-time.

Upvotes: 1

A M
A M

Reputation: 15267

Additional info:

If you want to pass a plain old C-array to a function, you have 2 possibilities.

  1. Pass by reference
  2. Pass by pointer

It seems that you want to pass by reference. But you are using the wrong syntax.

Please see:

void function1(int(&m)[3][4])   // For passing array by reference
{}
void function2(int(*m)[3][4])   // For passing array by pointer
{}

int main()
{
    int matrix[3][4]; // Define 2 dimensional array

    function1(matrix);  // Call by reference
    function2(&matrix); // Call via pointer 
    return 0;
}

What you pass to the function is a decayed pointer to array of int.

Simply correct the syntax and it will work.

Additional hint:

Do not use plain C-style arrays in C++. Never. Please use STL containers.

Upvotes: 2

Related Questions