# Thread: Need help on understandind arrays

1. ## Need help on understandind arrays

Hey, I went over the tut on arrays, and I haft to say I don't get one bit of if. How do I use it and why, what for? I'm really confused about this, I read the tut, but I really don't understand it.

When I ran the program in the tut, well, lets just say I don't know where all this info is coming from or how to use it, can anyone help me please? I'm really wanting to learn C++ right about now.

2. Well, post the code you don't understand and tell us what you understand from the code. We'll take you on from there. Don't forget to post the code between [.code][./code] (without dots)

3. Alright, this is the code,

Code:
```#include <iostream>

using namespace std;

int main()
{
int x;
int y;
int array[8][8]; // Declares an array like a chessboard

for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ )
array[x][y] = x * y; // Set each element to a value
}
cout<<"Array Indices:\n";
for ( x = 0; x < 8;x++ ) {
for ( y = 0; y < 8; y++ )
cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
cout<<"\n";
}
cin.get();
}```
Thing I don't understand is where all the numbers come from when I run this program.

And if you don't mind, could you give me a better understanding of arrays, cause I still don't quite get it.

EDT: Oh wow, nevermind, I figured out what was wrong. I didnt' put the code in properly, heh.

But hey, could you still give me a better understanding of this?

4. Code:
`int array[8][8];`
Any array or 'board' that is 9 by 9. It's not 8 by 8 because the array starts at [0][0].

Code:
```  for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ )
array[x][y] = x * y; // Set each element to a value
}```
This is the method of giving each part of the array it's own value so if you built the array the order the values assign would look like this because for each time that x loops y will loop 8 times. Each [] representing an element in the array
Code:
```[1] [10] [19] [] [] [] [] []
[2] [11] [20] [] [] [] [] []
[3] [12] [21] [] [] [] [] []
[4] [13] []   [] [] [] [] []
[5] [14] []   [] [] [] [] []
[6] [15] []   [] [] [] [] []
[7] [16] []   [] [] [] [] []
[8] [17] []   [] [] [] [] []
[9] [18] []   [] [] [] [] []```
It would go on to fill the rest of the elements but keep in mind those numbers aren't actually stored in the array but they are there to demonstrate the order in which the for loop gives each [] a value. Each array element is set to x*y and the x and y values depend on how far the for loops have gone through their cycle.

Code:
```  cout<<"Array Indices:\n";
for ( x = 0; x < 8;x++ ) {
for ( y = 0; y < 8; y++ )
cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
cout<<"\n";
}```
This just goes back through and outputs the numbers previously stored in the array.

5. >> Any array or 'board' that is 9 by 9. It's not 8 by 8 because the array starts at [0][0].

This is not correct. The array is 8x8 because when it is declared the values in the brackets indicate the size. Indexes 0-7 are valid for each dimension.

6. Arrays are just large clumps of variables and data.
In large programs with many variables it can be extremely tiresome to declare 300 integers in this manner.
int Num1, Num2, Num3.. etc..

You could declare an array of 300 integers with much more ease.
int Array[300]

The program you demonstrated was a two-dimensional array.
int Array[300][300]

Arrays work by indexing the desired integer based on the number you put inside the brackets.
Array[253] = 12
That would assign the 254th element in the array to 12.

For loops are used to iterate through each element assigning or reading the data from within the array.

for(int i = 0; i <= 299; ++i)
std::cout << Array[i];

would display every element in my previous array.

Arrays first element is also 0
So Array[0] would be a correct operation.

Code:
```
for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ )
array[x][y] = x * y; // Set each element to a value
}```
Just assigns the board to values similiar to a times table.

7. Sorry accidentally posted twice.

8. Originally Posted by JJFMJR
Arrays work by indexing the desired integer based on the number you put inside the brackets.
Array[253] = 12
That would assign the 253rd element in the array to 12.
well... 254th.

9. Thanks for your insights guys, it's really helping me out. My only question left now that leaves me in confusion is.

Whats the difference between:
cout<< array[i];

and

std::cout<< array[i];

10. Originally Posted by C++mastawannabe
Thanks for your insights guys, it's really helping me out. My only question left now that leaves me in confusion is.

Whats the difference between:
cout<< array[i];

and

std::cout<< array[i];