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

Implementation of Array Operations Using Functions and Loops

This C program implements an array with operations for creation, insertion, deletion, searching, updating, and displaying elements. The main function uses a switch case to call the corresponding function based on the user's choice. Each function implements the operation on the array - create initializes it, insert adds an element, delete removes one, search finds a value, update modifies an element, and display prints out the array. This allows the user to interactively perform different actions on and view the contents of the array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Implementation of Array Operations Using Functions and Loops

This C program implements an array with operations for creation, insertion, deletion, searching, updating, and displaying elements. The main function uses a switch case to call the corresponding function based on the user's choice. Each function implements the operation on the array - create initializes it, insert adds an element, delete removes one, search finds a value, update modifies an element, and display prints out the array. This allows the user to interactively perform different actions on and view the contents of the array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
struct array{
int choice;
char g=’y’;
}arr;
void create();
void insert();
void deletion();
void search();
void update();
void display();
int a[100];
int element,i,loc,size,n,j,key,pos;
int main()
{
// clrscr();

printf("C Program to perform operations in an Array using switch case\n");


printf("1. Create an Array and Array Elements\n");
printf("2. Inserting an Element in an Array\n");
printf("3. Deleting an Element in an Array\n");
printf("4. Searching an Element in an Array\n");
printf("5. Updating an Element in an Array\n");
printf("6. Displaying the Elements in an Array\n");
do{
printf("\nSelect your choice : \n");
scanf("%d",&arr.choice);
switch(arr.choice)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
deletion();
break;
case 4:
search();
break;
case 5:
update();
break;
case 5:
display();
break;
default:
printf("Wrong choice, Please try again later");
}
}while(arr.g==’y’||arr.g==’n’);
return 0;
}
void create()
{
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
}
void insert()
{
printf("List before Insertion: ");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to insert\n");
scanf("%d",&element);
printf("Enter a position to insert an element %d\n",element);
scanf("%d",&loc);
loc--;
for(i=size-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=element;
printf("List after Insertion: ");
for(i=0;i<size+1;i++)
{
printf("%d ",a[i]);
}
}
void deletion()
{
printf("List before deletion\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to delete\n");
scanf("%d",&n);
for(i=0;i<size;i++)
{
if(a[i]==n)
{
for(j=i;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("List after deletion\n");
for(i=0;i<(size-1);i++)
{
printf("%d ",a[i]);
}
}
void search()
{
printf("List before searching\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to be searched\n");
scanf("%d",&key);
j=0;
while( j < size){
if( a[j] == key) {
break;
}

j = j + 1;
}
printf("\nElement %d is found at %d position", key, j+1);
}
void update()
{
printf("List before updation\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to be updated\n");
scanf("%d",&key);
printf("\nEnter an position to be updated\n");
scanf("%d",&pos);
a[pos-1] = key;
printf("\nArray elements after updation :\n");

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


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

You might also like