Introduction

An array is a collection of similar data types (e.g., char, int, short, long, struct A, etc.) that are stored in continuous memory locations and can be accessed using a single variable name. In other words, we can say that it is a data structure that can store multiple datatypes of same types and can be accessed using the index.

Array
Array

Let us say we need to store the family member count of each house in any area called “Block A”. So, we can use
int house_1= 5;
int house_2= 3;
int house_3= 3;
and so on. But if that block has 150 houses then we may need lot of patience to declare so many int variables. Declaring so many variables is one small part of big problem. The bigger challenge will finding the value stored in any variable e.g. house_133. We will either need to use if-else ladder or switch case. both are not a good solution.

So, here is a solution that can solve the problem and can be accessed in fixed constant time. Below is the example for this:

int family_member_in_house[150];

here we have created an array named family_member_in_house which can store 150 entries. To access the values at any index e.g. 10 we can use family_member_in_house[10]. As we can access any element in a constant time so it is efficient way also. Let is now understand it with example code of array i.e. initializing, writing, reading it.

Example

#include <stdio.h>

#define HOUSE_COUNT 150

int main() {
    int family_member_in_house[HOUSE_COUNT] = {0}; // Initializing array with 0
    int house_number, member_count;

    // Ask user to enter the number of family members for each house
    printf("Enter the number of family members for each house:\n");
    for (int i = 0; i < HOUSE_COUNT; i++) {
        printf("House %d: ", i+1);
        scanf("%d", &family_member_in_house[i]);
    }

    // Ask user to enter house number and reply back with number of members in that house
    while (1) { // Keep prompting user until they enter an invalid house number
        printf("\nEnter a house number (1-%d) to check member count (enter 0 to quit): ", HOUSE_COUNT);
        scanf("%d", &house_number);

        if (house_number == 0) {
            break; // Exit the loop if the user entered 0
        } else if (house_number < 1 || house_number > HOUSE_COUNT) {
            printf("Invalid house number. Please try again.\n");
            continue; // Go back to the beginning of the loop
        }

        member_count = family_member_in_house[house_number - 1]; // Subtract 1 to get the correct index
        printf("There are %d members in House %d.\n", member_count, house_number);
    }

    return 0;
}

Here we have declared an array with size=150. int family_member_in_house[ HOUSE_COUNT] = {0}. “{0}” means that we are initializing it with 0. We can also initialize it like {1, 2, 10, 5, … if we want to initialize the array with predefined number.

After that, we asked user to enter the number of family members using family_member_in_house[i]. This is the method to access the index of array. i can be 0 to 149. The index in array starts with 0 instead of. So arr[10]=40 is the way to write 40 to the index 10 of array arr.

Then we are reading the array using family_member_in_house[house_number - 1].

An Array is stored in the memory in continuous memory. So, when we are trying to read/write to it then the processor takes the base address of array and adds the index to that memory. in this way it is able to read/write in constant time.

Advantages & disadvantage

AdvantagesDisadvantages
Allows for efficient storage and retrieval of multiple elements of the same data typeIt has a fixed size, so they cannot be resized dynamically at runtime
Provides direct and fast access to any element of the array using its indexIt may consume more memory than necessary if not sized properly
Enables efficient traversal of elements using loops or other iterative constructsInserting or deleting elements in an array can be inefficient and may require shifting elements
Can be used to implement various data structures such as stacks, queues, and treesArray indices can be error-prone and lead to out-of-bounds errors
Allows for easy and efficient sorting and searching of elements using built-in algorithms or user-defined functionsMultidimensional arrays can be difficult to work with and understand
Can be passed to functions as arguments, making it easy to manipulate them using functionsArrays in some programming languages may not automatically initialize all their elements to zero, leading to uninitialized values

Reference

2 thoughts on “Array: The Unsung Hero of Programming”

Leave a Reply

Your email address will not be published. Required fields are marked *