Quick question on the analysis of this short code snippet.

This is a discussion on Quick question on the analysis of this short code snippet. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main (void) { int i, ar [100]; printf ("\n\n\n"); for (i = 0;i < 100;i = ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    12

    Quick question on the analysis of this short code snippet.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int i, ar [100];
        
        printf ("\n\n\n");
        
        for (i = 0;i < 100;i = i + 1)
        {
            ar[i] = 1;
            ar[11] = -5;
            ar[12] = ar[12] + 1;
            ar[13] = ar[0] + ar[11] + 4;
        }
        for (i = 10; i <= 14; i = i + 1)
        {
            printf ("ar[%2d] = %4d\n", i, ar[i]);
        }
        return 0;
    }
    Hi. I am trying to understand this output. It outputs mostly as I would expect, except for the 89. I was expecting a 2 there. And, I must admit, I have no idea how this 89 came about. Any tips?

    1
    -5
    89
    0
    1

    I get all but the 89. I was expecting a 2 there. What am I missing?

    Thanks your your help.

    Have a nice weekend,

    Jalisco

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    433
    When i = 12,

    Code:
            ar[12] = 1;
            ar[11] = -5;
            ar[12] = ar[12] + 1;
            ar[13] = ar[0] + ar[11] + 4;
    Because you set ar[i] each time round the loop. So when you finish iteration with i=13, ar[12] is 3.... so on and so forth until i = 99 and ar[12] is 89.

    Why would you expect a 2? You increment it every time round the loop. I expected 100 at first glance.

    Also - the first 12 increments of ar[12] from i=0..11 will read and write garbage as ar is initialised.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,360
    you should initialise all of your array to start with
    Last edited by rogster001; 06-23-2012 at 06:50 AM.
    Thought for the day:
    You got big stones calling out GCC! (anduril462)
    FLTK:The most fun you can have with your clothes on.
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Thanks for the help.

    Still not getting it completely. =(

    But, after some minor playing around, it looks like ar12 goes into the iteration, and then +1 is added to i all the way to 99, with 2 getting the bonus + 2 per previous assignment.

    Is that what is happening? thanks

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    433
    Quote Originally Posted by jalisco View Post
    But, after some minor playing around, it looks like ar12 goes into the iteration, and then +1 is added to i all the way to 99,
    Yes.
    Quote Originally Posted by jalisco View Post
    with 2 getting the bonus + 2 per previous assignment.
    Bonus +2? I'm not sure I follow.
    This line
    Code:
        for (i = 0;i < 100;i = i + 1)
        {
            ar[i] = 1; // here
    Sets every element of ar to 1, one at a time (except the ones you've added special code for)
    ar[0] = 1, ar[1] = 1, ar[2] = 1...... ar[99] = 1.

    You increment ar[12] every time through the loop, but then you overwrite the value with 1 when i = 12.

    Quote Originally Posted by jalisco View Post
    Is that what is happening? thanks
    I think I'm making it sound more complicated than it is. Try this:
    Code:
    for (i = 0;i < 100;i = i + 1)
        {
            ar[i] = 1;
            ar[11] = -5;
            ar[12] = ar[12] + 1;
            ar[13] = ar[0] + ar[11] + 4;
            printf("i: %d   ar[12]: %d\n", i, ar[12]); // <-- print ar[12] for each i.
        }
    It looks like:
    Code:
    These are because the array is uninitialised.
    i: 0   ar[12]: -858993459
    i: 1   ar[12]: -858993458
    i: 2   ar[12]: -858993457
    i: 3   ar[12]: -858993456
    i: 4   ar[12]: -858993455
    i: 5   ar[12]: -858993454
    i: 6   ar[12]: -858993453
    i: 7   ar[12]: -858993452
    i: 8   ar[12]: -858993451
    i: 9   ar[12]: -858993450
    i: 10   ar[12]: -858993449
    i: 11   ar[12]: -858993448
    ar[12] is set to 1 then incremented.
    i: 12   ar[12]: 2
    then incremented from now on
    i: 13   ar[12]: 3
    i: 14   ar[12]: 4
    i: 15   ar[12]: 5
    i: 16   ar[12]: 6
    i: 17   ar[12]: 7
    i: 18   ar[12]: 8
    i: 19   ar[12]: 9
    i: 20   ar[12]: 10
    ....... up to 99

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,360
    It is a bad idea to allocate your working values to your array from the get go in the working function, having only had an unititilaised segment of memory beforehand, you should secure that memory with a known value of 0 or somesuch before you start using it. - it will make you very happy in the longer term.
    Last edited by rogster001; 06-24-2012 at 03:06 PM.
    Thought for the day:
    You got big stones calling out GCC! (anduril462)
    FLTK:The most fun you can have with your clothes on.
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird double/int question (very short code)
    By Minty in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2011, 01:58 PM
  2. what will be the output of this code snippet
    By devilofwar in forum C Programming
    Replies: 12
    Last Post: 12-13-2010, 04:41 PM
  3. Explanation of code snippet
    By Jelte in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2010, 05:59 AM
  4. C code snippet which I need help understanding
    By c_me in forum C Programming
    Replies: 3
    Last Post: 07-28-2006, 06:06 AM
  5. Replies: 1
    Last Post: 01-23-2002, 02:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21