# Thread: Looping problem

1. ## Looping problem

So at the moment I'm basically running a test with 2 parameters, amount of numbers randomly generated (N) and the maximum value of which the numbers must not exceed (Y).

Code:
```    for(i = 0; i <= N-1; i++)
{
double u  =  gsl_ran_exponential (r, tao);
a[i] = u;
b = whichbin(bin,a[i],M,Y,i);
if (b != -1)
{
counter[b]++;
}
printf("%f\n", a[i]);
}```
and the whichbin function include

Code:
```  if (a[i] > Y)
{
printf("Invalid Number:");
b = -1;
}```
If i input 10 and 10 at command prompt, it will translate to 10 numbers with the maximum value of 10, otherwise it will be considered as an outlier.

I was wondering, if 1 out of the 10 numbers is an outlier, (i.e output: Invalid Number: 11.243423) , I am left with 9 numbers instead of 10. How can I generate another number when I encounter an outlier? I believe this has something to do with a while statement, something like
Code:
`while (counter[b] < N)`
but I'm not sure where to place it.

2. Only increment your loop counter on a valid number.

Quzah.

3. Originally Posted by quzah
Only increment your loop counter on a valid number.

Quzah.
Right, I realize that...I'm just not sure exactly how to go about that.

Code:
```if (b != -1)
{
counter[b]++;
}```
Takes care of that problem, right?

So I first check if the number is valid or not:
if it is, then process it like normal.
if it is not, then generate another one until it is valid, then process it like normal.

That means I need to generate another value, and then check it again through the same code, and then continue to process it like normal.

Tried several things but none worked out...

4. So where exactly in the above code, does b have anything to do with your loop counter?

Quzah.

5. Originally Posted by quzah
So where exactly in the above code, does b have anything to do with your loop counter?

Quzah.
my whichbin functions returns a value of b... if the b is not -1, then the loop counter adds one.

i want to know if the b is = -1, how can i get the code to generate another number?

6. One more time...
Code:
`for( i = 0; i <= N-1; i++)`
Where again is b controlling your loop?

Quzah.

7. Originally Posted by quzah
One more time...
Code:
`for( i = 0; i <= N-1; i++)`
Where again is b controlling your loop?

Quzah.
I guess it is not.

What do I do to allow b to control my loop? I generate a b-value for every i I process.

8. Remove the increment in the for loop, and put it somewhere in the middle so that you increment only on valid numbers.

Perhaps you need the following:
Code:
``` for(i = 0; i <= N-1; )
{
double u  =  gsl_ran_exponential (r, tao);
a[i] = u;
b = whichbin(bin,a[i],M,Y,i);
if (b != -1)
{
i++;
}
printf("%f\n", a[i]);
}```

9. Originally Posted by noodles
Remove the increment in the for loop, and put it somewhere in the middle so that you increment only on valid numbers.

Perhaps you need the following:
Code:
``` for(i = 0; i <= N-1; )
{
double u  =  gsl_ran_exponential (r, tao);
a[i] = u;
b = whichbin(bin,a[i],M,Y,i);
if (b != -1)
{
i++;
}
printf("%f\n", a[i]);
}```
That will print an uninitialised value from array a and it isn't bumping the array of counters.

Try:
Code:
```      i = 0;
while(i <= N-1)
{
double u  =  gsl_ran_exponential (r, tao);
a[i] = u;
b = whichbin(bin,a[i],M,Y,i);
if (a[i] > Y)
{
printf("Invalid Number: %f\n", a[i]);
}
else
{
printf("%f\n", a[i]);
counter[b]++;
i++;
}
}```

Popular pages Recent additions