Remove a specified row from a 2D array in C++

In this C++ snippet, I take user inputs for the number of rows and columns of a 2D array, fill the array, and then remove a specified row based on the user's input. After that, I display the modified array without the deleted row. This example illustrates basic array manipulation in C++.
 avatar
unknown
c_cpp
5 months ago
451 B
3
Indexable
#include <iostream>
using namespace std;

int main() {
    int m, n; cin >> m >> n;
    int arr[m][n];
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            cin >> arr[i][j];
        }   
    }
    int del; cin >> del; del--;
    
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            if(i != del) cout << arr[i][j] << " ";
        }
        if(i != del) cout << endl;
    }
    
    return 0;
}
Editor is loading...
Leave a Comment