Arrays are helpful critters because they can be used in many ways. For example, a tic-tac-toe board can be held in an array. Arrays are basically a way to store numerous values below the similar given name. You can create an array out of any data-type including structures and classes.

Think about arrays like this:

Code:
[][][][][][]
All of the bracket pairs are a slot (element) in the array, as well as you can set information into every one of them. It is approximately like having a collection of variables side by side.

Lets look at the syntax for declaring an array.

Code:
int examplearray[100]; // This declares an array
This would create an integer array with 100 slots, or else places to store values (also called elements). To access an exact part factor of the array, you just set the array name and, in brackets, an index number. This corresponds to an exact constituent of the array. The one trick is that the first index number, as well as thus the first element, is zero, and with the last is the number of elements minus one. 0-99 in a 100 element array, for example.

What can you perform with this straightforward knowledge? Let’s declare you want to store a string, as C had no included datatype for strings, it was universal to make use of arrays of characters to reproduce strings. (C++ now has a string type as part of the standard library.)

For example:

Code:
char astring[100];
Will permit you to state a char array of 100 elements, or else slots. Then you can obtain input into it from the user, as well as if the user types in a long string, it will go in the array. The efficient fixation is that it is extremely simple to work with strings in this way, as well as there is even a header file called cstring. There is one more lesson on the uses of strings, so it’s not essential to converse here.

The most helpful feature of arrays is multidimensional arrays. How I think regarding multi-dimensional arrays:

Code:
[][][][][]
[][][][][]
[][][][][]
[][][][][]
[][][][][]
This is a graphic of what a two-dimensional display appears like when I imagine it.

For example:

Code:
int twodimensionalarray[8][8];
Declares an display that has two dimensions. Visualize of it as a chessboard. You can simply make use of this to store information regarding several type of game or else to write somewhat like tic-tac-toe. To access it, all you require are two variables, one that goes in the first slot as well as one that goes in the second slot. You can even make a three dimensional array, though you most likely won't require to. Actually, you might make a four-hundred dimensional array. It would be confusing to imagine, But. Arrays are treated like any additional variable in most ways. You can alter one value in it by putting:

Code:
arrayname[arrayindexnumber] = whatever;
Or, for two dimensional arrays

Code:
arrayname[arrayindexnumber1][arrayindexnumber2] = whatever;
But, you should not at all try to write data what went before the previous constituent of the collection, for example when you have a 10 aspect collection, as well as you try to write to the [10] aspect. The memory for the collection that was allocated for it will only be ten positions in memory; however the next location could be something, which could crash your PC.

You will locate lots of helpful things to do with arrays, from storing information regarding certain things below one name, to making games like tic-tac-toe. One proposal I have is to make use of for loops when access arrays.

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();
}
Here you make out that the loops work fine as they raise the variable for you, as well as you only require to increase by one. It’s the easiest loop to read, as well as you access the whole array.

One fixation that arrays don't need that additional variables do is a situation operator when you want to have a pointer to the string. For example:

Code:
char *ptr;
char str[40];
ptr = str;  // Gives the memory address without a reference operator(&)
As opposed to

Code:
int *ptr;
int num;
ptr = &num; // Requires & to give the memory address to the ptr
The basis for this is that when an array given name is used as an appearance, it refers to a pointer to the former aspect, not the whole array. This rule reasons a huge deal of uncertainty.