Tipsglobe Logo Facebook Share Twitter Share Google Share LinkedIn Share StumbleUpon Share

0
Imagine a running competition with 5 runners. We want to store the seconds needed to finish the race for each runner. We would need 5 variables, one for each runner:
int runner1 = 200;
int runner2 = 105;
int runner3 = 340;
int runner4 = 243;
int runner5 = 345;
The disadvantage of the above code is that we can`t loop through the results. In this situation the best method is to use an array.
An array would look in the following way:
runner[0] = 200;
runner[1] = 105;
runner[2] = 340;
runner[3] = 243;
runner[4] = 345;

Those numbers in the brackets are the index number, and these numbers are showing the position of a given value in the array.
Initializing arrays
An array can be initialized in three different ways:

1. Syntax:
type identifier[max_elements_number];
Example:
int runner[5];
runner[0] = 200;
runner[1] = 105;
runner[2] = 340;
runner[3] = 243;
runner[4] = 345

2. Syntax:
type identifier[max_elements_number] = {element_1,element_2,element_max_elements_numbe};
Example:
int runner[5] = {200,105,340,243,345};
The number of elements can be fewer than the max_elements_number, but not greater.

3. Syntax:
type identifier[] = {element_1,element_2,element_n};
Example:
int runner[] = {200,105,340,243,345};
The maximum number of elements is generated automatically based on the added elements number.

The runner array look in the following way for the computer:
runner =
200105340243345
(0) (1) (2) (3) (4)
This is an one-dimensional array, because it has only one index number. So, yes an array can have more than one index. Now you probably ask, why to use more than one indexes ?

Well, let`s continue the idea of a running competition. Now imagine that you need to store the timings of the last 3 competitions for our 5 runners. The new array would look like this:
runner =
200105340243345
223324323454123
245203150340311
(0)
(1)
(2)
(0) (1) (2) (3) (4)
So we have 5 runners and a column for each of them. We have 3 competitions and a row for each of them.

Getting a value from this table
Let`s say we need the timing of runner 2`s from the competition 1:
cout << runner[2][1]; //323

Initializing a multi-dimensional array
Because this is very similar to the one-dimensional arrays I will show you just some example:
int runner[5][3];

or

int runner[5][3] = {{200,223,245},{105,324,203},{340,323,150},{243,454,340},{345,123,311}};

or

int runner[][3] = {{200,223,245},{105,324,203},{340,323,150},{243,454,340},{345,123,311}};
Note: With multi-dimensional arrays only the first index can be leaved blank!

The max_elements_number needs to be a constant value, this can`t be changed during the program runs. The first element of an array is always the 0th element.

Here is an example of how useful can arrays be:
#include

using namespace std;

int main(){
int number[11];
for(int i=0;i<=10;i++){
number[i] = i;
}
for(int i=0;i<=10;i++){
cout << number[i] << endl;
}
system("pause");
}
The above program simply loads an array with numbers from 0 to 10 (11 elements) and then writes out the numbers. Using simple variables this would not be possible, we would need 11 variables and many lines of code for the same result.

Post a Comment Blogger

Emoticon
:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
Top