Author: Gour Ch. Saha
Contact for any query:: gour_ch_saha@yahoo.co.in

#include<stdio.h>
#define TRUE 1
#define FALSE 0
float find_median(int *a, int n1 , int *b , int n2);
void main()
{


	int a[10]={3,4,5,6,7,8,9};
	int b[10]={1,2};
	float median;
	median=find_median(a,4,b,1);
	printf("The median is %f\n",median);
}

float find_median(int *a, int n1 , int *b , int n2)
{
	int n , even_flg = FALSE;
	int acount , bcount , ccount;
	float median;
	int a_overflow=FALSE;
	int b_overflow=FALSE;
	acount=0;
	bcount=0;
	n=(n1+n2)/2;
	if((n1+n2)%2==0)
	{
		even_flg=TRUE;
	}

	for(ccount=0;ccount<n && acount<n1 && bcount<n2 ; ccount++)
	{
		if(a[acount]<b[bcount])
		{
			acount++;
		}
		else
		{
			bcount++;
		}
	}

	while(ccount<n && acount<n1)
	{
		ccount++;
		acount++;
	}

	while(ccount<n && bcount<n2)
	{
		ccount++;
		bcount++;
	}

	if(acount>=n1)
	{
		acount--;
		a_overflow=TRUE;
	}
	if(bcount>=n2)
	{
		bcount--;
		b_overflow=TRUE;
	}



	if(even_flg == TRUE)
	{
		if( (acount+1<n1 && a[acount]<b[bcount])||
                    (b_overflow==TRUE))
		{
			printf("First if\n");
			median = (float)(a[acount]+a[acount-1])/2;
		}
		else if( (bcount+1 <n2 && b[bcount]<a[acount])||
                         (a_overflow==TRUE) )
		{
			printf("Second if\n");
			median = (float)(b[bcount]+b[bcount-1])/2;
		}
		else if(a_overflow==TRUE)
		{
			printf("Third if\n");
			median = (float)(a[acount]+b[bcount])/2;
		}
	}
	else if(b_overflow==TRUE)
	{
		median=(float)a[acount];
	}
	else if(a_overflow==TRUE)
	{
		median=(float)b[bcount];
	}
	else if(a[acount]<b[bcount])
	{
		median=(float)a[acount];
	}
	else
	{
		median=(float)b[bcount];
	}

	return median;
}

Free Web Hosting