In the following c code, the num_elements is gobal.
But if i make it as local and return , it gives strange value just before return it becomes '0' ...can anybody explain why ?
int num_elements =0;
//generates a psuedo-random integer between min and max
int randint(int min, int max)
{
if (min>max)
{
return max+int((min-max+1)*rand()/(RAND_MAX+1.0));
}
else
{
return min+int((max-min+1)*rand()/(RAND_MAX+1.0));
}
}
unsigned int sample(unsigned int m, unsigned int n, unsigned int *set)
{
unsigned int t, i;
unsigned int j,l;
if (m==0)
return 0;
else
{
l=sample(m-1, n-1, set);
t = randint(1,n);
if(num_elements !=0)
{
for (i=0; i <num_elements; i++)
{
if ( t == set[i])
{
/* add t to s */
set[num_elements] = t;
}
else
{
/* add to n to s; */
set[num_elements] = n;
break;
}
}
}
else
{
set[num_elements] = t;
}
}
j= num_elements++;
printf("%d", num_elements);
return j;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int a = 2, b=4;
unsigned int set[10];
num_elements=0;
sample(a, b, set);
printf("%d", num_elements);
Sleep(10000);
return 0;
}