I want to ask the user for the size of a 2D array arr[][], but also pass it through the function initializeArray. However, if I pass it through the function, I would have to have a size declarator for col, which doesn't allow the user to enter their own value for the size
#include<iostream>
using namespace std;
void initializeArray(arr[][10], int N);
int main() {
int N;
cout << "enter an array size: ";
cin >> N;
int arr[N][N];
initializeArray(arr, N); // I get an error here
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
void initializeArray(int arr[][10], int N) {
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
arr[i][j] = 0;
}
The only solution I found was the make arr[][] a global array, but in that case, I would have to still declare the size parameters, and I want the user to enter whatever they want. Is there another way to fix this?