Basic Examples of Array Data Structure Usage

In this section we will go through basic programming problems involving arrays.

Create and fill an array using Inputs from Command Line

In all the programming examples on arrays so far, we have hard coded all the values present in the array. But in most of the practical scenarios, we will have to take inputs dynamically when the program is actually being run. In this article, we will go through details of how to initialize and array and fill in the elements at run time by taking user input through command line or terminal.

Read More

Searching a value in Array Data Structure

As a beginner in the world of programming, you’ll often find yourself dealing with arrays—a fundamental data structure that stores collections of elements. Understanding how to search for a specific element within an array is an essential skill. In this article, we’ll introduce you to one of the simplest yet effective searching algorithms: the Linear Search.

Read More

Find Sum of all elements in an Array

In this article, we will go through code examples which help in finding sum of all elements present in an array. The intent of this article is to help understand various concepts in arrays, so we avoid using ready made functions like sum etc and instead use array iteration/traversal to find out the sum.

Read More

Find Maximum and Minimum elements in an Array

Finding the maximum and minimum values in an array is a fundamental operation in programming, often encountered when analyzing data. In this tutorial, we’ll explore how to accomplish this task using straightforward code examples.


#include <stdio.h>

int main() {
    // Declare an integer array
    int numbers[] = {10, 5, 20, 8, 15};

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

    // Initialize variables to store the maximum and minimum values
    int max = numbers[0]; // Assume the first element is the maximum
    int min = numbers[0]; // Assume the first element is the minimum

    // Loop through the array to find the maximum and minimum values
    for (int i = 1; i < count; i++) {
        // Check if the current element is greater than the current maximum
        if (numbers[i] > max) {
            max = numbers[i]; // Update the maximum value
        }

        // Check if the current element is smaller than the current minimum
        if (numbers[i] < min) {
            min = numbers[i]; // Update the minimum value
        }
    }

    // Display the maximum and minimum values
    printf("Maximum element in the array: %d\n", max);
    printf("Minimum element in the array: %d\n", min);

    return 0;
}

Here’s how the program works:

Read More

Reversing the order of elements in an Array Data Structure

Reversing an array involves re-arranging the order of all its elements such that the first element becomes the last, the second element becomes the second last element, and so on. For example:

Array Before Reversal:

[ A | B | C | D | E ]

Array After Reversal:

Read More

Rotate elements in an Array Data Structure

Array rotation is the process of shifting all elements present in an array by a specified number of positions either to the left or to the right. To understand the concept of array rotation, it is helpful to think of an array as a circular structure i.e, imagine that all the elements in an array are laid out in a circle, rather than a straight line. Below is how you can visualize an array as a circular structure:

Read More

Sort an Array Containing 0's and 1's

Sorting an array containing only 0s and 1s involves rearranging all the elements the array such that all 0’s are grouped together at the beginning, followed by all the 1’s. This kind of problem is also known as “binary array sorting” or “segregate 0s and 1s”.

Read More