Array Examples




Array Basics

Array Initialization



This example demonstrates two common ways to initialize arrays in C: using an initializer list and using a loop to set values.




Array Iteration

Array Traversal



This example shows two methods to traverse an array: using array indexing and using pointer arithmetic.

Array Searching



This code demonstrates a linear search to find a specific element in an array. It iterates through the array and stops when the target is found or the end is reached.




Array Manipulation

Array Sorting



This example implements the Bubble Sort algorithm to sort an array in ascending order. It repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.

Array Reversal



This function reverses the order of elements in an array in-place. It uses two pointers, one starting from the beginning and one from the end, swapping elements as it moves towards the center of the array.

Array Rotation



This code rotates an array to the left by a specified number of positions. It first stores the elements to be rotated, then shifts the remaining elements, and finally places the stored elements at the end.

Array Merging



This code merges two sorted arrays into a single sorted array. It compares elements from both arrays and adds them to the result array in the correct order.




Multi-dimensional Arrays

Array 2d Operations



This example demonstrates operations on a 2D array, including initialization, traversal, and element summation. It shows how to access and manipulate elements in a multi-dimensional array.




Dynamic Memory Management

Array Dynamic Allocation



This example shows how to dynamically allocate memory for an array, resize it, and properly free the memory. It demonstrates the use of malloc() for initial allocation, realloc() for resizing, and free() for deallocating memory.

Related Examples