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

1.array Operations

The document describes a menu-driven C program that implements array operations like creating an array, displaying elements, inserting an element at a given position, and deleting an element at a given position. The program uses functions to implement each operation on an integer array. A main function contains a switch-case menu to call the appropriate function based on user's choice. Functions are defined to create the array by taking size and elements from user, display elements with headings, insert an element by shifting other elements and incrementing size, and delete an element by shifting elements and decrementing size.

Uploaded by

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

1.array Operations

The document describes a menu-driven C program that implements array operations like creating an array, displaying elements, inserting an element at a given position, and deleting an element at a given position. The program uses functions to implement each operation on an integer array. A main function contains a switch-case menu to call the appropriate function based on user's choice. Functions are defined to create the array by taking size and elements from user, display elements with headings, insert an element by shifting other elements and incrementing size, and delete an element by shifting elements and decrementing size.

Uploaded by

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

/*1Q.

Design, Develop and Implement a menu driven Program in C for the following
Array operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations. */

//Required Header files


#include<stdio.h>

// Global Declaration
int a[20];
int n,elem,i,pos;

// Function declaration or Prototype


void create();
void display();
void insert();
void delete();

// main function to drive to program


void main()
{
int choice;
while(1) // infinite loop
{
// menu options
printf("\n MENU \n");
printf("1.CREATE\n");
printf("2.DISPLAY\n");
printf("3.INSERT\n");
printf("4.DELETE\n");
printf("5.EXIT\n");

// read choice
printf("Enter your choice\n");
scanf("%d",&choice);

switch(choice)
{
case 1:create();
break;

case 2:display();
break;

case 3:insert();
break;

case 4:delete();
break;

case 5:return;

default:printf("\n Invalid choice\n");


} // end of switch
} // end of infinite while loop
} // end of main

// a. Creating an Array of N Integer Elements


void create()
{
printf("\nEnter the size of the array elements\n");
scanf("%d",&n);
printf("\nEnter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}

//b. Display of Array Elements with Suitable Headings


void display()
{
printf("\n The array elements are:\n");
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i, a[i]);
}

// c. Inserting an Element (ELEM) at a given valid Position (POS)


void insert()
{
printf("\nEnter the position for the new element\n");
scanf("%d",&pos);
// check for invalid position
if(pos>=n+1)
printf("Insertion not possible\n");
else
{
printf("\n Enter the element to be inserted:\t");
scanf("%d",&elem);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i]; // right shift elements before insertion
a[pos]=elem; // insert element
n++; // increase number of elements by 1
}
}

//d. Deleting an Element at a given valid Position(POS)


void delete()
{
printf("\n Enter the position of the element to be deleted\n");
scanf("%d",&pos);
if(pos>=n+1)
printf("Deletion not possible");
else
{
printf("\n The deleted element is %d",a[pos]); // display the deleting element
at the given position
for(i=pos;i<n-1;i++)
a[i]=a[i+1]; // left shift elements after deletion
n--; // reduce number of elements by 1
}
}

You might also like