0% found this document useful (0 votes)
17 views12 pages

Data Structures: Arrays Explained

The document provides a comprehensive overview of linear data structures, specifically focusing on arrays, including their definitions, types (1D, 2D, and multidimensional), memory representation, indexing, and operations such as insertion, deletion, and traversal. It also highlights the applications of arrays in various fields like data storage, image processing, and scientific simulations, as well as the concept of sparse matrices and their efficient storage methods. Additionally, it includes practical programming examples in C to illustrate the concepts discussed.

Uploaded by

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

Data Structures: Arrays Explained

The document provides a comprehensive overview of linear data structures, specifically focusing on arrays, including their definitions, types (1D, 2D, and multidimensional), memory representation, indexing, and operations such as insertion, deletion, and traversal. It also highlights the applications of arrays in various fields like data storage, image processing, and scientific simulations, as well as the concept of sparse matrices and their efficient storage methods. Additionally, it includes practical programming examples in C to illustrate the concepts discussed.

Uploaded by

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

Shantilal Shah Engineering College, Bhavnagar

Information Technology Department


rd
3 Semester BE03000081- Data Structure
Unit-2 LINEAR DATA STRUCTURE
Lecture Notes (Array)
1. Introduction to Arrays
1. Definition:
An array is a data structure that stores a fixed-size collection of elements of the
same data type. These elements are stored in contiguous memory locations and
can be accessed using an index. Arrays are widely used in programming for
organizing and processing sets of data efficiently.

2. Syntax (C Language):
data_type array_name[size];
Example:
int numbers[5]; // Declares an array of 5 integers
float marks[10]; // Declares an array of 10 float values
char grade[4]; // Declares an array of 4 characters

3. Real-life Analogy:
Consider an array like a row of mailboxes or a parking lot:
- Each mailbox (or parking spot) is a location (index) in the array.
- Each letter (or car) placed in the mailbox (or parking spot) is the data stored at
that index.
- You can easily access any mailbox (or parking spot) using its number (index).

4. Purpose and Importance of Arrays:


- Arrays allow efficient storage and retrieval of multiple values using a single
variable name.
- Arrays reduce code complexity by eliminating the need for multiple variables.
- Arrays enable easy traversal and manipulation of data using loops.
- Arrays are essential for implementing algorithms such as searching, sorting, and
matrix operations.
- Useful in applications like student mark sheets, image processing, statistical
computations, etc.

2. Types of Arrays

1|Page
1. One-Dimensional Array (1D Array)

A one-dimensional array is a list of elements stored in a single row.


It is accessed using a single index.

Example:

For example:

int arr[5] = {10, 20, 30, 40, 50};

 Assume the base address of arr[0] is 1000.


 Since int takes 2 bytes, elements will be stored as:

Elemen Addres
t s
arr[0] 1000
arr[1] 1002
arr[2] 1004
arr[3] 1006
arr[4] 1008

Program:
#include <stdio.h>
void main()
{
int arr[3] = {10, 20, 30, 40, 50};
for(int i = 0; i <= 4; i++)
{
printf("Element at index %d = %d\n", i, arr[i]);
}
getch();
}

2. Two-Dimensional Array (2D Array)

A two-dimensional array stores data in matrix form and is accessed using


two indices (row and column).

Example:

int mat[2][3] =
{
{1, 2, 3},
{4, 5, 6}

2|Page
};

Stored in row-major order (C default):

mat[0][0], mat[0][1], mat[0][2], mat[1][0], mat[1][1], mat[1][2]

If base address is 2000, and int = 2 bytes:

mat[0][0] = 2000
mat[0][1] = 2002
mat[0][2] = 2004
mat[1][0] = 2006
mat[1][1] = 2008
mat[1][2] = 2010

Program:
#include <stdio.h>
void main()
{
int mat[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
printf("mat[%d][%d] = %d\n", i, j, mat[i][j]);
}
}
getch();
}

3. Multidimensional Array (3D Array)

