-
Notifications
You must be signed in to change notification settings - Fork 0
/
q6.cpp
38 lines (29 loc) · 946 Bytes
/
q6.cpp
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
#include <stdio.h>
int findElementLocation(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index where the element is found
}
}
return -1; // Return -1 if element is not found
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int targetElement;
printf("Enter the element to search: ");
scanf("%d", &targetElement);
int location = findElementLocation(arr, size, targetElement);
if (location != -1) {
printf("Element %d found at index %d.\n", targetElement, location);
} else {
printf("Element %d not found in the array.\n", targetElement);
}
return 0;
}