# Thread: Need help with this

1. ## Need help with this

Right, so I picked up this tutorial a couple days ago, it went pretty good, until I got to the array part of the turorial.

Code:

Code:
```billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;```
For thisa one I just don't understand what the bottom line is doing, this an example code.

Code:
```// arrays example
#include <iostream>
using namespace std;

int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}```
Both taken from: http://www.cplusplus.com/doc/tutorial/arrays.html

This was a full program for example that was in the tutorial, and I do not understand what the billy[n] thing is doing. If I am correct,

result += billy[n]

is:
result = result + billy[n]

But there is no billy N.

I also do not understand what this code is doing:

=
Code:
```#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT * WIDTH];
int n,m;
int main ()

{
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}
return 0;

}```
Why is this code acessing jimmy like this? There is no jimmy by the name of n*width+m.

Also for the part delcaring the array, is it declaring an Array with 15 indices (width * height = 3* 5)?

2. ok what you need to understand is how arrays work, they allow
you to store a lot of related data under the same name, by
accessing different elements of the array.

int array [5];

this creates space in the memory for 5 elements of type int.
the individual elements can store data. also element numbering
starts at 0. so to clarify, what this code does:

int billy [] = {16, 2, 77, 40, 12071};

is this

Code:
```billy [0] = 16;
billy [1] = 2;
billy [2] = 77;
.
.```
and so on. the numbers are called the array element index, and
it basically directs the computer to the part of the array to save
the data in.

Code:
```  for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}```
what the for loop does is cycle through a range of values, each
time storing them in the variable n. so the first iteration,

billy[n] is the same as billy[0]
the next time it will be 1, and so on. in effect that program adds
all the elements in the array. you are correct regarding +=

given the above, you should be able to see that you can enter
a mathematical formula in for an array element index and what
it does is access the element which the formula evaluates to.
that should clear up your second question

EDIT:
just one more thing, when you declare arrays, you cant use
a variable size, for example:

int n;
int array [n];

the difference between this and the WIDTH * HEIGHT is that they
are constant values, so when compiling, they become replaced
by 3 and 5, making the array size a definite value (15). it
is possible to allocate a variable sized array using a more complex
topic called memory allocation, but that is a bit beyond your level
for the time being. it is used when the software designer doesn't
know exactly how big an array to allocate, ie user defined. don't
worry about that lot for a while

3. You probably need to study loops at this point, in particular for-loops.
Code:
```  for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}```
For example, in the above code the line result += billy[n] executes five times, with n a different value each time (0,1,2,3,4).

4. Ahh,, I see! It all seems so simple now! I wonder how I missed that :P Sometimes judgement just fails sometimes ya know?

Also, I already know what loops do, they are quite easy to figure out :P