Skip to content

Commit c487dfe

Browse files
authored
Update Array.md
1 parent 5b56f34 commit c487dfe

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Array.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,31 @@ You can perform a search for an array element based on its value or its index.
172172
There are two searching techniques linear and binary. For simplicity, I am implementing linear search algorithm to search element in array.
173173

174174
```c
175+
#include<stdio.h>
176+
int main()
177+
{
178+
int array[5] = { 2, 6, 8, 3, 9};
179+
180+
// initial size of array
181+
int n = 5;
182+
int i = 0;
183+
184+
// element to be searched
185+
int x = 6;
186+
int index;
175187

188+
// iterate the array elements using loop if any element matches the key, store the index
189+
for (i = 0; i < n-1; i++)
190+
{
191+
if (array[i] == x)
192+
{
193+
index = i;
194+
break;
195+
}
196+
}
197+
198+
// print the index of element
199+
printf("Element %d is found at %d",x,index + 1);
200+
201+
}
176202
```

0 commit comments

Comments
 (0)