M

Multi - Dimensional Arrays

Inserting and Deleting Elements

Presented By

Chandra Shekar Behara, Suraj & John

Simulator Live Fundamentals 00:00:00

Concept Overview

Multi-Dimensional Concepts

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.

Row-major thinking Index precision Shift-based updates
Rows & Columns Every position is defined by two coordinates working together.
Memory Block The structure is stored continuously, which makes shifts unavoidable.
Fast Access Direct indexing stays constant-time even when updates become expensive.

📑 Logical Structure

The grid relies on precise index management for both rows (i) and columns (j) to maintain structural integrity.

📋 Shifting Requirement

Because memory is sequential, adding or removing elements requires shifting others to prevent gaps or overlaps.

Static Declaration
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

The Logic of Insertion & Deletion

Row operations move larger contiguous blocks at once, while column operations revisit each row individually. That difference is the heart of the cost model.

Insertion Logic

Row Insertion

Identify target row and move all following blocks downward in memory as a single group.

Column Insertion

Requires traversing every row and shifting inner elements horizontally.

Deletion Logic

Row Deletion

Overwrite target row by shifting subsequent blocks upward in memory.

Column Deletion

Iterate all rows and pull following elements leftward to fill vertical gap.

Algorithm Workflow

Four-step execution path

01

Identify

Choose the exact row or column index that becomes the edit target.

02

Shift

Move surrounding values so the structure stays gap-free and ordered.

03

Resize

Update the logical bounds after the insertion or deletion is complete.

04

Optimize

Prefer bulk moves and cleaner traversal patterns when performance matters.

Rows and Columns

Well-aligned reference for operations, algorithm and C code

This section now uses a cleaner stacked layout so the comparison, algorithm flow, and code stay readable without leaving large empty gaps.

Clean layout
Row Operation

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.

Column Operation

Update each row carefully

Select the column index, shift values within every row left or right, then update the total column count after the operation.

Algorithm Flow

Step-by-step procedure for insertion and deletion

The same sequence supports row insertion, row deletion, column insertion, and column deletion while keeping the matrix structure valid.

01
Read Matrix

Take rows, columns, and the matrix values as input.

02
Choose Target

Pick whether the edit is for a row or a column and choose the target index.

03
Validate Index

Check that the chosen row or column position is inside the allowed range.

04
Shift Values

Move rows or matrix elements in the correct direction to create or close space.

05
Apply Change

Insert the new row or column values, or remove the selected ones.

06
Resize and Show

Update the row or column count, then print the updated matrix.

C Program

Insertion and deletion in a 2D array

Row and column logic
#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

Interactive Laboratory

Real-time simulation of shifting elements in memory.

Live shift visualizer

Quick Reference

Algorithm aligned with a modern page layout

The algorithm is now placed first, centered properly, and styled as a visual flow so it appears clearly before the C program.

Row Operation

Move complete rows together

Choose the row index, shift the full row blocks upward or downward, then update the row count after insertion or deletion.

Column Operation

Update every row carefully

Choose the column index, shift values inside each row left or right, then update the column count after the operation.

Algorithm On Page

Step-by-step flow for insertion and deletion

This same structure works for row insertion, row deletion, column insertion, and column deletion in a two-dimensional array.

01
Read Input

Read rows, columns, and all matrix elements from the user.

02
Choose Operation

Select row insertion, row deletion, column insertion, or column deletion.

03
Validate Position

Check that the selected row or column index is within the valid range.

04
Shift Elements

Move rows or columns to create space or to close the gap after deletion.

05
Insert or Delete

Place the new values in the correct position or remove the selected values.

06
Resize and Display

Update rows or columns, then print the updated matrix as the final result.

C Program

Insertion and deletion in 2D array

Code in C
#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

3 x 3

Cells Loaded

9

Ops Executed

0

Last Action

System ready

Event Monitor

System initialized. Awaiting instructions...

Performance View

Performance Metrics

Complexity Matrix
Access Time O(1)
Insertion (Row) O(N*M)
Deletion (Col) O(N*M)
Constant access Growing shift cost

As the grid expands, insertion and deletion cost grows with the number of cells touched during every shift.

Certification Check

Final Assessment

Test your knowledge on Multi-D array operations.

Score: 0/10

Participant details are required

Each visitor must enter their name, class, company or college name, and program name before the assessment begins.

Every participant gets a different quiz

The page now builds a personalized randomized question set from a larger question bank instead of showing the same fixed list to everyone.