# need help understanding basic arrays

• 03-15-2012
jpatrick
need help understanding basic arrays
Hey everyone..

So i'm adding arrays to the already long-enough list of C concepts that i don't understand. I looked at the tutorial on this site and i've googled for info on arrays; nothing i have read has helped me understand any better so i was hoping someone here could maybe explain what's happening in layman's terms.

To be clear, this is not homework, this is just an example practice question that i don't quite understand..

What is the output of the following program?
Code:

```Int main() { Int array[5] = {3, 6, 2, 7, 1} Int I;   For (i=0; i<4; i++)               Array[i] = array[i] + aray[i+1] For (i=0; i<5; i++)               Printf(“%d”, array [i]); Return 0; }```
now i put this into codeblocks and the output was '98981.' However, i don't understand how. I guess my real question is this: What's going on within the for loops?
• 03-15-2012
Subsonics
Quote:

Originally Posted by jpatrick
now i put this into codeblocks and the output was '98981.' However, i don't understand how. I guess my real question is this: What's going on within the for loops?

The last loop just prints the array. Given:
Code:

`int array[5] = {3, 6, 2, 7, 1};`
the last array adds the the value of i and i + 1 so the result you're seeing is 3+6 = 9, 6+2 = 8, 2+7 = 9, 7+1 = 8, 1 is the last value and is unaffected since the first loop only loops while i < 4. Don't know how to explain that differently, hope that made sense.
• 03-15-2012
TheBigH
You have three different spellings of array in here:

Code:

```For (i=0; i<4; i++)               Array[i] = array[i] + aray[i+1]```
• 03-15-2012
jpatrick
i do understand better now; I mean, i see how you got the output, at least. i guess i was confusing myself after looking at i=0 so my thought process was something like this: since i=0 then 0+0+1=0...
• 03-15-2012
jpatrick
i know bigH. i was rushing :) sorry lol. but rest assured i fixed it in codeblocks.. i copied and pasted this from a powerpoint.
• 03-15-2012
jpatrick
for more clarification.. here's another example that i can't figure out with the same logic because it is a different type of problem..

Code:

```int main() {               int a[] = {8, 1, 6, 3, 5, 7, 2, 4, 10, 9};               int i;                                 for (i=0; i<10; i++)                               a[i] = a[a[i]-1];                 for (i=0; i<10; i++)                               printf("%d ", a[i]);                                 return 0; }```
i'm not entirely sure what
Code:

`a[i] = a[a[i]-1];`
• 03-15-2012
TheBigH
Well, let's break it up into parts. First look at a[i]. When i=0, a[i]=8. So a[0]-1 = 7.

Now we can put that into the entire expression

Code:

```a[0] = a[a[0]-1] a[0] = a[7] a[0] = 4```
because 4 is the the value stored at a[7].
• 03-15-2012
jpatrick
i see. so then the next value would be a=1, 1-1=0, and 8 is at the 0 value but it has been replaced by 4? so the second value is 4? is that how it works?
• 03-15-2012
TheBigH
Bingo.
• 03-15-2012
jpatrick
well thanks for the help. i'm pretty sure i understand it now. i'm assuming things might get a little complicated with all the numbers changing so i'll just have to keep track of everything on paper.

i appreciate it