A multidimensional array can have more than two dimensions.


A 3D array represents data in depth, row, and column format.

Example:

int arr[2][2][2] =
{
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};

3|Page
Program:
#include <stdio.h>
void main()
{
int arr[2][2][2] =
{
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
}
}
}
getch();
}

3. Memory Representation & Indexing in Arrays

1. Memory Size Calculation:

To calculate the memory used by an array:

📌 For 1D array:

int arr[5];

 Size = number of elements × size of data type


 5 × 2 = 10 bytes (if int is 2 bytes)

📌 For 2D array:

int mat[3][4];

 Total elements = 3 × 4 = 12
 Memory size = 12 × 2 = 24 bytes

4|Page
2. Indexing in Arrays

Arrays use zero-based indexing, meaning the first element is at index 0.

📌 Formula:

Address of arr[i] = Base_Address + (i × size_of_element)

🧮 Example:

int arr[5]; // base = 1000

 arr[2] is at: 1000 + (2 × 2) = 1004

3. Accessing Elements via Index

You can access elements directly using their index:

printf("%d", arr[3]); // accesses 4th element

❗ Important Points:

 Array index starts from 0.


 Accessing out-of-bound index is undefined behavior.

arr[10]; // invalid if size is 5

5|Page
4. Operations on Arrays
1. Insert Operation

Goal: Add an element at a specific index in the array.

🧾 C Program for Insert:


#include <stdio.h>

void main()
{
int arr[100] = {10, 20, 30, 40, 50};
int size = 5, i, pos = 2, element = 25;

printf("Original Array: ");


for(i = 0; i < size; i++)
printf("%d ", arr[i]);

// Shifting elements to the right


for(i = size; i > pos; i--)
arr[i] = arr[i - 1];

arr[pos] = element;
size++;

printf("\nArray after Insertion at position %d: ", pos);


for(i = 0; i < size; i++)
printf("%d ", arr[i]);

getch();
}

🧠 Explanation:

 We shift all elements from the position (to be inserted) to the right.
 Insert the element at the required index.
 Increase the size of the array.

2. Delete Operation

Goal: Remove an element from a specific index and shift the remaining
elements.

🧾 C Program for Delete:


#include <stdio.h>

6|Page
void main()
{
int arr[100] = {10, 20, 30, 40, 50};
int size = 5, i, pos = 2;

printf("Original Array: ");


for(i = 0; i < size; i++)
printf("%d ", arr[i]);

// Deleting element at position


for(i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];

size--;

printf("\nArray after Deletion at position %d: ", pos);


for(i = 0; i < size; i++)
printf("%d ", arr[i]);

getch();
}

🧠 Explanation:

 Shift all elements to the left from the position.


 The last value becomes a duplicate or garbage, so we reduce the
logical size.

3. Traverse Operation

Goal: Print or process every element of the array.

🧾 C Program for Traverse:


#include <stdio.h>

void main()
{
int arr[] = {5, 10, 15, 20, 25};
int size = sizeof(arr)/sizeof(arr[0]), i;

printf("Traversing Array: ");


for(i = 0; i < size; i++)
printf("%d ", arr[i]);

getch();
}

🧠 Explanation:

7|Page
 Simple for loop from index 0 to size-1.
 Allows viewing, modifying, or analyzing elements.

✅ Summary Table

Operatio
Description Key Step
n
Insert Add element at index Shift right, insert
Delete Remove element from index Shift left, size--
Access each element
Traverse Loop through array
sequentially

5. Applications of Arrays
Arrays are fundamental data structures used in a wide variety of applications
due to their simple structure, fast access time, and ease of use.

1. Storing Data Collections

 Arrays are used to store multiple values of the same type in a single
variable.
 Example: Storing the marks of 100 students:
int marks[100];

2. Matrix Representation

 2D arrays are commonly used to represent matrices in mathematics


and physics.
 Example:

int matrix[3][3] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

