Min menu

Pages

VB

Arrays in C plus Plus C++

 matrices inC Plus Plus

the matrix (Array)It is a single variable consisting of several elements(Elements)of the same type.
Each element in the array can store one value.

The elements of an array are distinguished from each other by a specific number given to each element called .index.
The first element in an array is always its number0.

Now, you have to know that the number of array elements is fixed, that is, once you have defined it, you cannot change it again, noting that you can change the values ​​of these elements whenever you want.


 The benefits of matrices in C++

  • Reduce the number of similar variables, for example if we want to define 10 variables of their typeint,We define a single matrix of 10 elements.

  • Working with the code becomes easier, because if you store the information in an array, you can modify them, compare them or fetch them all at once in very small code using a loop.

  • You can access any item through a numbertheindexhis own.

Methods for defining a matrix inC++

There are three ways you can define an array(Declare Array)We will get to know them later.

// The following method is used to define an array with the number of its elements specified :
datatype [ size ] arrayName;

// The following method is used to define an array with its initial values ​​specified
datatype [] arrayName = {value1, value2, ..};

// The following method is used to define an array with the number of its elements and the value of some of its elements
datatype [ size ] arrayName = {value1, value2, ..};


  • datatype : The type of values ​​that can be stored in the elements of an array.

  • size : the number of array elements.

  • arrayName : the name of the array.

  • [] : This symbol represents how many dimensions the matrix is ​​composed of.



Examples showing how to define a unitary matrix(One Dimensional Array).

Examples

// It consists of 5 elements of type int, here we have defined a one-dimensional array named
		  int arr[5];
	  
		  // and we put 6 elements in it, and this means that the number of its elements became 6 because we did not specify the number of its elements, int its type arr, here we have defined a one-dimensional array named
		  int arr[] = {1, 2, 3, 4, 5, 6};
	  
		  // and it consists of 5 elements, and we put initial values ​​in the first 3 elements in which its type is int, arr, here we define a one-dimensional array named
		  int arr[5] = {1, 2, 3};
	


Examples showing how to define a binary matrix(Two Dimensional Array).

Examples

// It consists of 3 x 4 elements of type int, here we have defined a two-dimensional array named
		  int arr[4][3];
	  
		  // we put in it two arrays of 3 elements each, this means that the array consists of 2 x 3 elements of type int, here we have defined a two-dimensional array named
		  int arr[][] = {
		  {1, 2, 3},
		  {4, 5, 6}
		  };
	  
		  // and it consists of 3 x 4 elements, and we put 6 prime values ​​in it of type int, here we define a two-dimensional array named
		  int arr[4][3] = {
		  {1, 2, 3},
		  {4, 5, 6}
		  };
	

 Access to array elements inC++

Now suppose we define an array of typeint,her namea,It consists of 5 elements.

int a[] = { 10, 20, 30, 40, 50 }; 
	  

You can visualize the shape of the matrix ain memory as follows.

Since the array consists of 5 elements, the elements are given numbersindexesin order from 0 to4 .

So here the number of array elements is equal to 5 and it is a constant that cannot be changed later in the code.
And to reach the value of any element we useindexThe item that was given to him.


In the following example, we define an array, then change the value of the first element in it, and then return the value of all the elements.

Example

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have defined an array of 5 elements
		  int arr[] = {10, 20, 30, 40, 50};
	  
		  // Here we change the value of the first element and the last element in the array
		  arr[0] = 1;
		  arr[4] = 5;
	  
		  // Here we have shown the values ​​of all the elements of the array
		  cout << "arr[0] = " << arr[0] << endl;
		  cout << "arr[1] = " << arr[1] << endl;
		  cout << "arr[2] = " << arr[2] << endl;
		  cout << "arr[3] = " << arr[3] << endl;
		  cout << "arr[4] = " << arr[4] << endl;
	  
		  return 0;
		  }
	

We will get the following result when running

arr[0] = 1
		  arr[1] = 20
		  arr[2] = 30
		  arr[3] = 40
		  arr[4] = 5
	

 Setting the initial values ​​of the array elements inC++

If you create a new array with only the number of its elements specified and without giving it initial values, you will likely find strange values ​​in some of its elements.
The reason for this is that these values ​​were already present in memory, and nothing more.

So if you want to create a new array with any default values ​​that may be present in it, you must pass the default value you want to set to the array's elements at the moment it is created.


In the following example, we have defined an array consisting of 5 elements and did not give it initial values, and then display the default values ​​in it.

The first example is about how to set the initial values ​​of the elements of an array in C++

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have defined an array of 5 elements
		  int arr[5];
	  
		  // Here we have shown the default values ​​present in the array elements
		  cout << "arr[0] = " << arr[0] << endl;
		  cout << "arr[1] = " << arr[1] << endl;
		  cout << "arr[2] = " << arr[2] << endl;
		  cout << "arr[3] = " << arr[3] << endl;
		  cout << "arr[4] = " << arr[4] << endl;
	  
		  return 0;
		  }
	

