Array Insertion

Algorithm

Step 1: Start

Step 2: Declare an array of size 7, integer i, pos, and n

Step 3: Print the initial array

Step 4: Read the position and value to insert

Step 5: Shift elements to the right from the given position

Step 6: Insert the new value at the given position

Step 7: Print the updated array

Step 8: End

Flowchart

graph TD; A([Start]) --> B["Initialize array with values"]; B --> C[/Print initial array/]; C --> D[/Input position and value/]; D --> E{Check if position is valid}; E -->|Yes| F[Shift elements to the right]; E -->|No| G[/Print Invalid position and End/]; F --> H[Insert new value at position]; H --> I[/Print new array/]; I --> J([End]); classDef start fill:#00cc44,stroke:#006622,stroke-width:2px,color:#ffffff; classDef stop fill:#ff4444,stroke:#cc0000,stroke-width:2px,color:#ffffff; classDef process fill:#4488ff,stroke:#0044cc,stroke-width:2px,color:#ffffff; classDef decision fill:#ff8844,stroke:#cc4400,stroke-width:2px,color:#ffffff; classDef input_output fill:#ffdd44,stroke:#cc9900,stroke-width:2px,color:#000000; class A start; class J,G stop; class B,F,H process; class C,D,I input_output; class E decision;

Program

#include <stdio.h>

int main() {
    int ar[7] = {10, 20, 30, 40, 50}, i, pos, n;
    printf("\nPrint Array");
    for (i = 0; i < 5; i++) {
        printf("\n%d", ar[i]);
    }
    printf("\nEnter position to insert:");
    scanf("%d", &pos);
    printf("\nEnter value:");
    scanf("%d", &n);
    
    // Validate position
    if (pos < 0 || pos > 5) {
        printf("\nInvalid position!");
        return 1;
    }
    
    // Shift elements
    for (i = 5; i >= pos; i--) {
        ar[i + 1] = ar[i];
    }
    ar[pos] = n;
    
    printf("\nPrint New Array");
    for (i = 0; i < 6; i++) {
        printf("\n%d", ar[i]);
    }
    return 0;
}
                
Test Case Output
Print Array
10
20
30
40
50
Enter position to insert: 2
Enter value: 25
Print New Array
10
20
25
30
40
50