0% found this document useful (0 votes)
30 views7 pages

Understanding Arrays in Data Structures

An array is a collection of elements of the same data type stored in contiguous memory locations, accessed via an index. The document covers standard operations on arrays, including traversal, insertion, deletion, searching, and updating, with examples for both one-dimensional and two-dimensional arrays. It also discusses memory storage orders (row-major and column-major), implementation of array operations, and applications of arrays in various fields.

Uploaded by

Arun Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Understanding Arrays in Data Structures

An array is a collection of elements of the same data type stored in contiguous memory locations, accessed via an index. The document covers standard operations on arrays, including traversal, insertion, deletion, searching, and updating, with examples for both one-dimensional and two-dimensional arrays. It also discusses memory storage orders (row-major and column-major), implementation of array operations, and applications of arrays in various fields.

Uploaded by

Arun Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structure - Arrays

1. Definition An array is a collection of elements of the same data type, stored in


contiguous memory locations.

Each element is accessed using an index (subscript).


• Syntax: data_type array_name[size];
• Example: int marks[5]; // array of 5 integers

2. Standard Operations on Arrays


The most common operations performed on arrays include:
1. Traversal – Accessing each element of the array.
2. Insertion – Adding a new element at a given position.
3. Deletion – Removing an element from a given position.
4. Searching – Finding the location of an element (Linear or Binary search).
5. Updation – Modifying the value of an element.

3. One-Dimensional Array (1D Array) is a linear list of elements.


Exsample Program:
#include <stdio.h>
#define SIZE 5

int main() {
int arr[SIZE] = {10, 20, 30, 40, 50};
int i, search = 30, found = 0;

// Traversal
printf("Array elements:\n");
for(i = 0; i < SIZE; i++) { printf("%d ", arr[i]);}

// Searching
for(i = 0; i < SIZE; i++) {
if(arr[i] == search) {
printf("\nElement %d found at index %d\n", search, i);
found = 1;
break;
}
}
if(!found)
printf("\nElement not found\n");

// Updating
arr[2] = 99;
printf("\nArray after updation:\n");
for(i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}

return 0;
}
4. Two-Dimensional Array (2D Array) is like a table with rows and columns.
It is declared as: data_type array_name[rows][cols];
Example Program: Matrix Addition
#include <stdio.h>
#define ROW 2
#define COL 3

