Thread: 'For loop' looping one too many times.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    'For loop' looping one too many times.

    The issue occurs in the following procedure. I have included the entire procedure, as the contained for loop calls the procedure.

    The first part of the procedure generates all sets of size 'size', of ints 1 to 9 (inclusive), but in ascending order (not necessarily 1, 2, 3..., could be 1, 7, 9). If you've played sudoku before, you might recognise this as part of a 'locked set' algorithm that.

    Once a set of size n is generated, the locked set algorithm uses it.

    The issue in more detail: the for loop has two test conditions, one for ensuring 'value' does not exceed 9, the second test condition harder to explain, not essential, but prevent unnecessary computation (same set being generated many times). For some reason, the for loop ends when value reaches 10 (shown when I print out contents of array).

    If I run procedure with size = 9, a set of 1 to 10 inc. is generated, so for loop is looping one too many times.

    Code:
    void setScan(int size, int value, int *arrayPtr, int count)  // Size is size of wanted number set.
    {
        int remainSize = size;  // Stops loop 1 generating same set as other recursions.
        int x, y;
        if(size > 0)
        {
            while((value <= 9) && (value <= (10 - size)))  // This loop generates all sets of size 'size', and for each set, looks for a locked set.
            {
                if(value == 10);
                    printf("Hmm\n");
                arrayPtr[count] = value;
                remainSize--;  // Size signifies how much of set left to create.
                value++;
                count++;
                setScan(remainSize, value, arrayPtr, count);
            }
        }
        else  // size == 0, count == size of set
        {
            printf("Current set is: ");
            for(y = 0; y <= count; y++)
                printf("%i, ", arrayPtr[y]);
            printf("\n");
            /*for(y = 0; y <= 8; y++)
            {
                for(x = 0; x <= 8; x++)
                {
                    //rowSetScan(x, y, arrayPtr, count);
                    //colSetScan(x, y, arrayPtr, count);
                    //boxSetScan(x, y, arrayPtr, count);
                }
            }*/
        }
    }
    Last edited by thealmightyone; 02-20-2009 at 06:23 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    are you aware that in the array of size n availeble indexes are
    0, 1, ..., n-1?

    so standard form of the loop is
    Code:
    for(i =0; i < n;i++)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    I am aware of the standard form. Initial value of loop is determined by 'value', passed in by procedure call. So, it is very possible for value to exceed 9, but loop should not begin/continue if value exceeds 9.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think what vart actually meant was that you normall use < rather than <= to determine the limit of the loop.

    And if there is NEITHER an initialization, nor a increment step in the loop, then I would rather use while than for. What I'm saying is that if two of the three units that make up a for-loop are empty, why is it a for-loop?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Although they are both good points, they don't address the problem. Still arises with a while loop which uses '<' over '<='.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thealmightyone View Post
    Although they are both good points, they don't address the problem. Still arises with a while loop which uses '<' over '<='.
    Sorry, but if you have modfied the code, can you please post the modified code. There may be other issues, but certainly using <= instead of < is a common reason for getting one too many, and your code shows that very pattern. Since your new code isn't identical to what is being posted, having the new code will help analyze the problem. If we start guessing what changes you have made, then we may well go wrong - not to mention that your new code may have CHANGED something else that is affecting the problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    I have simply replaced the for with a while, but kept the '<=' as a personal preference. Code in first post updated.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you actually seeing it loop too many times, or saying that it does because it prints "hmm"?

    Have another look at this:
    Code:
                if(value == 10);
                    printf("Hmm\n");
    If you don't spot anything wrong, look again. Keep looking until you spot something wrong.

    Edit: Or if you are using gcc, add -Wextra to your command line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  2. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  3. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM