Follow

Subscribe

Follow

Subscribe

Arrays

Learn now how to manipulate large data sets. With arrays, you have the ability to manipulate multiple variables as if they were a single one.

Before starting to learn about arrays, it is essential that you already know what a variable and a for repetition structure is.

To learn about variables, read this article where we explain to you in detail about the subject.

To learn about for repetition structures, read this article where we explain to you in detail on the subject.

But let’s get down to business!

What is an array?

In the most direct way possible, an array can be defined as a programming feature that allows you to manipulate many variables in a simple and intuitive way. But before understanding how this is possible, let’s take a look at some practical examples of the problems that can be solved by using an array.

The problem of using variables

Imagine that we want to create a program to ask the user for a single numeric value and display it on the screen. It is a very simple task! The Algorithm below does just that:

1.int value;

2.println("Enter a value");

3.value = input();

4.println("Entered value = "+value);

See in line 1 (code above) that a single variable was created to store this value, which was requested from the user (line 3 of the code above) and printed on the screen (line 4 of the code above).

Now imagine that, instead of a single numeric value, we need to ask the user for 5 numeric values. In this case, we need to create 4 more variables, request the 4 more values from user and write 4 more print commands. See below how this new code would look:

1.int value1;

2.int value2;

3.int value3;

4.int value4;

5.int value5;

6.println("Enter the value 1");

7.value1 = input();

8.println("Enter the value 2");

9.value2 = input();

10.println("Enter the value 3");

11.value3 = input();

12.println("Enter the value 4");

13.value4 = input();

14.println("Enter the value 5");

15.value5 = input();

16.println("Value 1 entered = "+value1);

17.println("Value 2 entered = "+value2);

18.println("Value 3 entered = "+value3);

19.println("Value 4 entered = "+value4);

20.println("Value 5 entered = "+value5);

As you can see the code has grown a lot! For each new information that the program needs to manipulate, we need to create a new variable. Imagine now if we needed to request 1000 values instead of 5. It would be a disaster! The code would be huge!

To begin to solve the problem of having to write many lines of code to deal with many variables, we resort to the use of arrays.

How to create an array?

The standard way to create an array varies depending on the programming language. Below is a widely used way to create an array. The code shows the creation of a single array called “values” to store 5 variables of type int:

1.int[ ] values = new int[5];

See (in the code above) the striking detail of an array: the presence of the brackets [” “]” (known as “subscript operator”). They are the ones that differentiate the creation of an array and a variable. Note that “int value” is a variable and “int[ ] values” is an array.

Let’s look at more details.

The creation of an array is divided into two stages: declaration and initialization. See the illustration below.

The snippet “int[ ] values” represents the declaration of the array of type int. The “new int[5]” section represents the initialization of the array. In the declaration we use the empty brackets [” “]“. At initialization we put the capacity value inside the square brackets.

We could have created arrays to store other Primitive Types of variables in place of the type int. For example:

1.char[ ] letters = new char[10];

2.double[ ] wages = new double[35];

3.boolean[ ] validations = new boolean[12];

We could also have created arrays to store Abstract Data Types, instead of Primitive Types:

1.String[ ] names = new String[8];

2.Employee[ ] employeesList = new Employee[30];

3.People[ ] residents = new People[100];

It is important to note that there are striking differences in the way of creating an array depending on the programming language that is used. In the C and C ++ languages, for example, the creation is much simpler. In the Python language, there is no arrays; libraries, such as NumPy, are used to emulate the behavior of an array.

See below the code for creating an array in several different programming languages:

1.int[ ] values = new int[5];

2.// or 

3.int values[ ] = new int[5];

1.// there is no need to use the word new 

2.int values[5];

1.// there is no need to use the word new 

2.int values[5];

1.int[ ] values = new int[5];

1.# Python lacks support for arrays 

2.# It is recommended to use the NumPy library 

Indexes of an array

The positions of an array (known as indexes) are used as a mechanism for accessing its elements. Let’s take the example of our array of capacity 5, that is, with 5 indexes.

1.int[ ] values = new int[5];

The indexes of an array are numbered from the value 0 (zero) to the value of its capacity minus one unit (capacity-1). For example: the array of capacity 5 (created in the code above) has the indexes from 0 to 4, as shown in the illustration below:

Likewise, an array of capacity 10 has indexes from 0 to 9:

The indexes represent the positions of each of the variables in an array.

Exercise 1

Show that you understand the concept of indexes in an array and answer the exercise below:

Assigning values to an array

To better understand the meaning and use of indexes, let’s compare the assignment of values to a variable with the assignment of values to an array:

Assigning a value to a variable

1.int value;

2.value = 50;