When running the program, we got a strange result, as we found default values ​​in some elements.
Note: It is normal that you will not get the same result that appeared to us because these values ​​are random values.

arr[0] = 8
		  arr[1] = 0
		  arr[2] = 42
		  arr[3] = 0
		  arr[4] = 15275776	
	


In the following example, we set the value 0as the initial value for all the elements of the array, and then display their values.

The second example is how to put the initial values ​​of the elements of an array in C++

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we define an array of 5 elements and specify that all of its elements have a default value of 0
		  int arr[5] = {0};
	  
		  // Here we have shown the default values ​​present in the array elements
		  cout << "arr[0] = " << arr[0] << endl;
		  cout << "arr[1] = " << arr[1] << endl;
		  cout << "arr[2] = " << arr[2] << endl;
		  cout << "arr[3] = " << arr[3] << endl;
		  cout << "arr[4] = " << arr[4] << endl;
	  
		  return 0;
		  }
	

We will get the following result when we run the program.

arr[0] = 0
		  arr[1] = 0
		  arr[2] = 0
		  arr[3] = 0
		  arr[4] = 0	
	

 Find out the number of elements of an array inC++

If you want to know the number of elements in an array, you can get it by dividing the size of the array by the type of elements stored in it.


An example showing how to find out the number of elements in an array C++

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we define an array of 5 elements and specify that all of its elements have a default value of 0
		  int arr[] = {1, 2, 3, 4, 5};
	  
		  // n by the size of the type of the first element in it, and then we store the result in the variable arr here we divide the number of elements of the array
		  int n = sizeof(arr) / sizeof(arr[0]);
	  
		  // n we have stored in the variable arr here we have printed the number of elements of the array
		  cout << "Number of elements in the array is: " << n;
	  
		  return 0;
		  }
	

We will get the following result when we run the program.

Number of elements in the array is: 5
	    


These are some of the other methods you may find used to find out the sizes of matrices.

// You can get the number of elements of an array of whatever type by the following method
		sizeof(arrayName) / sizeof(arrayElement[0])
	
		// you can use the following method int if the array type is
		sizeof(arrayName) / sizeof(int)
	
		// You can use the following string method if the type of the array is
		sizeof(arrayName) / sizeof(string)
  



A note on how to find out the number of elements in an array C++

The method we used to find out the number of array elements can be applied to any other data type but cannot be used with pointers(Pointers)Which we will learn about in later lessons.

Dealing with the matrix by a loop inC++

When dealing with arrays, you will most likely use a loop to loop through their values, whether to search for a value in them, update their values, or just print the values ​​in them.


In the following example, we assume that the number of elements of the array whose values ​​we will display are known.

First example Dealing with an array by a loop in C++

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have created an array containing 3 text values
		  string fruits[3] = {"Apple", "Banana", "Orange"};
	  
		  // on a new line fruits here we created a loop, in each cycle it returns a value from the values ​​in the array
		  for (int i=0; i<3; i++)
		  {
		  cout << fruits[i] << endl;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running

Apple
		  Banana
		  Orange
	

In the following example, we assume that the number of elements of the array whose values ​​we will display is unknown.

Second example Dealing with the array by a loop in C++

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have created an array containing 3 text values
		  string fruits[] = {"Apple", "Banana", "Orange"};
	  
		  // n and then store it in the fruits variable. Here we have calculated the number of elements of the array
		  int n = sizeof(fruits) / sizeof(fruits[0]);
	  
		  // on a new line fruits here we created a loop, in each cycle it returns a value from the values ​​in the array
		  for (int i=0; i<n; i++)
		  {
		  cout << fruits[i] << endl;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running

Apple
		  Banana
		  Orange
	

the ringforeachinC++

Starting with the translation versionC++11forA new episode has been addedForeach Loop.
This loop allows you to pass through all the elements of the array without having to define a counter and specify where it starts and ends.


 The general shape of the ringForeach In C Plus Plus  C++

for (element: array)
{
// statements
}

  • element : It is a regular variable that we define inside the loop and give it the same type of array that we put after the colon, because in each cycle it will store the value of one of its elements, so its type must be made the same as its type.

  • array : The array we want to access all of its elements.

  • statements are all the commands placed in the loop and are executed in each cycle.

So here the loop passes through all the elements of the array in order from the first element to the last element, and in each cycle it stores the value of the element in the variable we have defined.


Now we are going to write a simple program that displays the values ​​of all the elements of an array using the loopForeach.

Example

#include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have created an array containing 3 text values
		  string fruits[] = {"Apple", "Banana", "Orange"};
	  
		  // element in the fruits variable here in each cycle the value of an element of the array will be stored
		  for (string element: fruits)
		  {
		  // element Here the value stored in the variable will be displayed
		  cout << element << endl;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running

Apple
		  Banana
		  Orange
	

In later lessons you will learn how to pass an array to a function and how to define an array that returns a function.
In addition, you will learn about other types of matrices that do not have specific sizes and can be handled more freely.