8|Page
3. Image Processing

 Digital images are stored as 2D arrays of pixel values.


 In grayscale images: each pixel is represented as a value from 0–255.
 Color images use 3D arrays for RGB components.

4. Database Table Implementation

 Tables in databases can be simulated using 2D arrays.


 Each row can represent a record, and each column a field.

5. Used in Sorting and Searching Algorithms

 Arrays are commonly used with:


o Linear Search
o Binary Search
o Bubble Sort, Quick Sort, Merge Sort, etc.

6. Implementing Other Data Structures

 Arrays are used internally to implement:


o Stacks
o Queues
o Heaps
o Hash Tables
o Graphs (Adjacency Matrix)

7. Storing Sensor Data in Embedded Systems

 Arrays store continuous stream data from sensors (temperature,


pressure, etc.) in IoT devices.

8. Game Development

 Used to maintain the game board (like chess, tic-tac-toe, or


minesweeper) as a 2D array.
 Arrays also store positions, scores, and player stats.

9|Page
9. String Manipulation

 In C, strings are implemented as character arrays:

char name[10] = "Chintan";

10. Scientific Simulations

 Arrays are heavily used in simulations for physics, chemistry, and


engineering (like weather models, fluid simulations).

6. Sparse Matrix

1. Definition:
A sparse matrix is a matrix in which most of the elements are zero. In contrast, a dense matrix
has most of its elements as non-zero. When working with large matrices, storing all elements—
including zeros—can waste memory. Therefore, sparse matrices are stored in a special format
to save memory and computation time.

2. Why Use Sparse Matrix?


- Efficient storage for large matrices with few non-zero elements.
- Faster processing when most data is zero.
- Useful in areas such as:
• Graphs (Adjacency matrices)
• Machine learning (e.g., document-term matrices)
• Image processing

Example:
Consider a 5x5 matrix:

0 0 0 0 9
0 0 8 0 0
0 0 0 0 0
6 0 0 0 0
0 0 0 7 0

This matrix has only 4 non-zero elements. Storing all 25 elements is inefficient. Instead, we can
store only non-zero elements using Triplet Representation.

10 | P a g e
3. Triplet Representation:
Row Column Value

0 4 9

1 2 8

3 0 6

4 3 7

4. C Code to Convert Sparse Matrix to Triplet Form:

#include <stdio.h>

int main() {
int sparse[5][5] = {
{0, 0, 0, 0, 9},
{0, 0, 8, 0, 0},
{0, 0, 0, 0, 0},
{6, 0, 0, 0, 0},
{0, 0, 0, 7, 0}
};

printf("Triplet Representation:\n");
printf("Row\tCol\tValue\n");

for (int i = 0; i < 5; i++) {


for (int j = 0; j < 5; j++) {
if (sparse[i][j] != 0) {
printf("%d\t%d\t%d\n", i, j, sparse[i]
[j]);
}
}
}
return 0;
}

11 | P a g e
5. Benefits of Sparse Matrix:
- Memory Efficiency: Only store what's needed.
- Faster Computation: Avoid processing zeros.
- Useful for Algorithms: Especially in graph and scientific computing.

Self Learning Program for Practice:

1. To insert 10 element in 1-dimensional array and print the same.


2. To find minimum and maximum element from 1-Dimensional array.
3. To search the element in 1-dimensional array using given index.
4. To read and store the roll no and marks of 10 students using array.
5. To find the sum and average of different numbers which are accepted by
user as many as he wants.
6. To find out which number is even or odd from list of 10 numbers using
array.
7. To Count and display of positive or negative numbers in an array.
8. To insert element in one dimensional array at given index.
9. To delete element in one dimensional array from given index.
10. To merge two 1-dimensional arrays.
11. Read five persons height and weight and count the number of person
having height greater than 170 and weight less than 50.
12. Insert element in 2-dimensional array and print the same.
13. Traverse the 2- dimensional array in row and column manner.
14. Add two 2-dimensional arrays.
15. Multiply two 2-dimensional arrays.

12 | P a g e

You might also like