(C++) How to pass 2D array to function that the size is declared by user input

4 days ago 10
ARTICLE AD BOX

So the question given to me is to make a C++ program where the user inputs the size (declared by n) of a matrix N x N, and then there's a function for the user to fill the matrix called inputMatrix

#include <iostream> using namespace std; int n; void inputMatrix(int matrix[][n]) // first error { for (int i=0 ; i<n ; i++) { for(int j=0 ; j<n ; j++) { cout << "Matrix[" << i << "][" << j << "]: "; cin >> **(matrix + i + n * j); } } } int main() { cout << "input n: "; cin >> n; int matrix[n][n]; inputMatrix(matrix[n][n]); // second error }

When I run, it gets an error "error: size of array 'matrix' is not an integral constant-expression" and "error: invalid conversion from 'int' to 'int (*)[1]' [-fpermissive]"

Read Entire Article