Shift full row blocks
Select the row index, move the affected rows upward or downward together, then update the total row count after insertion or deletion.
Inserting and Deleting Elements
Presented By
Chandra Shekar Behara, Suraj & John
Concept Overview
A multi-dimensional array is an array of arrays. In memory, it exists as a contiguous block, but logically we visualize it as a grid or matrix.
The grid relies on precise index management for both rows (i) and columns (j) to maintain structural integrity.
Because memory is sequential, adding or removing elements requires shifting others to prevent gaps or overlaps.
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access: arr[row][col]
Working Lens
The best way to read this topic is to treat every insertion or deletion like a choreography problem: find the gap, shift the surrounding values, and preserve the shape of the matrix.
Core Mechanics
Row operations move larger contiguous blocks at once, while column operations revisit each row individually. That difference is the heart of the cost model.
Identify target row and move all following blocks downward in memory as a single group.
Requires traversing every row and shifting inner elements horizontally.
Overwrite target row by shifting subsequent blocks upward in memory.
Iterate all rows and pull following elements leftward to fill vertical gap.
Four-step execution path
Choose the exact row or column index that becomes the edit target.
Move surrounding values so the structure stays gap-free and ordered.
Update the logical bounds after the insertion or deletion is complete.
Prefer bulk moves and cleaner traversal patterns when performance matters.
Rows and Columns
This section now uses a cleaner stacked layout so the comparison, algorithm flow, and code stay readable without leaving large empty gaps.
Select the row index, move the affected rows upward or downward together, then update the total row count after insertion or deletion.
Select the column index, shift values within every row left or right, then update the total column count after the operation.
The same sequence supports row insertion, row deletion, column insertion, and column deletion while keeping the matrix structure valid.
Take rows, columns, and the matrix values as input.
Pick whether the edit is for a row or a column and choose the target index.
Check that the chosen row or column position is inside the allowed range.
Move rows or matrix elements in the correct direction to create or close space.
Insert the new row or column values, or remove the selected ones.
Update the row or column count, then print the updated matrix.
C Program
#include <stdio.h>
#define MAX 10
void printMatrix(int arr[MAX][MAX], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%4d", arr[i][j]);
}
printf("\\n");
}
}
void insertRow(int arr[MAX][MAX], int *rows, int cols, int pos, int newRow[MAX]) {
int i, j;
for (i = *rows; i > pos; i--) {
for (j = 0; j < cols; j++) {
arr[i][j] = arr[i - 1][j];
}
}
for (j = 0; j < cols; j++) {
arr[pos][j] = newRow[j];
}
(*rows)++;
}
void deleteRow(int arr[MAX][MAX], int *rows, int cols, int pos) {
int i, j;
for (i = pos; i < *rows - 1; i++) {
for (j = 0; j < cols; j++) {
arr[i][j] = arr[i + 1][j];
}
}
(*rows)--;
}
void insertColumn(int arr[MAX][MAX], int rows, int *cols, int pos, int newCol[MAX]) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = *cols; j > pos; j--) {
arr[i][j] = arr[i][j - 1];
}
arr[i][pos] = newCol[i];
}
(*cols)++;
}
void deleteColumn(int arr[MAX][MAX], int rows, int *cols, int pos) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = pos; j < *cols - 1; j++) {
arr[i][j] = arr[i][j + 1];
}
}
(*cols)--;
}
Hands-On Lab
Real-time simulation of shifting elements in memory.
Quick Reference
The algorithm is now placed first, centered properly, and styled as a visual flow so it appears clearly before the C program.
Choose the row index, shift the full row blocks upward or downward, then update the row count after insertion or deletion.
Choose the column index, shift values inside each row left or right, then update the column count after the operation.
This same structure works for row insertion, row deletion, column insertion, and column deletion in a two-dimensional array.
Read rows, columns, and all matrix elements from the user.
Select row insertion, row deletion, column insertion, or column deletion.
Check that the selected row or column index is within the valid range.
Move rows or columns to create space or to close the gap after deletion.
Place the new values in the correct position or remove the selected values.
Update rows or columns, then print the updated matrix as the final result.
C Program
#include <stdio.h>
#define MAX 10
void printMatrix(int arr[MAX][MAX], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%4d", arr[i][j]);
}
printf("\\n");
}
}
void insertRow(int arr[MAX][MAX], int *rows, int cols, int pos, int newRow[MAX]) {
int i, j;
for (i = *rows; i > pos; i--) {
for (j = 0; j < cols; j++) {
arr[i][j] = arr[i - 1][j];
}
}
for (j = 0; j < cols; j++) {
arr[pos][j] = newRow[j];
}
(*rows)++;
}
void deleteRow(int arr[MAX][MAX], int *rows, int cols, int pos) {
int i, j;
for (i = pos; i < *rows - 1; i++) {
for (j = 0; j < cols; j++) {
arr[i][j] = arr[i + 1][j];
}
}
(*rows)--;
}
void insertColumn(int arr[MAX][MAX], int rows, int *cols, int pos, int newCol[MAX]) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = *cols; j > pos; j--) {
arr[i][j] = arr[i][j - 1];
}
arr[i][pos] = newCol[i];
}
(*cols)++;
}
void deleteColumn(int arr[MAX][MAX], int rows, int *cols, int pos) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = pos; j < *cols - 1; j++) {
arr[i][j] = arr[i][j + 1];
}
}
(*cols)--;
}
Matrix Size
Cells Loaded
Ops Executed
Last Action
System initialized. Awaiting instructions...
Performance View
As the grid expands, insertion and deletion cost grows with the number of cells touched during every shift.
Certification Check
Test your knowledge on Multi-D array operations.
Each visitor must enter their name, class, company or college name, and program name before the assessment begins.
The page now builds a personalized randomized question set from a larger question bank instead of showing the same fixed list to everyone.