Notice in line 2 of the code above how the value 50 is assigned to the variable “value” without any problem! It is as if the value 50 is thrown into the variable.

Could the same be done for an array?

Assigning a value to an array

1.int[ ] values = new int[5];

2.values = 50; // this is a error! 

Notice in line 2 of the code above how the value 50 is assigned to the “valuesarray! This is a big mistake! The fact that the “valuesarray is a collection of 5 variables prevents the value 50 from being directly assigned to it without first specifying the index of the insertion position.

To specify the index of the insertion position in an array, we also use the brackets [” “]” (the “subscript operator”). See the code below for an example of creating an array of capacity 5, assigning some values and printing those values on the screen:

1.int[ ] values = new int[5];

2.values[0] = 13;

3.values[1] = 53;

4.values[2] = 9;

5.values[3] = 16;

6.values[4] = 25;

7.println(values[0]);

8.println(values[1]);

9.println(values[2]);

10.println(values[3]);

11.println(values[4]);

Notice in lines 2 to 6 (code above) how the brackets are used to specify the position of assignment of the values in the array. The same is done on lines 7 to 11 (code above), where the brackets are used to read the values of the array.

See in the animation below the simulation of the assignment of values to an array by its indexes:

See below for complete examples of the code above in several different programming languages:

1.import java.util.Scanner;

2.class Main {

3.     public static void main(String[] args) {

4.          int[ ] values = new int[5];

5.          values[0] = 13;

6.          values[1] = 53;

7.          values[2] = 9;

8.          values[3] = 16;

9.          values[4] = 25;

10.          System.out.println(values[0]);

11.          System.out.println(values[1]);

12.          System.out.println(values[2]);

13.          System.out.println(values[3]);

14.          System.out.println(values[4]);

15.     }

16.}

1.#include <stdio.h>

2.int main(void) {

3.     int values[5];

4.     values[0] = 13;

5.     values[1] = 53;

6.     values[2] = 9;

7.     values[3] = 16;

8.     values[4] = 25;

9.     printf("%d\n",values[0]);

10.     printf("%d\n",values[1]);

11.     printf("%d\n",values[2]);

12.     printf("%d\n",values[3]);

13.     printf("%d\n",values[4]);

14.}

1.#include <iostream>

2.int main() { 

3.     int values[5];

4.     values[0] = 13;

5.     values[1] = 53;

6.     values[2] = 9;

7.     values[3] = 16;

8.     values[4] = 25;

9.     std::cout << values[0] << std::endl;

10.     std::cout << values[1] << std::endl;

11.     std::cout << values[2] << std::endl;

12.     std::cout << values[3] << std::endl;

13.     std::cout << values[4] << std::endl;

14.}

1.using System;

2.class MainClass {

3.     public static void Main (string[] args) {

4.          int[ ] values = new int[5];

5.          values[0] = 13;

6.          values[1] = 53;

7.          values[2] = 9;

8.          values[3] = 16;

9.          values[4] = 25;

10.          Console.WriteLine(values[0]);

11.          Console.WriteLine(values[1]);

12.          Console.WriteLine(values[2]);

13.          Console.WriteLine(values[3]);

14.          Console.WriteLine(values[4]);

15.     }

16.}

1.# Python lacks support for arrays 

2.# This example uses the NumPy library 

3.import numpy

4.values = numpy.array([13, 53, 9, 16, 25])

5.print(values[0])

6.print(values[1])

7.print(values[2])

8.print(values[3])

9.print(values[4])

Limits of an array

It is the sole responsibility of the programmer to manage the limits of an array’s indexes. Imagine what would happen if we tried to access index 6 of an array that has capacity 5? The code below illustrates this situation:

1.int[ ] values = new int[5];

2.values[6] = 37;

The problem is that, in most programming languages, this algorithm would be compiled and executed normally. The error would happen during execution, when the algorithm tries to assign the value 37 to index 6 of the array (line 2 of the code above). This type of error is known as “indexing out of bounds“.

It is very common for novice programmers to make these “indexing out of bounds” errors when using arrays. So stay tuned!

Exercise 2

Based on the code below, drag the values to the correct indexes of the array.

Automation of arrays

Let’s return to the initial problem of the article, which creates a program to request 5 numerical values from the user and displays them on the screen. But now we are going to use an array instead of 5 variables. The code below shows this program:

1.int[ ] values = new int[5];

2.println("Enter the value 1");

3.values[0] = input();

4.println("Enter the value 2");

5.values[1] = input();

6.println("Enter the value 3");

7.values[2] = input();

8.println("Enter the value 4");

9.values[3] = input();

10.println("Enter the value 5");

11.values[4] = input();

12.println("Value 1 entered = "+values[0]);

