Thread: Another question from the land of the newbies.

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    3

    Another question from the land of the newbies.

    Hello, guys! Last time you've been of immense help to me (pointing out a most stupid mistake indeed), so this time I hope you will also be kind enough to explain something to a newbie.
    Here's is the deal. I've actually reached the "sorting chapter" of my textbook and, predictably enough, tripped over the "Bubble array". Anyhow, this book (Absolute beginner's guide to C) provides the following code for sorting the array of random numbers (previously generated):
    Code:
    for(outer = 0; outer < 9; outer ++)
        {
            did_swap = 0;
            for(inner = outer; inner < 10; inner ++)
            {
                if(nums[inner] < nums[outer])
                {
                    temp = nums[inner];
                    nums[inner] = nums [outer];
                    nums[outer] = temp;
                    did_swap = 1;
                }
            }
            if(did_swap == 0)
            {
                break;
            }
        }
    What I can't wrap my head around is the following: we set the OUTER counter to 0 (so, if my array has nine numbers (5, 1, 4, 8, 7, 2, 3, 9, 10, 14), the number I'm dealing with is 5).
    Then we state that INNER = OUTER, which effectively makes it equal to 0 (so, again, in my array the number we'll be dealing with will be 5).
    How can we compare nums[inner] with nums[outer], if both of them are 0, which makes them essentially one and the same number???? I could understand this code if we had inner = outer + 1...But I have no idea what to make of inner = outer...
    The code works, though, which does not contribute to my self-esteem at all!
    Thank you in advance for your help and patience!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Get ready for a self-esteem boost: I totally agree with you.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    17
    Of course we can compare two similar numbers, you're absolutely correct when nums[inner]=nums[outer]=nums[0], basically it's comparing the same number so the if would return a false in this case. Try to look what happens when the iteration proceeds more and it'll start to clear up.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    You can take a look at this link that I found. Two versions of Bubble sort C codes are mentioned there. By the way you may need to include <stdbool.h> for the second C code so that you could compile it.

    Specially note the bool type and usage of true rather than 1, and false rather than 0. see this link too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbies question
    By yiannistamv in forum C Programming
    Replies: 1
    Last Post: 01-20-2015, 07:18 AM
  2. Question. 4 Newbies 4all time
    By lifeisendless4m in forum C Programming
    Replies: 14
    Last Post: 10-07-2004, 12:19 PM
  3. This land is my land!
    By 7smurfs in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-22-2004, 07:16 AM
  4. Question (i Speak For All Newbies)
    By Leeman_s in forum Game Programming
    Replies: 8
    Last Post: 10-31-2001, 10:02 PM
  5. Newbies question about the integer type
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 08:09 PM

Tags for this Thread