int main() {
int A[ROW][COL] = {{1,2,3},{4,5,6}};
int B[ROW][COL] = {{6,5,4},{3,2,1}};
int C[ROW][COL];
int i, j;

// Matrix Addition
for(i = 0; i < ROW; i++) {
for(j = 0; j < COL; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}

printf("Resultant Matrix:\n");
for(i = 0; i < ROW; i++) {
for(j = 0; j < COL; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}

return 0;
}

• Row-Major vs Column-Major Order


Data storage in 2D arrays in memory, can be either row-major or column-major order.

Row-Major Order The elements of a row are stored contiguously (side by side) in
memory. After finishing one row, storage continues with the next row. C, C++, Python
(NumPy default) use row-major order.

Example (2×3 matrix): Memory (row-major):


1 2 3 123456
4 5 6

Column-Major Order The elements of a column are stored contiguously in memory. After
finishing one column, storage continues with the next column. Fortran, MATLAB, R use
column-major order.

Example (same 2×3 matrix): Memory (column-major):


1 2 3 142536
4 5 6
• Row-Major vs Column-Major Order – Address formula calculation

Suppose we have a 2D array A[ROW][COL] stored in memory,


• Base Address (BA): Starting address of the array in memory
• W: Size of each element (in bytes)
• Indices: row index i, column index j

Row-Major Order (C, C++, Python default) Formula:


Address(A[i][j])=BA+(i×COL+j)×W

Column-Major Order (Fortran, MATLAB, R) Formula:


Address(A[i][j])=BA+(j×ROW+i)×W

Examples :
Matrix (3×3), Base Address = 1000, W = 4 bytes. Find address of A[1][2] (2nd row, 3rd
column) in both Row-major and Column-major order

Ans: Row-Major: 1000+(1×3+2)×4=1000+20=1020


Column-Major: 1000+(2×3+1)×4=1000+28=1028
5. Implementation of Array Operations – 1D

(a) Insertion in 1D Array (b) Deletion in 1D Array


#include <stdio.h> #include <stdio.h>
#define SIZE 100 #define SIZE 100

int main() { int main() {


int arr[SIZE], n, pos, val, i; int arr[SIZE], n, pos, i;

printf("Enter no. of elements: "); printf("Enter no. of elements: ");


scanf("%d", &n); scanf("%d", &n);

printf("Enter elements:\n"); printf("Enter elements:\n");


for(i = 0; i < n; i++) for(i = 0; i < n; i++)
scanf("%d", &arr[i]); scanf("%d", &arr[i]);

printf("Enter position to insert (0- printf("Enter position to delete (0-


%d): ", n); %d): ", n-1);
scanf("%d", &pos); scanf("%d", &pos);
printf("Enter value: ");
scanf("%d", &val); // Shift elements left
for(i = pos; i < n-1; i++)
// Shift elements arr[i] = arr[i+1];
for(i = n; i > pos; i--) n--;
arr[i] = arr[i-1];
arr[pos] = val; printf("Array after deletion:\n");
n++; for(i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("Array after insertion:\n");
for(i = 0; i < n; i++) return 0;
printf("%d ", arr[i]); }

return 0;
}

6. Implementation of Array Operations – 2D Array Insert

For a 2D array insertion program, we’ll extend the same concept:


• In a 1D array, we shift elements to the right.
• In a 2D array, we can insert a new element at a given row and column index,
shifting elements of that row to the right.
• If the row overflows, we can shift elements into the next row (like matrix-style
shifting).
#include <stdio.h>
#define ROWS 10
#define COLS 10

int main() {
int arr[ROWS][COLS];
int m, n; // current rows and columns
int row, col; // position to insert
int val;

printf("Enter number of rows and columns: ");


scanf("%d %d", &m, &n);

printf("Enter elements of %dx%d matrix:\n", m, n);


for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}

printf("Enter row (0-%d) and column (0-%d) to insert: ", m-1, n-1);
scanf("%d %d", &row, &col);

printf("Enter value: ");


scanf("%d", &val);

// Shift elements for insertion


for(int i = m-1; i >= row; i--) {
for(int j = n-1; j >= 0; j--) {
if(i == row && j >= col) {
arr[i][j+1] = arr[i][j]; // shift within row
}
else if(i > row) {
arr[i][j+1] = arr[i][j]; // shift all rows below
}
}
}

arr[row][col] = val;
n++; // increase column count (assuming row size fixed)

printf("Matrix after insertion:\n");


for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}
6. Implementation of Array Operations – 2D Array Delete
#include <stdio.h>

int main() {
int rows, cols;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int arr[100][100]; // maximum size


printf("Enter elements of the 2D array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

int delRow, delCol;


printf("Enter position to delete (row and column): ");
scanf("%d %d", &delRow, &delCol);

if (delRow < 0 || delRow >= rows || delCol < 0 || delCol >= cols) {
printf("Invalid position!\n");
return 0;
}

// Shift elements in the row to the left


for (int j = delCol; j < cols - 1; j++) {
arr[delRow][j] = arr[delRow][j + 1];
}

// Optional: If a row becomes empty, you can also delete that row
// Here we just reduce column count
cols--;

printf("\nArray after deletion:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}

Home work – Write the UPDATE programs for 1D and 2D array


7. Applications of Arrays
1. Storing data – Used to store marks, salaries, test scores, etc.
2. Matrices – Used in scientific computations, graphics, simulations.
3. Sorting & Searching – Arrays support algorithms like bubble sort, quicksort,
binary search.
4. Strings – Strings in C are implemented using character arrays.
5. Data Structures – Arrays are building blocks for stacks, queues, heaps, and hash
tables.
6. Lookup tables – Storing and accessing precomputed values (e.g., sine tables).

You might also like