Thread: Increment/Decrement operator troubles

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8

    Increment/Decrement operator troubles

    I have this bubble sort code that is just supposed to sort elements of an array in ascending order:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i = 0, didSwap = 0, temp = 0, inner = 0, outer = 0;
        char buffer[100] = {0};
        int firstArray[10] = {0};
    
        for (i = 0; i < 10; ++i)
        {
            printf("firstArray[%d]: ", i);
            fgets(buffer, sizeof(buffer), stdin);
            sscanf(buffer, "%d", &firstArray[i]);
        }
    
        for (outer = 0; outer < 9; outer++)
        {
            didSwap = 0;
    
            for (inner = outer; inner < 10; inner++)
            {
                if (firstArray[inner] < firstArray[outer])
                {
                    temp = firstArray[inner];
                    firstArray[inner] = firstArray[outer];
                    firstArray[outer] = temp;
                    didSwap = 1;
                }
            }
    
            if (didSwap == 0)
                break;
        }
    
        for (i = 0; i < 10; ++i)
            printf("\nfirstArray[%d] = %d", i, firstArray[i]);
    
        return (0);
    }
    The sorting works fine when the increment operators inner++ and outer++ are in postfix form. However, when I change it to prefix form ++inner and ++outer it no longer sorts. I always thought that it didn't matter which form you used in the third part of a for statement.

    Can someone enlighten me on to why this happens? Thanks in advance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you mean only changing this:
    Code:
        for (outer = 0; outer < 9; outer++)
        {
            /* ... */
            for (inner = outer; inner < 10; inner++)
    to this
    Code:
        for (outer = 0; outer < 9; ++outer)
        {
            /* ... */
            for (inner = outer; inner < 10; ++inner)
    I notice no difference.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Increment and decrement operators when taken as a single statement, behave the same regardless of prefix or postfix. The last segment, as Dave mentioned, of your for loop is considered a single statement as you are currently using it, and as such, the result is the same.
    Code:
    i++;
    ++i;
    The above two lines have the same result. The difference you'll see is when you do things like:
    Code:
    x = a[ ++i ];
    x = a[ i++ ];
    fun( i++ );
    fun( ++i );
    Things like that.


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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    Dave_Sinkula, yes, I did mean that. For some reason when I compile it using prefix form it refuses to sort.

    Quzah, I thought that was how it worked, but something seems to be wrong in my case.

    Maybe my compiler is messing something up? I'm using Code::Blocks with MingW. I don't know what else could be the problem.

    Thanks for the replies.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by LineOFire
    Maybe my compiler is messing something up? I'm using Code::Blocks with MingW. I don't know what else could be the problem.
    When you're new, save that until the very last. (That also happens to be what I used to check both forms.)

    Perhaps post the result(s) of your attempt(s)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could post your prefix code. It's rare to have a compiler bug of this nature.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    Never mind this post. I don't know what happened but it is working like it should now, even using the prefix form. I probably forgot to save before recompiling or something.

    Glad to realize it was human stupidity and not compiler error because Code::Blocks certainly is excellent.

    Thanks for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interlocked increment/decrement
    By Elysia in forum C++ Programming
    Replies: 17
    Last Post: 04-13-2009, 01:46 PM
  2. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM
  3. Increment/Decrement for loop?
    By --]bthNzA in forum C Programming
    Replies: 11
    Last Post: 12-29-2001, 01:10 AM