9.10.25

Understanding Arrays in C



The Bookshelf Analogy & Book-Author Example

Arrays are one of the most essential concepts in C programming. They let you store multiple items of the same type in contiguous memory, making it easier to organize, access, and manipulate data. Think of an array as a bookshelf, where each shelf holds one book simple, organized, and neat!

In this blog, we’ll go through array concepts, C programs to create, display, and manage arrays, and a book-author example using arrays of strings.

The Bookshelf Analogy

Real-world item     Programming term Example
Bookshelf         Array     int favorite_numbers[8];
Book     Array element     favorite_numbers[0] = 100;
Shelf number     Index     favorite_numbers[3]
Number of shelves     Array size     8

Array Operations in C

creating, displaying, and managing arrays:

#include <stdio.h>

void DisplayIntArray(int given_array[], int array_data_size) {
    printf("Size of array is %d elements\n", array_data_size);
    printf("Array element values are:\n");
    for (int elementIndex = 0; elementIndex < array_data_size; elementIndex++) {
        printf("%d\n", given_array[elementIndex]);
    }
}

void DisplayFloatArray(float given_array[], int array_data_size) {
    printf("Size of array is %d elements\n", array_data_size);
    printf("Array element values are:\n");
    for (int elementIndex = 0; elementIndex < array_data_size; elementIndex++) {
        printf("%f\n", given_array[elementIndex]);
    }
}

int main() {
    // Integer Array Example
    int favorite_numbers[8];
    int favourite_numbers_length = sizeof(favorite_numbers) / sizeof(int);

    favorite_numbers[0] = 100;
    favorite_numbers[1] = 99;
    favorite_numbers[2] = 5;
    favorite_numbers[3] = 23;
    favorite_numbers[4] = 7;
    favorite_numbers[5] = 18;
    favorite_numbers[6] = 4;
    favorite_numbers[7] = 47;

    DisplayIntArray(favorite_numbers, favourite_numbers_length);

    // Another Integer Array Example
    int books[4] = {1984, 2001, 2010, 2020};
    DisplayIntArray(books, sizeof(books) / sizeof(int));

    // Float Array Example
    float favourite_fractions[3] = {3.14, 1.171, 2.72};
    DisplayFloatArray(favourite_fractions, sizeof(favourite_fractions) / sizeof(float));

    return 0;
}

Program Explanation

Creating Arrays
    int favorite_numbers[8];
    float favourite_fractions[3];

We declare arrays of a specific type and size.

Assigning Values

We can assign elements individually:
favorite_numbers[0] = 100;
favourite_fractions[1] = 1.171;

Or initialize at declaration:

int books[4] = {1984, 2001, 2010, 2020};

Displaying Arrays

We use functions with loops to display elements:
for (int i = 0; i < array_size; i++) {
    printf("%d\n", given_array[i]);
}

Array Size Calculation

int length = sizeof(favorite_numbers) / sizeof(int);

sizeof(favorite_numbers) → Total bytes of array
sizeof(int) → Size of one element
Divide to get number of elements

Important Points

Arrays store multiple values of the same type.
Indexing starts at 0.
Use loops to display or modify arrays efficiently.
Contiguous memory allows fast access.

Inserting & Deleting Elements

Since arrays have a fixed size, inserting or deleting requires shifting elements:

Delete the 2nd element example:

for(int i = 1; i < length - 1; i++) {
    favorite_numbers[i] = favorite_numbers[i + 1];
}
length--; // logically reduce array size

Book-Author Example Using Arrays of Strings

#include <stdio.h>

int main() {
    // Array of book titles
    char books[5][50] = {
        "Rich Dad Poor Dad",
        "1984",
        "The Book Theif",
        "The Alchemist",
        "Animal Farm"
    };

    // Array of authors corresponding to books
    char authors[5][50] = {
        "Robert Kiyosaki",
        "George Orwell",
        "Markus Zusak",
        "Paulo Coelho",
        "George Orwell"
    };

    printf("Book List:\n\n");

    // Display book and author
    for (int i = 0; i < 5; i++) {
        printf("Book: %s\nAuthor: %s\n\n", books[i], authors[i]);
    }

    return 0;
}

Explanation

books[5][50] → 5 book titles, each up to 50 characters.

authors[5][50] → 5 authors corresponding to books.

Using the same index, we link a book with its author.
Loop through arrays to display paired data.

Output


Book List:

Book: Rich Dad Poor Dad
Author: Robert Kiyosaki

Book: 1984
Author: George Orwell

Book: The Book Theif
Author: Markus Zusak

Book: The Alchemist
Author: Paulo Coelho

Book: Animal Farm
Author: George Orwell


No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...