/ / Array. Elements of the array. Sum of array elements, number

The array. Elements of the array. Sum of array elements, number

Programming is a long, creative process. It's hard enough to learn anything in this area if you do not have any ability to understand the principles on which programs and applications should be built. Today we'll talk about the array, array elements and the simplest operations with them.

array array elements

Definition

Before working with this element of the environmentprogramming, we need to understand what we are dealing with. Teachers at universities can persist you with abstruse definitions and require you to cram them, but it does not matter, for a real programmer it is important to understand the very essence, and not be able to explain it to others. What is an array? Elements of the array all together and make up this object. In other words, it is a set, a table, a string of different values. Together they make up a numbered list of elements. The array looks like this:

  • M (i), where M is the array itself, its name. i is the number of the element of the array. Together these two numbers can be read as the i-th element of the array M.

In various programming languages ​​thisvalues ​​can be assigned different types. For example, in Pascal, the numbering can only occur in digits and the variable i can only be of type integer. In PHP, everything is different. There i is the key by which the element can be found in the array, and it does not matter if the key is the whole word - array ("bar"). In this case, the elements of the array can be of absolutely any type.

array elements

Cycles

This concept is useful to us when consideringsome operations with arrays. Cycles are conditional expressions that allow you to repeat the same operation over and over again until the repetition condition is met. There are two types of cycles.

  • "Not yet." In this case, the cycle body will be repeated until the final condition is reached. That is, the counter will first change, then the calculations will take place, and only then the cycle will finish.
  • "Till". With this option a little bit different. First, the execution condition is checked, then the loop program is executed, and only then the counter changes.

In principle, both options are equivalent, in our case it does not matter which one to use, but each will have its own method.

Addition

In some cases, the programmer needs tofind out what the sum of array elements is. This task means that we need to add all the elements of the array. Cycles help us with this. In this example, we will not focus on a specific programming language and just line by line to describe what line to contain.

php array element

  1. Declare the variables. We need to declare an array "M", the counter of the element number of the array "i", a variable indicating the number of elements of the array "k", and also the variable "R", which will display the result of the operation.
  2. Enter the number of elements of the array "k" in any way.
  3. Enter the elements of the array. You can organize it through a series of dialog boxes with the user or simply assign values ​​to each individually.
  4. Assign i = 1, R = 0.
  5. Now the most difficult. We need to organize a cycle. To do this, you must first select its type. Below is an example of a cycle for counting elements. For example, we used the programming language - Pascal.

repeat

R = R + M [i];

i = i + 1;

until i> k

What do we see? First, the loop is opened with the "repeat" command. After that, to the previous value of the variable, which means the sum of all elements of the array, we add the next element of the array. We increase the counter (the number of the array). Next, with the "until" command, we check to see if the loop counter has gone out of the array. After all, if we only have 5 elements (k = 5), then adding M [6] does not make sense, it will be empty.

Condition

Before proceeding to the next task with arrays, let's remember conditional operators. In most programming languages, its syntax looks like this:

if (condition) then (series of commands) else (commands if the condition is false);

sum of array elements

The general description may sound like this: "If the condition is true, then make the first block of commands, otherwise make the second block." Conditional operators are useful when comparing different values ​​and determining their further "fate". Together with the cycles, they become a powerful tool for analyzing the data array.

Comparison

What else allows us to make an array? Elements of the array can be sorted, checked to see if they are suitable for certain conditions, and compared between each other. Another favorite example of university teachers is to find the maximum element of the array. For example, we use the C ++ language.

  • Without going into details, it is necessary to declare the sameVariables, as in the previous example, with a few exceptions. With another type of cycle, you'll have to cheat a little. In the new case, "i = 0". Why this need, explain below.

while (i <= k)

{

i = i + 1; // or can be replaced by i + = 1;

if (R <= M [i])

{

R = M [i]

}

}

the maximum element of the array

As you can see, this type of cycle first checkscondition, and only then starts counting the amount. What exactly is happening? First, the validity of the inequality i <= k is checked, if so, we go to the first element of the array M [1] and compare it with our checking variable "R". If the "R" is smaller than the array element, then the value of this element is assigned to it. Thus, by the time we go through the whole array, there will be the largest number.

PHP

At the moment this is one of the mostdemanded programming languages. It is strange that in most even the most eminent universities are taught not to him, but to the most banal basics, which a fifth-grader is able to master. What makes it so different from other languages ​​we have studied?

PHP allows the programmer to compile theversatile array. Elements of the array in it can be of absolutely any type. If in the same Pascal we need to specify a single type (for example, numeric), then we will not write a line with the text there, without changing the type of the array ... But if you change the type, then the numeric data in it will become just text, and so we can not perform any mathematical operations with them without additional code and headache.

In PHP, an array element is an independentunit. The array is used exclusively for the convenience of storing information and accessing it. And most importantly, for those who are accustomed to working with arrays on other YAPs, you can organize exactly the same element counters. The reference to array elements in PHP is a little more complicated than in other languages, but it's worth it.

number of elements in the array

The result

What can we say in conclusion? Arrays are multi-dimensional data stores that allow you to operate while working with them with large amounts of information. This article did not consider multidimensional arrays, since this topic is for a separate conversation. Finally a little advice. In order to better understand the subject of arrays, imagine a series of numbers - here is the first, here is the second and so on. This is the array. If you need to address one of them, simply indicate the program number. This perception will greatly simplify your life in school. Remember that it is not always worth listening to the abstruse speeches of teachers, it is better to find your way to understanding the topic.

Read more: