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 the contains 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 string 12 we define an integer constant SIZE, which will store the size of the array (a certain us, the number of elements). In string 13 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 value SIZE. 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 the loop. 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 you must change the value of a single. 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 this video lesson:

Newsletter of programming:

Date
page
Arrays in c++. One-dimensional arrays in C ++.
Rating
51star1star1star1star1star

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

  1. I have a question, But if for example I xochu ask your question in the program, how to make, to keyword perform certain actions ?

    For example:

    When you enter the program “find the perimeter”, then at search suggestions, program naxodila word “perimeter” perexodila and in line with the perimeter of the expression, and if you enter the phrase “search area”, perexodila is in line with the expression of the area ?

    O'clock 4 I searched in the internet something like, but I did not find, and sort out one letter, so the program codes are a great RON, Well, the permutation of words “find the perimeter” or “subtract the perimeter” prixoditsya already write more if statements.

    1. Languages ​​C / C ++ languages ​​are not very high (compared with Python or Java, for example). And least of all the attention paid to them is processed (analysis) text information. It is much better done in other languages: Perl or Python same.

      Basically, you really have to analyze sequentially one letter … and even taking into account the, what Russian letters presented as wchar_t, i.e.. no single-byte

      Some help in C ++ you can have a string type, type:

  2. I am trying to solve this puzzle:

    “In the one-dimensional array of N elements to find and display the indices of two neighboring elements, the difference between them in absolute maximum. If there are several pairs, view the first”.

    I did not understand a damn. Any such neighboring elements? What's the difference modulo? Help, vyzhprogrammisty!

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>