binary_boy 0 Newbie Poster

this is an assignmentin which i have to create a programme which takes in two input arrays which are already sorted and outputs a combined sorted array. this has to be done by multi threading. we have to create a new thread to take the input of the arrays and one thread each for finding the position for every element in the output array. i have written this code, but i cant figure out why its not working...can someone help..

#include<stdio.h>
#include<malloc.h>
#include<pthread.h>
#include<stdlib.h>

int n,m,i,sum;
int *arr1,*arr2,*result;

void *arrinp(void *param3);  //prg to take array inputs
void *solve1(void *param1);  //prg to find position of elements of array1 in result array	
void *solve2(void *param2);  //prg to find position of elements of array2 in result array 

int main()
{
	pthread_t tid1;
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_create(&tid1,NULL,arrinp,(void *)NULL);
	pthread_join(tid1,NULL);
	
	pthread_t tid[n+m];
	for(i=0;i<n;i++)
	{
		pthread_create(&tid[i],NULL,solve1,(void *)arr1[i]);
		
	}
	for(i=0;i<m;i++)
	{
		pthread_create(&tid[i],NULL,solve2,(void *)arr2[i]);
	}
	for(i=0;i<(n+m);i++)
	{	
		pthread_join(tid[i],NULL);
	}
	for(i=0;i<(n+m);i++)
	{
		printf("%d",result[i]);
	}	
	pthread_exit(NULL);
			
}

void *arrinp(void *param3)
{
	printf("Enter the size of array1:\n");
	scanf("%d",&n);
	printf("Enter the size of array2:\n");
	scanf("%d",&m);
	arr1=calloc(n,sizeof(int));	
	arr2=calloc(m,sizeof(int));
	printf("Enter elements of array1(< than %d):",n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr1[i]);
	}
	printf("Enter elements of array2(< than %d):",m);
	for(i=0;i<m;i++)
	{
		scanf("%d",&arr2[i]);
	}
	result=calloc((n+m),sizeof(int));
	arr1[0]=1;
	arr2[1]=2;
	pthread_exit(0);
}			
	
void *solve1(void *param1)
{
	int a,k;
	printf("bc");	
	if(arr1[n]>arr2[m])
	{
		result[n+m]=arr1[n];
	}
	else
	{
		result[n+m]=arr2[m];	
	}		
	k=atoi(param1);
	while(a<=m)
	{
		if(k<arr2[a])	
		{
			result[i+a]=k;
			break;
		}
		else
		{
			a++;
		}
	}
	for(i=0;i<(n+m);i++)
	{
		printf("%d\t",result[i]);
	}	
	pthread_exit(0);
}				

void *solve2(void *param2)
{
	int a,k;
	k=atoi(param2);
	while(a<=n)
	{
		if(k<arr1[a])	
		{
			result[i+a]=k;
			break;
		}
		else
		{
			a++;
		}
	}
	for(i=0;i<(n+m);i++)
	{
		printf("%d\t",result[i]);
	}	
	pthread_exit(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.