13.println("Value 2 entered = "+values[1]);

14.println("Value 3 entered = "+values[2]);

15.println("Value 4 entered = "+values[3]);

16.println("Value 5 entered = "+values[4]);

Note that the only optimization achieved in this code was the creation of the array (line 1 of the code above). Instead of creating 5 variables, we created just a single array of data.

Note that the user’s requests for values (lines 2 to 11 of the code above) and screen prints (lines 12 to 16 of the code above) still continue with repeated code.

This is not good! If we need to increase the number of requested values to 1000, we will face the same problem of gigantic source code!

To solve this problem we use repetition structures. The use of arrays is closely linked to the use of repetition structures. Without them, it is not possible to obtain the advantages of automation and code savings that arrays can provide.

But we will understand this with more examples. Observe the for repetition structure below:

1.for(int i=0; i < 5; i++){

2.     println(i);

3.}

If you know the for command, you know that the control variable “i” will, during repetitions, assume all values in the range 0 to 4.

Now observe in the illustration below how the indexes of an array of capacity 5 exactly match the values in the range 0 to 4.

Coincidence? No! Automation!

It is precisely the control variable “i” of the for command who will provide the automation of the array. See the code below:

1.for(int i=0; i < 5; i++){

2.     println(values[i]);

3.}

Note that the control variable “i” (line 2 of the code above) assumes within the brackets [” “]” of the array each of the indexes from 0 to 4 during the repetitions, printing, with a single line of code, all the positions of the array.

Click on the animation below and watch the simulation of how it happens:

In this way, we can modify the code of the initial problem of this article and optimize it with the for repetition structure. See below how this code looks to request 5 numerical values from the user and display them on the screen:

1.int[ ] values = new int[5];

2.for(int i=0; i < 5; i++){ // requests the 5 values

3.     println("Enter the value "+(i+1));

4.     values[i] = input();

5.}

6.for(int i=0; i < 5; i++){ // displays the 5 values 

7.     println(values[i]);

8.}

Now, regardless of the amount of values requested, there is no need to add any more lines of code. We just need to change the values 5 (in code) for the new desired quantity.

See the same complete code below in several different programming languages:

1.import java.util.Scanner;

2.class Main {

3.     public static void main(String[] args) {

4.          Scanner sc = new Scanner(System.in);

5.          int[] values = new int[5];

6.          for(int i=0; i < 5; i++){

7.               System.out.println("Enter the value "+(i+1));

8.               values[i] = sc.nextInt();

9.          }

10.          for(int i=0; i < 5; i++){

11.               System.out.println(values[i]);

12.          }

13.     }

14.}

1.#include <stdio.h>

2.int main(void) {

3.     int values[5];

4.     for(int i=0; i < 5; i++){

5.          printf("Enter the value %d\n",(i+1));

6.          scanf("%d",&values[i]);

7.     }

8.     for(int i=0; i < 5; i++){

9.          printf("%d\n",values[i]);

10.     }

11.}

 

1.#include <iostream>

2.int main() { 

3.     int values[5];

4.     for(int i=0; i < 5; i++){

5.          std::cout << "Enter the value " << (i+1) <<"\n";

6.          std::cin >> values[i];

7.     }

8.     for(int i=0; i < 5; i++){

9.          std::cout << values[i] << "\n";

10.     }

11.}

1.using System;

2.class MainClass {

3.     public static void Main (string[] args) {

4.          int[] values = new int[5];

5.          for(int i=0; i < 5; i++){

6.               Console.WriteLine("Enter the value "+(i+1));

7.               values[i] = Convert.ToInt32(Console.ReadLine());

8.          }

9.          for(int i=0; i < 5; i++){

10.               Console.WriteLine(values[i]);

11.          }

12.     }

13.}

1.# Python lacks support for arrays 

2.# This example uses the NumPy library 

3.import numpy

4.values = numpy.array([])

5.for i in range(5):

6.     value=int(input("Enter the value "+str(i+1)+"\n"))

7.     values = numpy.append(values, value)

8.for i in range(5):

9.     print(values[i])

Exercise 3

Show that you understand the concept of automation of an array and answer the exercise below:

Based on the code below, identify the generated values and drag them to the correct indexes of the array.

Practices

Check out the practices we have prepared below to help you deepen your knowledge on this subject!

Choose your programming language and dig deeper.

Finish your learning saga on basic programming. Read our article on functions. The last subject for you to learn in this basic programming series.

Was this article helpful to you?

So support us and share it with others who are interested in this subject!

WhatsApp
Facebook
Twitter
LinkedIn
Email

Other articles

Subscribe

This website uses cookies to ensure you get the best experience on our website.