Understanding Arrays in C Programming

5 minute
919 words
Categories:
Web Development
Tags:

What is Array

An array is a collection of the same type of data items, which are stored in consecutive memory locations under a common name.

Features of Array

  • An array is a linear data structure.
  • An array can store many values using a single variable name.
  • Arrays store elements of the same data type.
  • Every element has an index number. Array elements can be accessed quickly using index numbers.
  • Array elements are stored in continuous memory locations.

Types of Array

There are two types of Arrays :

1. One-Dimensional Array

A One-dimensional array is an array in which only one index is used to access or specify an element of the array.

Declaration of One-Dimensional Array

In C, One-dimensional array can be declared as follows:

data_type array_name [array_size];

Here, array_name is the name of the array, and array_size represents how many elements the array can store. The data_type specifies the type of data stored in the array, such as int, float, or char. The size of the array must be a whole number.

For example int array[5];

Initialization of One-Dimensional Array

In C, One-dimensional array can be initialized in two ways.

  • Initialization with Declaration

In this method, values are assigned to the array at the time of declaration as follows:

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

For example: int array[] = {1, 2, 3, 4, 5};. Here, it is not necessary to define size of array.

  • Initialization After Declaration

In this method, the array is declared first, and values are assigned later using indexes as follows:

array_name[index_number] = value1;

For example: if int array[5]; then array[0] = 1;. Now, first element of array is 1.

C
1
#include <stdio.h>
2

3
int main() {
4
    
5
    int arr[] = {2, 4, 8, 12, 16, 18};
6
    int n = sizeof(arr)/sizeof(arr[0]);
7

8
    // Printing array elements
9
    for (int i = 0; i < n; i++) {
10
        printf("%d ", arr[i]);
11
    }
12

13
    return 0;
14
}

Operations Perform on Arrays

1. Traversing

Traversing means accessing and displaying each element of an array one by one.

C Program for Traversing Array:

C
1
#include <stdio.h>
2

3
int main() {
4

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

7
    for(int i = 0; i < 5; i++) {
8
        printf("%d ", arr[i]);
9
    }
10

11
    return 0;
12
}

2. Insertion

Insertion means adding a new element into an array at a specific position.

C Program for Insertion

C
1
#include <stdio.h>
2

3
int main() {
4

5
    int arr[6] = {10, 20, 30, 40, 50};
6
    int pos = 2;
7
    int value = 25;
8
    int n = 5;
9

10
    for(int i = n; i > pos; i--) {
11
        arr[i] = arr[i - 1];
12
    }
13

14
    arr[pos] = value;
15
    n++;
16

17
    for(int i = 0; i < n; i++) {
18
        printf("%d ", arr[i]);
19
    }
20

21
    return 0;
22
}

3. Deletion

Deletion means removing an element from an array. After deletion, remaining elements are shifted left.

C Program for Deletion

C
1
#include <stdio.h>
2

3
int main() {
4

5
    int arr[5] = {10, 20, 30, 40, 50};
6
    int pos = 2;
7
    int n = 5;
8

9
    for(int i = pos; i < n - 1; i++) {
10
        arr[i] = arr[i + 1];
11
    }
12

13
    n--;
14

15
    for(int i = 0; i < n; i++) {
16
        printf("%d ", arr[i]);
17
    }
18

19
    return 0;
20
}

4. Merging

Merging means combining two arrays into a single array.

C Program for Merging Arrays

C
1
#include <stdio.h>
2

3
int main() {
4

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

9
    for(int i = 0; i < 3; i++) {
10
        arr3[i] = arr1[i];
11
    }
12

13
    for(int i = 0; i < 3; i++) {
14
        arr3[i + 3] = arr2[i];
15
    }
16

17
    for(int i = 0; i < 6; i++) {
18
        printf("%d ", arr3[i]);
19
    }
20

21
    return 0;
22
}

5. Searching

Searching means finding whether an element exists in an array or not.

C Program for Searching

C
1
#include <stdio.h>
2

3
int main() {
4

5
    int arr[5] = {10, 20, 30, 40, 50};
6
    int item = 30;
7
    int found = 0;
8

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

11
        if(arr[i] == item) {
12
            found = 1;
13
            printf("Element found at index %d", i);
14
            break;
15
        }
16
    }
17

18
    if(found == 0) {
19
        printf("Element not found");
20
    }
21

22
    return 0;
23
}

6. Sorting

Sorting means arranging array elements in a particular order such as ascending or descending order.

C Program for Sorting Array

C
1
#include <stdio.h>
2

3
int main() {
4

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

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

10
        for(int j = i + 1; j < 5; j++) {
11

12
            if(arr[i] > arr[j]) {
13

14
                temp = arr[i];
15
                arr[i] = arr[j];
16
                arr[j] = temp;
17
            }
18
        }
19
    }
20

21
    for(int i = 0; i < 5; i++) {
22
        printf("%d ", arr[i]);
23
    }
24

25
    return 0;
26
}

Previous

Git Setup for a New Project: A Complete Step-by-Step Guide

Next

HTML Element Reference Guide for Web Developers