C language content

One-Dimensional Arrays in C Language

A one-dimensional array in C is a linear data structure that stores a collection of elements of the same type in contiguous memory locations. Each element is accessed by its index, which starts from 0.

Declaring One-Dimensional Arrays

The syntax for declaring a one-dimensional array in C is:

				
					type arrayName[arraySize];

				
			
  • type: The data type of the array elements (e.g., int, float, char).
  • arrayName: The name of the array.
  • arraySize: The number of elements in the array

Example:

				
					int numbers[5];  // Declares an array of 5 integers

				
			

Initializing One-Dimensional Arrays

You can initialize an array at the time of declaration:

				
					int numbers[5] = {1, 2, 3, 4, 5};  // An array with 5 elements initialized

				
			

If you initialize fewer elements than the size of the array, the remaining elements will be set to 0 by default:

Example:

				
					int numbers[5] = {1, 2};  // Remaining elements will be initialized to 0

				
			

You can also omit the size of the array if you provide the initialization list:

				
					int numbers[] = {1, 2, 3, 4, 5};  // Size will be determined by the number of elements

				
			

Accessing Elements of One-Dimensional Arrays

Elements of an array are accessed using their indices. The index of the first element is 0, the second element is 1, and so on.

Example:

				
					int firstElement = numbers[0];  // Accesses the first element
int secondElement = numbers[1]; // Accesses the second element

				
			

Modifying Elements of One-Dimensional Arrays

You can modify an array element by assigning a new value to it using its index:

				
					numbers[0] = 10;  // Changes the first element to 10
numbers[1] = 20;  // Changes the second element to 20

				
			

Example Program

Here’s a simple C program that demonstrates the declaration, initialization, accessing, and modifying elements of a one-dimensional array:

				
					#include <stdio.h>

int main() {
    // Declare and initialize an array
    int numbers[5] = {1, 2, 3, 4, 5};
    
    // Access and print array elements
    printf("Original array elements:\n");
    for(int i = 0; i < 5; i++) {
        printf("Element at index %d: %d\n", i, numbers[i]);
    }
    
    // Modify array elements
    numbers[0] = 10;
    numbers[1] = 20;
    
    // Print modified array elements
    printf("Modified array elements:\n");
    for(int i = 0; i < 5; i++) {
        printf("Element at index %d: %d\n", i, numbers[i]);
    }
    
    return 0;
}

				
			

Array Size Determination

You can determine the size of an array using the sizeof operator:

				
					int size = sizeof(numbers) / sizeof(numbers[0]);  // Calculates the number of elements in the array

				
			

Passing Arrays to Functions

Arrays can be passed to functions by passing the array’s name, which is a pointer to the first element. The function must also know the size of the array.

Example:

				
					#include <stdio.h>

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

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    printArray(numbers, 5);  // Passing the array to a function
    
    return 0;
}

				
			

Common Operations on One-Dimensional Arrays

Traversal

Traversing an array means accessing each element of the array. It is commonly done using loops.

Example:

				
					for(int i = 0; i < 5; i++) {
    printf("%d ", numbers[i]);
}

				
			

Insertion

Inserting an element into a specific position of an array involves shifting elements to the right.

Example (inserting 50 at index 2):

				
					#include <stdio.h>

int main() {
    int numbers[6] = {1, 2, 3, 4, 5};
    int size = 5;  // Current number of elements in the array
    int newElement = 50;
    int position = 2;
    
    // Shift elements to the right
    for(int i = size; i > position; i--) {
        numbers[i] = numbers[i - 1];
    }
    
    // Insert new element
    numbers[position] = newElement;
    size++;
    
    // Print updated array
    for(int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    return 0;
}

				
			

Deletion

Deleting an element from a specific position in an array involves shifting elements to the left.

Example (deleting the element at index 2):

				
					#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    int size = 5;  // Current number of elements in the array
    int position = 2;
    
    // Shift elements to the left
    for(int i = position; i < size - 1; i++) {
        numbers[i] = numbers[i + 1];
    }
    
    size--;  // Decrease the size of the array
    
    // Print updated array
    for(int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    return 0;
}

				
			

Searching

Searching an array involves finding the index of a specific element.

Example (linear search):

				
					#include <stdio.h>

int linearSearch(int arr[], int size, int element) {
    for(int i = 0; i < size; i++) {
        if(arr[i] == element) {
            return i;
        }
    }
    return -1;  // Element not found
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    int element = 3;
    int index = linearSearch(numbers, 5, element);
    
    if(index != -1) {
        printf("Element %d found at index %d\n", element, index);
    } else {
        printf("Element %d not found\n", element);
    }
    
    return 0;
}

				
			

Sorting

Sorting an array involves arranging the elements in a specific order (ascending or descending).

Example (bubble sort):

				
					#include <stdio.h>

void bubbleSort(int arr[], int size) {
    for(int i = 0; i < size - 1; i++) {
        for(int j = 0; j < size - i - 1; j++) {
            if(arr[j] > arr[j + 1]) {
                // Swap elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int numbers[5] = {5, 3, 1, 4, 2};
    bubbleSort(numbers, 5);
    
    // Print sorted array
    for(int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    
    return 0;
}

				
			

Summary

One-dimensional arrays are a fundamental data structure in C that allows for efficient storage and manipulation of a collection of elements of the same type. Understanding how to declare, initialize, access, and perform operations on arrays is crucial for effective programming in C.

Scroll to Top