C PROGRAM WITH NO OUTPUT

  c++

I have this code that is giving me the output
floating point exception ./thr_reduce 0 3
With the first argument being 0 and the second 3.

#include<stdio.h>
#include<semaphore.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
pthread_t* thread;
long range=0;
int j=0;
int n, m;
double* globalsum;
void* function(void *arguments) {
double result;
int id =(long)arguments ;
for(long i=(id*n/m)+1; i<=(id+1)*n/m; i++) {
result += sqrt(i);
}
globalsum[id]= result;
int k=m/2;
for(int i =0; i <m; i++)
{
if(id < k)
{
pthread_join(thread[((id+k)*1)], NULL);
globalsum[id] +=globalsum[((id+k)*1)];
k=k/2;
}
else
break;
}if(id==0)
for(int i=0;i<m;i++)
{
printf("globalsum[%d]= %lfn ", i, globalsum[i]);
}
}

int main(int argc, char *argv[])
{
if(argc<3)
{
printf("The required arguments are two, kindly add them both");
exit(0);
}
m=atoi(argv[1]);
n=atoi(argv[2]);
globalsum=(double*)malloc(m*sizeof(double));
thread = (pthread_t*) malloc(sizeof(pthread_t)*m);
range = n/m;
for(long i=m-1; i>=0; i--)
{
if(pthread_create(&thread[i],NULL,function,(void*)i))
printf("Error:cannot be processed");
}

free(thread);
pthread_exit(NULL);
}

Am a novice in C programming Please help.
I just need to be pointed in the right direction.
It gets stuck on execution.
No output if the first argument is not zero.

Thank you for your feedback.

Source: Windows Questions C++

LEAVE A COMMENT