18.10.25

1. Array Operations in C, The Group of Friendship (Create, Insert, Delete & Display)


hello everyone 

We often see arrays as plain boxes full of numbers but what if they were friends on a Group, always standing side by side, never breaking formation? Let’s turn the boring concept of “Array Operations” into something fun a story of a friendly squad that knows how to organize itself perfectly.

We’ll perform 4 main operations:
Create (assemble the group)
Insert (add a new friend)
Delete (say goodbye to one)
Display (see who’s still there)

Program: Array Operations in C

/* Array Operations in C: Create, Insert, Delete, Display (using loops and meaningful variable names) */
#include <stdio.h>

int main() {
    int numbers[20];              // Array to hold friends
    int arraySize, index, position, newValue;
    int userChoice;

    // Create the Array
    printf("Enter number of friends to place on the group: ");
    scanf("%d", &arraySize);

    printf("Enter the numbers (identities) of your friends:\n");
    for (index = 0; index < arraySize; index++) {
        scanf("%d", &numbers[index]);
    }

    // Menu for Operations
    do {
        printf("\n----- FRIEND GROUP MENU -----\n");
        printf("1. Display Friends\n");
        printf("2. Add a New Friend (Insert)\n");
        printf("3. Remove a Friend (Delete)\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &userChoice);

        switch (userChoice) {

            // Display Array
            case 1:
                printf("Current Friends on Group: ");
                for (index = 0; index < arraySize; index++) {
                    printf("| %d ", numbers[index]);
                }
                printf("|\n");
                break;

            // Insert New Element
            case 2:
                printf("Enter position to add new friend (0 to %d): ", arraySize);
                scanf("%d", &position);
                printf("Enter the new friend's number: ");
                scanf("%d", &newValue);

                if (position < 0 || position > arraySize) {
                    printf("Invalid position!\n");
                } else {
                    for (index = arraySize; index > position; index--) {
                        numbers[index] = numbers[index - 1];
                    }
                    numbers[position] = newValue;
                    arraySize++;
                    printf("Friend added successfully!\n");
                }
                break;

            // Delete Element
            case 3:
                printf("Enter position of friend to remove (0 to %d): ", arraySize - 1);
                scanf("%d", &position);

                if (position < 0 || position >= arraySize) {
                    printf("Invalid position!\n");
                } else {
                    printf("Friend %d at position %d has left the Group.\n", numbers[position], position);
                    for (index = position; index < arraySize - 1; index++) {
                        numbers[index] = numbers[index + 1];
                    }
                    arraySize--;
                    printf("Friend removed successfully!\n");
                }
                break;

            // Exit
            case 4:
                printf("Goodbye! The Group session is over.\n");
                break;

            default:
                printf("Invalid choice! Please try again.\n");
        }

    } while (userChoice != 4);

    return 0;
}

A Group of Friends

Imagine you have a Group, and each spot on it can hold one friend (represented by a number). Each spot is neatly labeled 0, 1, 2, and so on. That’s your array. When you start the program, you decide how many friends should stand there and enter their numbers one by one. That’s how your array is created a perfect lineup of buddies.

Checking Who’s on the Group Display

Whenever you want to see your squad, just choose the “Display” option. Each friend proudly waves as their number is shown on screen:

“Hey, I’m 10!” - “I’m 20!” - “I’m 30!” - “I’m 40!”

A simple loop introduces them all, one after another.

A New Friend Joins Insert

If your Group looked like {10, 20, 30} and you insert 25 at position 2,
it now becomes {10, 20, 25, 30} perfect harmony again!
A new person wants to join the gang. You choose where they’ll stand say, position 2. Everyone standing at and after that position moves one step to the right to make space. The new friend steps in, and the lineup is restored.

Example:

When a Friend Leaves Delete

Sometimes, someone has to go maybe the friend at position 1. When they leave, everyone behind them steps one place forward to fill the gap.

Example:
If your array is {10, 20, 30, 40} and position 1 leaves,
the new group becomes {10, 30, 40} the squad stays tight-knit.

Output 


🏠 Homepage Next Chapter →


No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...