1

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.

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
  • `SIZE` is declared in `main()`. Put it above the declaration of your function and see what happens. Also, could you [edit your question](https://stackoverflow.com/posts/58584057/edit) and copy the verbatim error message here? –  Oct 27 '19 at 23:02
  • 1
    @Chipster `SIZE` in the function parameters will refer to the `SIZE` declared in the respective first parameter. (No matter where `const int SIZE = 10;` is placed.) It seems that OP is trying to use variable length arrays, which are not standard C++. – walnut Oct 27 '19 at 23:13
  • @uneven_mark Oh. Right. Yeah, that's not a thing in C++. –  Oct 27 '19 at 23:14
  • @LoriKeeland Please edit your question and include the error message you are receiving verbatim. Also please clarify whether you expect the arrays that you are passing to be fixed-size or dynamic size. – walnut Oct 27 '19 at 23:15
  • [Here](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) are a bunch of things you can try. –  Oct 27 '19 at 23:15
  • 2
    Have a look at `std::array` and `std::vector`. The first one is for fixed size arrays, the second one for dynamic arrays. Use that instead of the old-school raw arrays and you won't run into issues like this. – super Oct 27 '19 at 23:18

2 Answers2

2

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.

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
  • Personally, I use C-style arrays more than STL containers because they are better for performance. However, that is also a matter of taste. For general programming, where performance is not critical, STL containers (such as [std::vector](https://en.cppreference.com/w/cpp/container/vector)) will likely be the better choice, because they are easier to use. – Andreas Wenzel Oct 28 '19 at 12:40
1

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.

Andreas Wenzel
  • 4,984
  • 2
  • 7
  • 24