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.