Array Array elements Sum of array elements, quantity

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

array array elements

Definition

Before working with this element of the programming environment, we need to understand what we are dealing with. Teachers at universities can tell you abstruse definitions and require you to cram them, but it doesnโ€™t matter, for a real programmer it is important to understand the essence, and not be able to explain it to others. What is an array? The elements of the array all together make up this object. In other words, it is a collection, a table, a row of different values. Together, they make up a numbered list of items. The array looks like this:

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

Different types of programming language can be assigned to these values. For example, in Pascal, numbering can occur exclusively in numbers and the variable i can only be of type integer. In PHP, things are different. There i is the key by which an element can be found in the array, and it does not matter if the key is a whole word - array ("bar"). Moreover, the elements of the array themselves can be of absolutely any type.

array elements

Cycles

This concept is useful to us when considering some operations with arrays. Loops are conditional expressions that allow you to repeat the same operation again and again until the repetition condition is met. Two types of cycles can be distinguished.

  • "Not yet." In this case, the body of the cycle will be repeated until the final condition. That is, the counter will change first, then the calculations will go through, and only then the cycle will complete the work.
  • "Till". With this option a little different. First, the execution condition is checked, then the loop program is executed, and only then does the counter change.

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

Addition

In some cases, the programmer needs to know what is the sum of the elements in the array. This task means that we need to add all the elements of the array. With this, cycles will help us. In this example, we will not focus on a specific programming language and simply describe line by line what should be contained in which line.

php array element

  1. Declare variables. We need to declare the array "M", the counter of the number of the element of the array "i", a variable indicating the number of elements in the array "k", as well as the variable "R", which will display the result of the operation.
  2. Enter the number of elements in the "k" array in any way.
  3. Entering array elements. 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 hardest part. 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 opens with the "repeat" command. After that, to the previous value of the variable, meaning the sum of all elements of the array, we add the next element of the array. Increase the counter (array number). Next, with the "until" command, we check whether the loop counter is out of the array. Indeed, if we have only 5 elements (k = 5), then adding M [6] makes no sense, it will be empty.

Condition

Before moving on to the next problem with arrays, let's recall the conditional operators. In most programming languages, its syntax looks like this:

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

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 in comparing different values โ€‹โ€‹and determining their further "fate". Together with cycles, they turn into a powerful tool for analyzing a data array.

Comparison

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

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

while (i <= k)

{

i = i + 1; // either can be replaced with i + = 1;

if (R <= M [i])

{

R = M [i]

}

}

maximum array element

As you can see, this type of cycle first checks the condition, and only then starts the calculation of the amount. What exactly is going on? 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 "R" is less than an array element, then it will be assigned the value of this element. Thus, by the time we go through the entire array, the largest number will be contained there.

Php

At the moment, this is one of the most popular programming languages. It is strange that in most even the most eminent universities they teach not him, but the most commonplace principles, which a fifth grader is also able to master. How is it so different from the other languages โ€‹โ€‹we have examined?

PHP allows the programmer to compose the most versatile array. Elements of the array in it can be absolutely any type. If in the same Pascal we need to specify a single type (for example, numeric), then we canโ€™t write a line with text there without changing the type of array ... But if you change the type, then the numeric data in it will become just text, and that means we cannot perform any mathematical operations with them without additional code and a headache.

In PHP, an array element is an independent unit. The array is used solely for the convenience of storing information and accessing it. And most importantly, for those who are used to working with arrays for other PLs, you can organize exactly the same element counters. Accessing array elements in PHP is a bit more complicated than in other languages, but it's worth it.

number of array elements

Total

What can be said in conclusion? Arrays are multidimensional data storages that allow you to operate with large amounts of information while working with them. Multidimensional arrays were not considered in this article, since this topic is for a separate discussion. Finally, a little advice. To make it easier to understand the topic of arrays, imagine a series of numbers - this is the first, this is the second and so on. This is an array. If you need to contact 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.

Source: https://habr.com/ru/post/K19871/


All Articles