#include <stdio.h>
int abs(int a)
{
if(a>0)
return a;
else
return -a;
}
int max(int a,int b)
{
if(a<b)
return b;
else
return a;
}
int closest(int* arr,int n,int x)
{ int min=arr[0];
int diff=abs(arr[0]-x);
for(int i=1;i<n;i++)
{
if(abs(x-arr[i])<diff)
{
min=arr[i];
diff=abs(x-arr[i]);
}
else if(abs(x-arr[i])==diff && min!=arr[i])
{
min=max(min,arr[i]);
}
}
return min;
}
int main()
{
int testCases;
//printf("enter the number of test cases \n");
scanf("%d",&testCases);
int arr[testCases][100];
for(int i=0;i<testCases;i++)
{
//printf("enter the number of elements in the array and the test element \n");
int n,k;
scanf("%d %d",&n,&k);
//printf("enter the array \n");
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
printf("%d\n",closest(arr[i],n,k));
}
}
why does this show segmentation fault?
Comments
Post a Comment