ComputersProgramming

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.

Definition

Before working with this element of the programming environment, we need to understand what we are dealing with. Teachers at universities can repeat inscrutable 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 different programming languages, these values 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 an entire word - array ("bar"). In this case, the elements of the array can be of absolutely any type.

Cycles

This concept is useful to us when considering some 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 be performed, 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 to know 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 particular programming language and just line by line to describe what line to contain.

  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 left 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 problem 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);

The general description may sound like this: "If the condition is true, then make the first instruction block, 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, you need to declare the same variables 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 is needed, we will explain below.

While (i <= k)

{

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

If (R <= M [i])

{

R = M [i]

}

}

As you can see, this type of loop first checks the condition, and only then starts the calculation of the sum. 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 "R" is smaller than an 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 most popular 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. Why is it so different from other languages we have considered?

PHP allows the programmer to compile the most versatile 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 down the 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 independent unit. The array is used exclusively for the convenience of storing information and accessing it. And the main thing is that for those who are accustomed to working with arrays on other AP, you can organize exactly the same element counters. Accessing the elements of an array in PHP is a little more complicated than in other languages, but it's worth it.

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. In this article, multidimensional arrays were not considered, 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 absurd speeches of teachers, it is better to find your way to understanding the topic.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.