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.int runner2 = 105;
int runner3 = 340;
int runner4 = 243;
int runner5 = 345;
An array would look in the following way:
runner[0] = 200;
runner[1] = 105;
runner[2] = 340;
runner[3] = 243;
runner[4] = 345;
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
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 =
200 | 105 | 340 | 243 | 345 |
(0) (1) (2) (3) (4)
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 =
200 | 105 | 340 | 243 | 345 |
223 | 324 | 323 | 454 | 123 |
245 | 203 | 150 | 340 | 311 |
(0)
(1)
(2)
(1)
(2)
(0) (1) (2) (3) (4)
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!
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.
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");
}
Post a Comment Blogger Facebook
Click to see the code!
To insert emoticon you must added at least one space before the code.