INSERTION IN ARRAY

 INSERTION IN ARRAY:-


ALGORITHM:-    

 STEP 1. set j=n

 STEP2. repeat step 3 and 4 while j>=k

 STEP3. set LA[j+1];=LA[J]

 STEP4. SET J:= J-1

 STEP5. SET LA[K]:=ITEM

 STEP6.  SET N:=N+1

 STEP7. EXIT


CODE :- 

#include<stdio.h>

#include<conio.h>


void main() {

    int a[5], i = 0, j, position, value, size = 5;

    clrscr();

     printf("Enter array elements:\n");

    

    for (j = 0; j < 5; j++) {

        scanf("%d", &a[j]);

    }

    

    printf("Enter the position to insert (0-based index): ");

    scanf("%d", &position);

    

    if (position < 0 || position > size) {

        printf("Invalid position. Insertion failed.\n");

    } else {

        printf("Enter the value to insert: ");

        scanf("%d", &value);

        for (i = size - 1; i >= position; i--) {

            a[i + 1] = a[i];

        }

        a[position] = value;

        size++;

        printf("Array after insertion:\n");

        for (i = 0; i < size; i++) {

            printf("%d ", a[i]);

        }

        printf("\n");

    }


    getch();


}



TIME COMPLEXITY:-   

Comments

Popular posts from this blog

AI Lab file 2024

AI Prolog PDF