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!