# 7-1 find the specified element in the array

2022-04-29 14:30:05

7-1 Find the specified element in the array

Enter a positive integer repeat (0<repeat<10), do repeat The following operations are performed ：

Enter a positive integer n (1<n<=10), Then input n An integer is stored in the array a in , Enter another integer x, In the array a Search for x, If found, the minimum subscript of the corresponding element is output , Otherwise output "Not found".

Requires that functions be defined and called search(list, n, x), Its function is in the array list Search for elements x, If found, the minimum subscript of the corresponding element is returned , Otherwise return to -1, Function parameter list The type of is an integer pointer , Shape parameter n and x The type is int, The type of function is int.

Output format statement ：printf("index = %d\n", );

I / O example ： In parentheses is the description , No input / output required

### sample input :

``````2              (repeat=2)
3              (n=3)
1 2 -6
2              (x=2)
5              (n=5)
1 2 2 5 4
0              (x=0)
``````

### sample output :

``````index = 1

``````#include <stdio.h>
#define N 10
int search(int *list, int n, int x);
int main()
{
int repeat;
int n,x;
int a[N];
scanf("%d",&repeat);
while(repeat>0)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);

scanf("%d",&x);
int m=search(a,n,x);
if(m==-1)
else
printf("index = %d\n",m);
repeat--;
}
return 0;
}
int search(int *list, int n, int x)
{
int i;
for(i=0;i<n;i++)
{
if(list[i]==x)
{
return i;
break;
}
}
return -1;
}
``````