aside from that, you're still wrong
It's important that you stop being wrong
aside from that, you're still wrong
It's important that you stop being wrong
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
That's BS. Instead of shooting off comments like that, how about presenting some facts, i.e. some proof that you're right, and I'm wrong. How can the statement
be anything but declaring "i" as an element of the array "array", and defining its value?Code:array[ i ] = 10 - i;
i is an index used to refer to an element in that array - it is not the element itself. It's a bit like saying that "president is a person". Yes, THE president is a person, but he could be president of anything. It's only when you use that title with a particular context that it refers to an actual entity.
Ok, that makes sense. (That's all I was asking: to prove me wrong with facts, not general statements saying I'm wrong, but not backing it up with facts)
So what the following code
basically does is goes through all the elements in the array, using a single variable ("i") as reference, and declares each of them as 10 minus the current value of "i" (which changes by 1 each loop). And so the loop body will execute 10 times, and not the 1 like I was originally thinking it would, based on thinking "i" was an actual element of the array, and not just a reference to the elements in the array.Code:for ( i = 0; i < 10; ++i ) { array[ i ] = 10 - i; }
So, after the loop is finished,
array[0] = 10
array[1] = 9
array[2] = 8
array[3] = 7
array[4] = 6
array[5] = 5
array[6] = 4
array[7] = 3
array[8] = 2
array[9] = 1
Last edited by Programmer_P; 05-26-2009 at 04:09 PM. Reason: adding something
Right. That was a typo. I meant 10 times, but was thinking of the number of the last element in the array (i.e. 9). But I know it begins at 0, and so yes, the loop body will be executed 10 times, and the 10th time the variable "i" updates, "i" will be set to 10, and the loop wont continue.
Only trouble with that logic is, if the array has 10 elements (0 through 9), and the last one is supposed to be a null character, then why does that code set array[9] (i.e. the tenth element of the array) to value 1? Shouldn't it only go up to element 8 of the array and stop there?
So it should actually be a
and not aCode:int array[11];
or aCode:int array[10];
and not aCode:for ( i = 0; i <= 8; ++i; ) { array[ i ] = 10 - i; }
The way they have the array and for loop in the tutorial has the last element (which is supposed to be a NULL character) in the array declared as a 1.Code:for ( i = 0; i < 10; ++i ) { array[ i ] = 10 - i; }
Why is that?
Last edited by Programmer_P; 05-26-2009 at 05:03 PM.
Not all arrays have to end in a null character. If we know that all we have is 10 elements, we can just never let ourselves refer to an index greater than 9. Null characters are used in strings because they're useful when working with arrays that don't have a predetermined length. There are many functions in C (particularly the string functions) that do work on a char array 'until the end', wherever that may be. That kind of function would use the null character. Other methods just require knowing an explicit length, and work until that point is reached.
That variant will run exactly ZERO times, since i is not 9 the first time round, so it never enters the loop.Code:for ( i = 0; i == 9; ++i; ) { array[ i ] = 10 - i; }
Also, it's only strings that need to have a NUL character at the end.
Your second loop looks great.
--
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.
Right. I realized that already, and corrected it too, to say <=9 (less than or equal to 9) rather than ==9 (equals 9).
Ok, so I've been told three times already, and that's enough. I now know (thanks to y'all) that there only needs to be a NULL character at the end of character arrays, not integer arrays, which is what the one here was.