Untitled
#include<stdio.h> void SortedArray(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 0; j++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } } int found(int a[], int n, int target) { for (int i = 0; i < n; i++) { if (a[i] == target && a[i + 1] != target) { return i; } else if(a[i] == target && a[i + 1] == target) { return -1; } else if(target > a[i] && target < a[i + 1]) { return i + 1; } } } int main() { int n; int target; printf("Enter the index of array: \n"); scanf("%d", &n); printf("The array index is %d\n", n); int arr[n]; printf("enter the arrray: \n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("The array is: \n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); printf("Enter the Target element: "); scanf("%d", &target); SortedArray(arr, n); int result = found(arr, n, target); printf("The result is %d", result); }
Leave a Comment