0% found this document useful (0 votes)
4 views5 pages

Linear Array Operations in C

Uploaded by

akshayaarb24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Linear Array Operations in C

Uploaded by

akshayaarb24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CODE:

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

int arr[MAX_SIZE];

int size = 0,i;

void insert(int element, int position) {

if (size >= MAX_SIZE) {

printf("Array is full. Cannot insert.\n");

return;

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

printf("Invalid position.\n");

return;

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

arr[i] = arr[i - 1];

arr[position] = element;

size++;

printf("Element inserted successfully\n");

void delete(int position) {

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

printf("Invalid position.\n");

return;

for (int i = position; i < size - 1; i++) {

arr[i] = arr[i + 1];

size--;
printf("Element deleted successfully\n");

void display() {

printf("Array elements: ");

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

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

printf("\n");

int main() {

printf("\t\tPROGRAM 1:LINEAR ARRAY OPERATIONS\n");

printf("\t\t*************************************\n");

int choice, element, position;

while (1) {

printf("1. Insert\n");

printf("2. Delete\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter element to insert: ");

scanf("%d", &element);

printf("Enter position to insert: ");

scanf("%d", &position);

insert(element, position);

break;

case 2:

printf("Enter position to delete: ");

scanf("%d", &position);
delete(position);

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice.\n");

return 0;

OUTPUT:

PROGRAM 1:LINEAR ARRAY OPERATIONS

*************************************

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter element to insert: 5

Enter position to insert: 1

Invalid position.

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter element to insert: 6


Enter position to insert: 0

Element inserted successfully

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter element to insert: 12

Enter position to insert: 1

Element inserted successfully

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter element to insert: 33

Enter position to insert: 2

Element inserted successfully

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 1

Enter element to insert: 46

Enter position to insert: 3

Element inserted successfully

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 3


Array elements: 6 12 33 46

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 2

Enter position to delete: 1

Element deleted successfully

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 3

Array elements: 6 33 46

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice: 4

You might also like