C++ PROGRAMMING EBOOK LESSON 6 – ARRAYS

An clothing is a assemble of variables that uses digit study instead of many. To admittance apiece of the individualist variables you ingest a number.

How to ingest an array

To tell an clothing place conservativist brackets after the study of the array. These conservativist brackets staleness hit the sort of variables or elements exclusive them. Here is an warning of how to tell an clothing of integers which has 3 elements

int arr[3];

You crapper ordered the values of an clothing by using the sort of the surroundings you poverty to ordered in the conservativist brackets behindhand it. Array elements move from 0 and not 1.

int arr[3];
arr[0] = 5;
arr[1] = 7;
arr[2] = 3;

Here is a plateau that shows you what the clothing looks like.

arr

0 5
1 7
2 3

2D arrays

A 2D clothing is an clothing that has both rows and columns. You staleness ingest 2 sets of conservativist brackets when declaring a 2D clothing and when using it.

int arr[3][3];
arr[0][0] = 5;

Here is a plateau that shows you what a 2D clothing looks like.

arr

0 1 2
0 5 2 4
1 3 7 9
2 6 1 8

Using arrays with loops

Arrays materialize to be null more than connatural variables until you ingest them with loops. You crapper set every the elements of an clothing to 0 using a wrap instead of environment them apiece individually.

int arr[3];
for (int x=0; x<3; x++)
arr[x] = 0;

When you do this with a 2D clothing you requirement to ingest 2 loops.

int arr[3][3];
for (int x=0; x<3; x++)
for (int y=0; y<3; y++)
arr[x][y] = 0;

Comments are closed.