Write a function to search an item named searchKey in an array of 20 elements. Assumed the array has all integer value and also the search item is an integer.
Write a function to search an item named searchKey in an array of 20 elements. Assumed the array has all integer value and also the search item is an integer.
Step 1:-
Program Approach:-
- Use one header file
- Declare user-defined function search(int *Ar, int searchkey) that takes the address of the array as a first parameter and element in the second parameter.
- Declare 3 variable in the main() function
- Declare one variable in the search() function
- Declare one Array of type integer
Step 2:-
Explanation:-
1.The User-defined function is defined as a function that is designed by the user based on his requirement.
2.Pointer variable is a special variable that takes the address of another variable for example int *Ar
3.Array is defined as a similar type of data for example int A[2] array name is A and its data type is an integer. It takes two-element inside the array. The first element is store at location A[0]
4.== is used to compare the two values
Program:-
#include <stdio.h>
void search(int *Ar,int searchkey); //function declaration
int main()
{
int Arr[20];
int i,term;
printf("Enter the array element");
for(i=0;i<20;i++)
{
scanf("%d",&Arr[i]);
}
printf("Enter the element that you want to search");
scanf("%d",&term);
search(&Arr,term); //call the function
return 0;
}
void search(int *Ar,int searchkey) //function definition
{
int c;
for(c = 0; c <20; c++)
{
if(Ar[c] == searchkey)
{
printf("Element found in the array");
break; // terminate the loop
}
}
}
Step by step
Solved in 3 steps with 1 images