Array question! I dont understand.

// 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++) // <--I dont understand what these are for? Why use them if none of them are true about the arrays?
{
result += billy[n];/* <--This is essencially result=result+billy[n](0=0+Sum of billy arrays)?
} so why doesnt the number turn out to be negative? */
cout << result;
system("PAUSE");
return 0;

}
Question 1: does n represent the variables inside array billy? if so than why use the for functions n=0 n<5 and n increasing by one? None of these are true or describe a pattern that happens inside the array?
Question 2: isnt result defined as 0? So doesnt the result += billy [n] convert to 0=0+Billy[n]? So wouldnt n have to be negative?
Question 3: How does n translate to become the sum of billy [0]-billy[5] im completely lost! i dont see any definition that leads me to believe that n and the sum of the array are the same? Thanks in advanced im really lost
This:
int billy [] = {16, 2, 77, 40, 12071};
means to initialize an array of integers called billy. The array is filled with the integers 16, 2, 77, 40, 12071 in that order.

This:
int n, result=0;
creates an integer n (which is not initialized yet) and result which is assigned a value of 0.

This:
for (n=0; n<5; n++)
is the beginning of a for loop. It means, at the start of the loop, set n to 0. It will continue while n is less than 5. It increases n by one every time you complete the loop.

This:
result += billy[n];
means result should add billy[n] to itself. Billy[n] refers to the nth element in the array. The first element in the array has an "address" of 0. In this case, billy[0] = 16, billy[1] = 2, billy[2] = 77, etc.

So what the loop is doing is increasing result by billy[n] every time through the loop.
1. n is used to step through each element in the array. n starts at 0 and is increased by one each iteration of the loop. billy[n] <-- this will get the n:th element in the array so after the loop result will contain the sum of all the elements in the array.

2. I don't understand why have to be negative. This: n++ will increase n by one, not decrease.

3. result is the sum. n is just a number that is changed from 0 up to 5.
An array is a group of variables that share a name.

Here's an example:

1
2
3
4
5
int billy0 = 16;
int billy1 = 2;
int billy2 = 77;
int billy3 = 40;
int billy4 = 12071;


Here, we have 5 different variables, each with a unique value. You could do something like this:

int foo = billy1 + billy2; // foo == 79

Arrays let you do the same thing, but instead of having to give a unique name for each variable, they all share the same name. Instead, they have a unique "index":

1
2
3
4
5
6
7
8
9
10
int billy[5];  // an array of 5 ints

int billy[0] = 16; // index 0 is set to 16
int billy[1] = 2;  // index 1 is set to 2, etc
int billy[2] = 77;
int billy[3] = 40;
int billy[4] = 12071;


int foo = billy[1] + billy[2];  // foo == 79 


The thing that makes arrays useful is that you can use a variable to specify the index. So you can do something like this:

1
2
int index = 3;
int foo = billy[ index ];  // billy[index] == billy[3] == 40 



The for loop you posted simply uses 'n' as an index. 'n' counts from 0 to 4, allowing you to access each individual "element" in the array.

For loops operate like so:

1
2
3
4
for( initialization; condition; iteration )
{
   body;
}


1) 'initialization' is done once at loop start.
2) 'condition' is checked. If it's true, continue to step 3. Otherwise, exit the loop.
3) 'body' is executed
4) 'iteration' is executed
5) goto step 2

So with this simple loop:

1
2
3
4
for (n=0; n<5; n++)
{
  result += billy[n];
}


Here, the loop body will run 5 times. The first time it runs, n==0. the next time, n==1, then n==2, etc. The last time it runs... n==4.

The body is then using 'n' as an index to the 'billy' array, accessing each element in it. So the first time, it's adding billy[0] (16) to result. Then it's adding billy[1] (2) to the result, then billy[2] (77), etc.


EDIT: damn ninjas.
Last edited on
Ok thanks that cleared it up so much for me!
One more question if i may :)
Why is n<5? Why cant it be n<8 or n<9? Or does it not matter?
I did some playing around and noticed that anything from n<5-n<12 all give you the right answer. But if you change the loop to
for (n=0; n<13; n++)
you get a completely different answer? Why is that if there is no value for 13 in the array? Does it just repeat at 12 or something?
It is because your array has 5 elements that are numbered from 0 to 4. So the loop has the following construction ( I have introduced an integral constant to make the idea more clear ):


1
2
3
4
5
const int size = 5;

int billy [ size ] = {16, 2, 77, 40, 12071};
...
for ( int n = 0; n < size; n++ ) 
....
Last edited on
Than why will n<12 n<11 n<10 n<9 n<8 n<7 n<6 n<5 all still work?
You are not allowed to access billy[5], billy[6], etc. so we cannot be sure what happens. The program could crash or might not. What probably happens is that it will try to read the memory after the end of the array. This memory might be used for other variables so it just reads whatever is there.
Last edited on
You can imagine your RAM as a large room. When you create a variable, you're staking out a spot in that room. If you set a value to that variable, you're clearing out the spot and putting your own stuff in it.

So when you create the billy array, you cleared out a space for 5 integers and put your own integers there. When you try to access billy[5] to billy[11], well your array isn't actually that large.

But your program still looks in the spaces where those variables would be if they existed (since arrays are contiguous; each element is right next to each other).

It just so happens that the spots where those variables would have used (if they existed) are empty. So when adding whatever is in those spots, the sum doesn't change.

But you suddenly hit a huge pile of trash when you come up to the spot that would have been used by billy[12] and then you start seeing changes in the sum.
Topic archived. No new replies allowed.