0% found this document useful (0 votes)
53 views

Program 01: Write A Program To Implement For The Array Operations

The program implements array operations like insertion and deletion. It first takes input from the user to initialize an array with max size 100. It then prints the initial array. The program then takes input for a position and value to insert an element into the array. It prints the updated array after insertion. Similarly, it takes input for a position to delete an element and prints the final array after deletion.

Uploaded by

bhojraj singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Program 01: Write A Program To Implement For The Array Operations

The program implements array operations like insertion and deletion. It first takes input from the user to initialize an array with max size 100. It then prints the initial array. The program then takes input for a position and value to insert an element into the array. It prints the updated array after insertion. Similarly, it takes input for a position to delete an element and prints the final array after deletion.

Uploaded by

bhojraj singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Program 01

Write a program to implement for the Array operations.


Input Output
#include <stdio.h> Enter the number of elements in the array (max 100): 5
Enter the elements of the array:
#define MAX_SIZE 100 arr[0] = 1
int main() { arr[1] = 2
int arr[MAX_SIZE]; arr[2] = 3
int n, i, pos, value; arr[3] = 4
arr[4] = 5
printf("Enter the number of elements in the array (max %d): ", The array is: 1 2 3 4 5
MAX_SIZE); Enter the position where you want to insert an element (0-5): 3
scanf("%d", &n); Enter the value of the element to insert: 5
Element inserted successfully.
The array is now: 1 2 3 5 4 5
printf("Enter the elements of the array:\n"); Enter the position where you want to delete an element (0-5): 2
for (i = 0; i < n; i++) { Element deleted successfully.
printf("arr[%d] = ", i); The array is now: 1 2 5 4 5
scanf("%d", &arr[i]);
}

printf("The array is: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

printf("Enter the position where you want to insert an element (0-


%d): ", n);
scanf("%d", &pos);
printf("Enter the value of the element to insert: ");
scanf("%d", &value);

if (pos < 0 || pos > n) {


printf("Invalid position!\n");
} else {
for (i = n - 1; i >= pos; i--) {
arr[i+1] = arr[i];
}
arr[pos] = value;
n++;
printf("Element inserted successfully.\n");
}

printf("The array is now: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

printf("Enter the position where you want to delete an element (0-


%d): ", n-1);
scanf("%d", &pos);

if (pos < 0 || pos >= n) {


printf("Invalid position!\n");
} else {
for (i = pos; i < n-1; i++) {
arr[i] = arr[i+1];
}
n--;
printf("Element deleted successfully.\n");
}

printf("The array is now: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

You might also like