C++ Snippet

2D array input & print — tap "Copy" and paste it wherever you need.

main.cpp
#include <iostream>
using namespace std;

int main() {
    
    int n;
    cout << "Enter the size of the array: ";
    cin >> n;
    
    int arr[n][n];
    
    for (int i = 0; i < n; i++) {
        for (int j = 0;j < n; j++) {
            cout << "Enter a number for index [" << i << "][" << j << "]: ";
            cin >> arr[i][j];
        }
    }
    
    cout << endl;
    cout << endl;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0;j < n; j++) {
            if (true) {
                cout << arr[i][j] << " ";
            } else {
                cout << "  ";
            }
        }
        cout << endl;
    }
}