The basics of programming in c++ for beginners

Arrays in C++ (video)

массивы в c++, массивы в с++, одномерный массивArrays are extremely important theme in C ++. The programs are used very often and understand the subject must be thoroughly. Immediately you will please – to understand and learn how to use arrays is simple enough even a beginner.

So, why do we need arrays and what they look like? By now you already know well, that the program data is stored in the declared us variables of a certain type (int, double, char… ). But it so happens, that the program needs to store hundreds of (and even more) variables of the same type of data, as well as the need to work with them - assign values, modify them, etc..  

For example, it is necessary to store the serial numbers of rows. Agree –  anyone would be scared by the thought, it is necessary to create a hundred of type int variables, give each a unique name, and assign a value from 1 to 500-ta. (I'm scared already :) In this case, We simply save arrays.

Note the ground and move on to the practical example:

  • an array in c ++ is the sum of a certain number of one-type variables, with the same name. For example, int array [3];. This entry means, we declared an array named array , which thecontains 3 type variables int;
  • array variables are called elements ;
  • Each element has its own unique code - a sequence number. Using the index, we can refer to a specific element. IMPORTANT - indexing array elements begins with 0. So in an array int array [3] the first element has index 0, and the last - 2. To contact, for example, to the zero element of the array and change its value, it is necessary to specify the name of the array in square brackets to indicate the index– array [0] = 33.

Consider the example:

In line 12, we define an integer constant SIZE, which will store the size of the array (a certain us, the number of elements). On line 13 we declare an array: specify the data type of the, which will be stored in an array of cells,  give the name and specify the size in square brackets.

Important, in square brackets, we can only record the entire constant values. It must be either immediately enter an integer between square brackets in the array declaration (int firstArray[100];), or define an integer constant to declare an array and put in brackets the name of this constant (as in our example).

The second method is preferable to use, If during the program you will have to contact several times to an array through a loop. The reason is, that when we announce the loop, in it, you can specify a condition to change the counter valueSIZE.

Just imagine, that we need to change the size of the array with 10 elements on 200. In this case, we have to only just change the value of an integer constant, and thus we automatically default to the new size and value to the array, and in all cycles of the programme.

You can try this example, add any other digit in constant SIZE. And you will see, that the program will work fine - to create an array of many elements, on how you specify, make data and displays them on the screen.

In strings 15 – 19 define loop for. His counter i will serve as an index of array elements. At the very beginning, it is equal to 0 and each step is incremented by one until, until it becomes equal to SIZE –  the number of array elements.

Note, in one loop and we will assign different values ​​to the array elements and the next line of appeal to them, to display the data, they store, the screen.

Run the program and see the result:

массивы в c++, массивы в с++, одномерный массив

Assign the value of array elements can be different ways - initialize it when creating or using a loop. If the size of a large array, there is a great opportunity to use a loop for  or while  to initialize its elements. So we did in our example. You can fill the array with random numbers – this we have a separate article.

And if an array of very small, for example on 5 elements, initialize it can be immediately at the announcement:

массивы в c  , arrays in c ++, dimensional array

So the element with index 0 – firstArray[0] -will be set to 11, and the last element of the array firstArray[4] -value 15. There is a chip-you can not specify the size of the array in square brackets, and make a record:

массивы в c  , arrays in c ++, dimensional array

Previous post equivalent to that. Only in the second case, the compiler will automatically calculate the size of the array, in the amount of data in braces.

When initialization of the array elements, when the array must be cleaned of "garbage" (residual data of other programs in memory) it is better to assign all elements of the value 0.  It looks like this:

массивы в c  , arrays in c ++, dimensional array

You should remember, that such initialization is only possible to fill with zeros. If you want to fill the elements of the array to any other numbers, better to use cycle. In C ++ 11 (coding standard) using the list-initialization (initialization with braces) even allowed to drop sign= .

массивы в c  , arrays in c ++, dimensional array

I want to show another reception when creating an array initialization. For example, for an array of 30-minute items we need to make values 33 and 44 only in the cells with index 0 and 1 respectively, and the rest fill with zeros. Then do so:

массивы в c  , arrays in c ++, dimensional array

These data will be included in the zero and the first cell, and the other value will automatically 0.

Organize filling an array you can and using the operator cin:

To assign or change the value of a specific item, it is necessary to refer to it, using its index. For example, all the values ​​of an array of 500 elements that suit us, but it is necessary to change the value of only one. Then we turn to it by its index :  firstArray[255] = 7;

With that sorted out, now let's look, how does the array is located in memory. An array of type int of the five elements will take 20 bytes of memory – 4 bytes (int) * 5 (amount of elements) – and the data will reside in memory sequentially, as shown in Figure:

массивы в c  , arrays in c ++, dimensional array
int array of five elements in memory

 To summarize and mention the most important of all arrays:

  • array declaration syntax :

         tip_Dannyh_Massiva array_name [the size];

    • array variables are called elements, and each element has its own serial number - index.

 

    • numbering array indexes start at zero!!!

 

    • initialize an array only when it is created – int firstArray[3] = {1, 2, 3};  Initializes later is not allowed:   firstArray[3] = {1, 2, 3};  If the array has not been initialized in the beginning, you can assign values ​​to its elements, using loops or simply referring to a desired item by its index.

 

  • an array can be one-dimensional – such, as discussed in this example, and multidimensional –  dvumernыm, three-dimensional ... (they will be discussed in one of our next articles).

Don't forget about the need to practice solving tasks – Tasks: Arrays in C++. Want to learn more about arrays in C ++ (including character arrays and strings)? Watch these video tutorials:

25 thoughts on “Arrays in C++ (video)

    1. create a variable to store the value of the number of elements and put the value there like.
      int N = 0; //amount of elements
      cin<<N;
      int Arr[N];

Leave a Reply

Your email address will not be published. Required fields are marked *