- #1
iquicck
- 3
- 0
- Homework Statement
- I write a Linear Search code but ı redesign for ask to user version
- Relevant Equations
- Linear Search
I write a Linear Search code,then ı decided ask to user "r" and "int arr".I mean,User decide their r and arr numbers.I tried scanf("%d",r); command but doesn't work
This code my first code before the decide Ask user :
This code my first code before the decide Ask user :
C:
#include <stdio.h>
int search(int arr[], int n, int r)
{
int i;
for (i = 0; i < n; i++)
if (arr[ i] == r)
return i;
return -1;
}int main(void)
{
int arr[] = { 7, 9, 12, 28, 17,32 };
int r = 17;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, r);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Last edited by a moderator: