-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_operations.c
104 lines (100 loc) · 1.89 KB
/
array_operations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//This is a c program demonstrating creation and operations on array
#include<stdio.h>
#define SIZE 40
int arr[SIZE];
int n;
// create an array of size n.
void create()
{
printf("Enter the size of Array: \n");
scanf("%d",&n);
printf("Enter Data into Array one by one: \n");
for(int i=0;i<=(n-1);i++)
{
scanf("%d",&arr[i]);
}
}
// function to display array
void display()
{
printf("The Array is: \n");
for (int i = 0; i <=n-1; i++)
{
printf("%d ",arr[i]);
}
}
// insert element at end
void l_insert()
{
printf("Enter Data to be inserted: \n");
int data;
scanf("%d",&data);
arr[n]=data;
n++;
}
// insert element at start
void f_insert()
{
printf("Enter Data you want to insert: \n");
int data;
scanf("%d",&data);
int i;
for ( i = n;i>= 1; i--)
{
arr[i]=arr[i-1];
}
arr[0]=data;
n++;
}
//insert element at users choice loaction
void i_insert()
{
printf("Enter Data you want to insert: \n");
int data;
scanf("%d",&data);
printf("Enter the index where you want to insert: \n");
int index;
scanf("%d",&index);
int i;
for ( i = n;i>= index; i--)
{
arr[i]=arr[i-1];
}
arr[i]=data;
n++;
}
//function to delete an element from the array
void delete()
{
printf("\nEnter loc of element to be deleted ");
int loc;
scanf("%d",&loc);
int i;
for (i = loc-1;i<=n;i++)
{
arr[i]=arr[i+1];
}
n--;
}
// function to search an element in an array using linear search
void l_search()
{
printf("Enter element to be searched \n");
int item;
scanf("%d",&item);
for (int i = 0; i < n-1; i++)
{
if(arr[i]==item)
printf("Element found at location %d \n",i+1);
}
}
void main()
{
create();
l_search();
display();
f_insert();
l_insert();
i_insert();
delete();
}