Write a recursive function that, given an integer X, determines the position of the closest value of X in an array.
Prototype : int closest(int tab[], int N, int X, int position)
Write a main function to test your program.


Difficulty level
This exercise is mostly suitable for students
int closest(int tab[], int N, int X, int position)
{
	if(N<=0)
		return position;
	if( (abs(tab[N]-X)<=abs(position-X)) && N>0  )
		return closest(tab,N-1,X,tab[N]);
	return closest(tab,N-1,X,position);
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Right rotate an array