I have the following code. the temp_array seems to have some garbage value at the end even though i try to randomly distubute certain numbers to it. I went through the loop several times but couldn't get whats wrong. I really really appreciate if sombody can spot the error. Thankx for loking into it
have a good day
Code:
void rand_gen(int need_num, int **array, int dim){
int n, m, *temp_array;
int i=0, b, g, new1 ;
int q,w,h;
temp_array = (int *)malloc(need_num * sizeof(int));
for(h=0;h<need_num;h++){
temp_array[h]=0;}
srand(time(NULL));
for (i=0;i<need_num;i++)
{
b=rand()%need_num+1; // random number with in need_num
//temp storing the rand # in a array temp
new1 = 1;
for(g=0;g<i;g++)
if(temp_array[g]==b){new1 = 0;}
if (new1 == 1){
temp_array[i]=b;}
else
i--;
}
//WHEN dim =5, need_num=24
printf("\ntemp arra 24 is %d\n\n\n", temp_array[24]);<<//why this prints Garbage ???????????