I'm trying to make a program which fill a quadratic matrix with some random values. So here the source code (which works fine):
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
int const n = 5;
int matrix[n][n];
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
matrix[i][j] = rand()%10; // fill matrix
cout<<setw(2)<<matrix[i][j]; // matrix output
}
cout<<"\n"; // new line
}
}
But now I want to rewrite this code using my own function:
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void fillAndPrintArray (int **matrix, int n); // function prototype
int main()
{
int const n = 5;
int matrix[n][n];
fillAndPrintArray(matrix, n);
}
void fillAndPrintArray(int **matrix, int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
matrix[i][j] = rand()%10; // fill matrix
cout<<setw(2)<<matrix[i][j];
}
cout<<"\n"; // new line
}
}
But I can't compile this code. I'm getting error:
||In function 'int main()':error: cannot convert 'int (*)[5]' to 'int**' for argument '1' to 'void fillAndPrintArray(int**, int)'.
I don't know how to fix this.