ComputersProgramming

Arrays are ... A brief introduction to the topic

Anyone who has studied programming at a university knows that teachers tend to give only basic, basic material for their students. The topic of arrays is also considered, but at later courses. Why? Because arrays are the basis that allows a programmer to work with large volumes of information.

Introduction

Today's topic begins with the introduction of a definition of this term. Arrays are elements of the programming environment that represent a set of data in the form of a table or a row. Imagine a number of random numbers: 1, 6, 2, 4, 8. This will be an array. Each digit written in a line has its own serial number, and this is what allows to correlate them with the array in programming.

Recording

Consider how arrays are written in practice. Write, denote arrays - this means to specify the type of the program being created (what values will be stored in the array) and the number of cells. Sometimes programmers create immeasurable arrays without specifying the exact number of elements, but then when accessing them you have to be very careful that the program does not get stuck and start referring to empty cells.

  • D: array [1..k] of real; - This is how the array is recorded in Pascal. If you know when you create the program that you will have a maximum of 5 elements, you can use the D: array [1..5] of real;

As you might have guessed, D is the letter denoting the name of the array; Real is the type (format) of data that can be contained in an array; Array [] is the number of elements in the array.

Appeal

In order to work with an array element, it must be accessed from the program. Arrays are the same numbers or words as any other. In order to work with an array element, you must enter: D [1]. This allows you to select the first element of the array and perform operations with it. For example:

  • Print (D [1]); - this command will allow to display on the user's screen the value contained in the 1st cell of the array.

It is worth noting that if you are going to perform mathematical operations with arrays, then you should pay attention to the type. You can do this only if you have an array of numbers. To make it more clear:

  • If you have an array D: array [1..k] of text; - and in cell D [1] = 1, then you can not use this element in mathematical operations, because for the program "1" it will be just the word "one", not a digit. So keep an eye on the variables and their types.

If you are planning mathematical operations, or you simply need to store numbers in the array, you better worry about its type in advance and assign "real" or "integer".

Table

Let's now talk about the space around us. We live in a three-dimensional world, and most objects can be described by 3 parameters: length, width, height. So in arrays there is a dimension. Two-dimensional arrays are tables with data in which each element is awarded more than one sequence number, and two - the row number and column number. When accessing a two-dimensional array, you must specify both numbers - D [1; 1].

Accordingly, such an array will be able to store a larger amount of data. Unfortunately, in older programming languages, in most cases, the number of an array element can only be numbers. Therefore, storing data from large tables becomes very problematic due to the fact that each column of the table will have to create a separate array.

For example, suppose we have a table that records student data. They have: year of birth, last name, class.

1989 Ivanov Ivan 9
1988 Petrov Peter 10
....

Under normal conditions, we will have to create several arrays, depending on the needs. We can create one two-dimensional array of a numeric type to store the year of birth and class, and the second array for storing text information (FI). But it is inconvenient. First, the name and surname may need to be processed separately. Secondly, you can easily get confused when filling an array with year and class. Therefore, it will be easier to create 4 separate arrays for each column. Agree, it is very cumbersome?

PHP

PHP arrays solve the problem mentioned above. The fact is that in this programming language you can specify not only the data type in the array, but also the type of the counter (index). In addition, in one array can contain data of a variety of types. Creating a one-dimensional array (if you want to take one column):

  • $ Array = array (1989, 1988, ...);

This is an example of creating a simple array. The index is created automatically and is counted from zero. That is, the zero element of the array is 1989, the first is 1988, and so on. But what if we need to put the entire table into a multidimensional array? What are multi-dimensional PHP arrays? These are structures in which each element is also an array. How to disassemble the example given to us?

$ Table = array (

Array (1989, "Ivanov", "Ivan", 9),

Array (1988, Petrov, Peter, 10),

...

);

What do we have in the end? Before us is an array named $ table, in which the rows correspond to the rows in the presented table. If we talk about the elements of an array, they will look like this:

  • $ Table [0; 0] = 1989, $ table [0; 1] = "Ivanov", $ table [0; 2] = "Ivan", $ table [0; 3] = 9.
  • $ Table [1; 0] = 1988, $ table [1; 1] = "Petrov", $ table [1; 2] = "Peter", $ table [1, 3] = 10.

In this case, 0 and 3 columns of the array are numeric, and 1 and 2 are text columns. If necessary, you can always convert the necessary data into the desired format and merge cells.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

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