Thread: Looping problem

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Only increment your loop counter on a valid number.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote 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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So where exactly in the above code, does b have anything to do with your loop counter?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote 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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One more time...
    Code:
    for( i = 0; i <= N-1; i++)
    Where again is b controlling your loop?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote 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. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    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. #9
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote 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++;
              }
          }
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM