Thread: Question concerning for loops

  1. #16
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    aside from that, you're still wrong

    It's important that you stop being wrong
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    No, "i" is an element of the array
    So far you're winning "most ridiculous statement of the week" (granted the week is still young). i is most definitely not a member of the array.

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs down

    Quote Originally Posted by tabstop View Post
    So far you're winning "most ridiculous statement of the week" (granted the week is still young). i is most definitely not a member of the array.
    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

    Code:
    array[ i ] = 10 - i;
    be anything but declaring "i" as an element of the array "array", and defining its value?

  4. #19
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    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

    Code:
    array[ i ] = 10 - i;
    be anything but declaring "i" as an element of the array "array", and defining its value?
    let me show you
    Code:
    int main()
    {
      int i;
    
      for ( i = 0; i < 10; i++) {
        printf("array[%d] = %d\n", i, 10 - i);
      }
      
      return 0;
    }
    ^run that code
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #20
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs down

    Quote Originally Posted by ಠ_ಠ View Post
    let me show you
    Code:
    int main()
    {
      int i;
    
      for ( i = 0; i < 10; i++) {
        printf("array[%d] = %d\n", i, 10 - i);
      }
      
      return 0;
    }
    ^run that code
    Sorry, changing the code isn't going to help your point any...

  6. #21
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    Sorry, changing the code isn't going to help your point any...
    I'm showing you what it's actually doing
    (HIBT?)
    Last edited by ಠ_ಠ; 05-26-2009 at 03:20 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #22
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by sean View Post
    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
    Code:
    for ( i = 0; i < 10; ++i )
        {
            array[ i ] = 10 - i;
        }
    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.
    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

  9. #24
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    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
    Code:
    for ( i = 0; i < 10; ++i )
        {
            array[ i ] = 10 - i;
        }
    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 9 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.
    10 times
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by ಠ_ಠ View Post
    10 times
    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
    Code:
    int array[11];
    and not a
    Code:
    int array[10];
    or a
    Code:
    for ( i = 0; i <= 8; ++i; ) 
        {
               array[ i ] = 10 - i;
        }
    and not a
    Code:
    for ( i = 0; i < 10; ++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.

    Why is that?
    Last edited by Programmer_P; 05-26-2009 at 05:03 PM.

  11. #26
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    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
    Code:
    int array[11];
    and not a
    Code:
    int array[10];
    or a
    Code:
    for ( i = 0; i <= 9; ++i; ) 
        {
               array[ i ] = 10 - i;
        }
    and not a
    Code:
    for ( i = 0; i < 10; ++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.

    Why is that?
    null characters are for character arrays
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  12. #27
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for ( i = 0; i == 9; ++i; ) 
        {
               array[ i ] = 10 - i;
        }
    That variant will run exactly ZERO times, since i is not 9 the first time round, so it never enters the loop.

    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.

  14. #29
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Programmer_P View Post
    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
    Code:
    for ( i = 0; i < 10; ++i )
        {
            array[ i ] = 10 - i;
        }
    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.
    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
    did you happen to run the code I told you to run?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Cool

    Quote Originally Posted by matsp View Post
    Code:
    for ( i = 0; i == 9; ++i; ) 
        {
               array[ i ] = 10 - i;
        }
    That variant will run exactly ZERO times, since i is not 9 the first time round, so it never enters the loop.

    Also, it's only strings that need to have a NUL character at the end.

    Your second loop looks great.

    --
    Mats
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM