0% found this document useful (0 votes)
2 views

array ds 3 (1)

Uploaded by

12215139
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

array ds 3 (1)

Uploaded by

12215139
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Arrays in Data Structures and C Programming

An array is a collection of items stored at contiguous memory locations. In simple


terms, it's like a list that holds multiple values of the same type, such as integers,
floating points, or characters. Arrays are an important concept in data structures
because they allow efficient storage and access to a fixed number of elements.

Features of Arrays

1. Fixed Size: The size of an array is specified at the time of declaration and
cannot be changed later. For example, an array of size 10 can hold exactly 10
elements.
2. Homogeneous Data: All elements in an array must be of the same data type.
For example, an integer array can only store integers.
3. Indexed Access: Each element in an array is identified by an index, starting
from 0. This allows easy and fast access to elements.
4. Continuous Memory Allocation: All elements are stored in adjacent memory
locations.

Declaration of Arrays in C

To declare an array in C, you specify the data type, array name, and the size of the
array.

Syntax:

data_type array_name[array_size];

Example:

int numbers[5]; // Declares an array 'numbers' of size 5 to store integers

In the above example, numbers can store 5 integers, and you can access the
elements using indices from 0 to 4 (e.g., numbers[0], numbers[1]).

Initialization of Arrays

You can initialize an array at the time of declaration by providing a list of values.

Syntax:

data_type array_name[array_size] = {value1, value2, ..., valueN};

Example:
int numbers[5] = {10, 20, 30, 40, 50};

Alternatively, if the number of elements is known, you can omit the size, and the
compiler will automatically calculate it.

Example:

int numbers[] = {10, 20, 30, 40, 50}; // Size automatically set to 5

Accessing Elements of an Array

You can access the elements of an array using their index. The first element is at
index 0, the second at index 1, and so on.

Example:

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

printf("%d", numbers[0]); // Output: 10


printf("%d", numbers[2]); // Output: 30

You can also modify the values of array elements by assigning new values to
specific indices.

Example:

numbers[1] = 25; // Changes second element from 20 to 25

Array Traversal

Traversing an array means accessing each element of the array one by one. This is
commonly done using a for loop.

Example:

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

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


printf("%d ", numbers[i]);
}

Types of Arrays

1. One-dimensional Array: A single row of data (as discussed so far).


2. Multi-dimensional Arrays: Arrays that can have more than one dimension,
such as 2D or 3D arrays.
2D Arrays (Multi-Dimensional Arrays)

A 2D array can be thought of as a matrix or a table with rows and columns.

Syntax:

data_type array_name[rows][columns];

Example:

int matrix[3][4]; // Declares a 2D array with 3 rows and 4 columns

also initialize 2D arrays like this:

Example:

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

Accessing 2D Array Elements

To access an element in a 2D array, you need two indices: one for the row and one
for the column.

Example:

printf("%d", matrix[0][1]); // Output: 2

To traverse a 2D array, you use two for loops: one for the rows and one for the
columns.

Example:

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


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

Advantages of Arrays

1. Random Access: You can access any element directly using its index.
2. Efficiency: Arrays are efficient in terms of memory and accessing elements.
3. Easy Traversal: Looping through elements is easy.

Disadvantages of Arrays
1. Fixed Size: The size of an array is fixed, which can lead to unused memory or
overflow errors.
2. Homogeneous Data: Arrays can store only one type of data.
3. Costly Insertions/Deletions: Inserting or deleting elements in an array
(except at the end) requires shifting the other elements, which can be time-
consuming.

Common Operations on Arrays

1. Insertion: Adding an element to the array at a specific index (may require


shifting).
2. Deletion: Removing an element from the array (requires shifting the elements
to fill the gap).
3. Searching: Finding an element in the array (linear search, binary search).
4. Sorting: Rearranging the elements in a particular order (ascending or
descending).

Array Operations in C

1. Insertion:

#include <stdio.h>

void insert(int arr[], int n, int pos, int value) {


for (int i = n - 1; i >= pos; i--) {
arr[i + 1] = arr[i]; // Shift elements
}
arr[pos] = value;
}

int main() {
int arr[6] = {1, 2, 3, 5, 6};
int size = 5; // Current size of array

insert(arr, size, 3, 4); // Insert 4 at index 3

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


printf("%d ", arr[i]);
}

return 0;
}

2. Deletion:

#include <stdio.h>
void delete(int arr[], int n, int pos) {
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements
}
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int size = 5;

delete(arr, size, 2); // Delete element at index 2

for (int i = 0; i < size - 1; i++) {


printf("%d ", arr[i]);
}

return 0;
}

3. Searching (Linear Search):

#include <stdio.h>

int search(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index of the element
}
}
return -1; // Element not found
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int key = 3;

int index = search(arr, 5, key);

if (index != -1) {
printf("Element found at index %d\n", index);
} else {
printf("Element not found\n");
}

return 0;
}

4. Sorting (Bubble Sort):


#include <stdio.h>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[5] = {5, 3, 1, 4, 2};
int size = 5;

bubbleSort(arr, size);

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


printf("%d ", arr[i]);
}

return 0;
}

You might also like