# 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 &lt;stdio.h&gt; int main (void) { int i, ar [100]; printf (&quot;\n\n\n&quot;); for (i = 0;i &lt; 100;i = ...

1. ## 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?

Have a nice weekend,

Jalisco

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

4. 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. Originally Posted by jalisco
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.
Originally Posted by jalisco
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.

Originally Posted by jalisco
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. 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.