Arrays are A brief introduction to the topic

Table of contents:

Arrays are A brief introduction to the topic
Arrays are A brief introduction to the topic
Anonim

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

arrays are
arrays are

Introduction

Today's topic we will start by introducing a definition for this term. Arrays are elements of the programming environment that represent a set of data in the form of a table or string. Imagine a series 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 you to correlate (enter) them with an array in programming.

Record

Let's consider how arrays are written in practice. Write, designate arrays - this means specifying for the created program their type (what values will be stored in the array) and the number of cells. Sometimes programmers create dimensionless arrays without specifying the exact number of elements, but then when accessing them, one must be very careful so that the program does not loop and start accessing empty cells.

D: array[1..k] of real; - this is how an array is written in Pascal. If you know when creating a program that you will have a maximum of 5 elements, then you can use the notation D: array[1..5] of real;

As you might have guessed D is the letter for the name of the array; real is the type (format) of the data that can be contained in the array; array is the number of array elements.

php arrays
php arrays

Appeal

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

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

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

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

If you are planning mathematical operations, or the array just needs to store numbers, it's better to worry about its type in advance and assign it to "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. Likewise, arrays have dimensions. Two-dimensional arrays are tables with data in which each element is assigned not one ordinal number, but two - a row number and a column number. When referring to a two-dimensional array, you must specify both numbers - D[1;1].

array of numbers
array of numbers

Accordingly, such an array will be able to store a larger amount of data. Unfortunately, in the old programming languages, in most cases, only digits can act as the array element number. 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, let's say we have a table that contains student data. They have: year of birth, last name, class.

1989 Ivanov Ivan 9
1988 Petrov Peter 10
….

Under normal circumstances, we would have to create multiple arrays, depending on our needs. We can create one two-dimensional array of numeric type to store the year of birth and class, and a second array to store text information (F. I.). But it's inconvenient. First, the last name and first name may need to be processed separately. Secondly, you can easily get confused when filling the array with the year and class. Therefore, it will be easier to create 4 separate arraysfor each column. Agree, very cumbersome?

PHP

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

$array=array(1989, 1988, …);

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

two-dimensional array
two-dimensional array

$table=array(array(1989, "Ivanov", "Ivan", 9), array(1988, "Petrov", "Peter", 10), …);

What do we end up with? Before us is an array called $table, whose rows correspond to the rows in the presented table. If you talk about the elements of an array, then 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 array columns will be numeric, and 1 and 2 - text. If necessary, you are alwaysyou will be able to convert the required data to the desired format and merge the cells.

Popular topic

Editor's choice

  • Let's build iOs apps! Overview of programs, instructions, recommendations
    Let's build iOs apps! Overview of programs, instructions, recommendations

    Today more and more people are starting to dive into the world of IT. A fairly common industry is the creation of various kinds of mobile applications. Quite a lot of different games and programs have already been developed for Android. It is because of this that many developers have begun to move to the iOS platform. This is not a very crowded industry where you can find a lot of clients and make quite a lot of money

  • Moving WordPress to another hosting: features, procedure
    Moving WordPress to another hosting: features, procedure

    Today, every person who has his own website on the Internet may face the problem that he will need to transfer the site to another hosting. There can be many different reasons for this. Of course, the most common of them is dissatisfaction with the services provided. If you can't take it anymore, then you need to act. This is a rather long procedure. If you do everything step by step, you can safely transfer the site, and not redo everything several times

  • How to pass on the rights in "SAMP RP"?
    How to pass on the rights in "SAMP RP"?

    Every person who plays online games is trying to upgrade his character and achieve many interesting things. Users who prefer SAMP try to get a driver's license even from the first level. Experienced players may not take the exam, but simply go to the instructor and buy rights from him. Of course, it will be somewhat more expensive, but this way you can save your time. How to pass on the rights to "SAMP"? Let's review this

  • How to turn off the camera on a laptop? 3 easy ways
    How to turn off the camera on a laptop? 3 easy ways

    Today, hacking has begun to actively develop in the world of information technology. An experienced hacker can get into almost any computer and get the user's information, provided, of course, the laptop is connected to the network. Recently, users have been complaining that their webcam turns on by itself. This way hackers can see where you are and what you are doing. In the future, this information can be used in completely different ways, up to the fact that you become a star on YouTube

  • Shared AppStore Account Features
    Shared AppStore Account Features

    A shared App Store account has many features. It allows users to install games and applications, as well as watch movies in high definition. Shared accounts give official access to app and game downloads for Apple devices (iPhone, iPad, iPod Touch)