Arrays concept in C++ - ProgrammerTech
Language:

Search

Arrays concept in C++

  • Share this:
Arrays concept in C++

Arrays in C++

Arrays are a set of data of the same type (float,char,int,double).

And it has a common name, which means that the data stored in the array, for example, are all integers. And that it have a common name that expresses this data in the name of the filter.

In arrays, we can format the code and put a large number of variables in it without complexity, and we can access any element in this array and distinguish it by calling it its index.

 

Declaring an array in C++

There are three things to consider when declaring an array

  1. The name of the array is a name we choose just as we choose any variable name.
  2. The number of items in it.
  3. The type of data used.

There are three ways to write and define an array in C++

1 - Define an array with specifying the number of elements of the array.

datatype arrayName[size];

2 - Defining an array and defining the initial values ​​of the array.

datatype arrayName[] = {value1, value2, ..};

3 - Define an array and determine the number of its elements and the value of some of its elements.

datatype arrayName[size] = {value1, value2, ..};

Word's meaning:

  • 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 array is composed of.

example 1

#include <iostream>
using namespace std;
int main()
{
    int x[5];
    return 0;
}

where int is the type of data inside the array, x is the name of the array and 5 is the number of elements inside this array.

And this form we shortened everything in an array, as if we say that the form of this code without an array is.

x[0];
x[1];
x[2];
x[3];
x[4];

But the filter helped us in short all this, and each component is an independent variable from the other.

 

How to insert elements into an array

The name of the matrix and then the number of the element that we want to deal with is mentioned within the parentheses, so if we want, for example, to put the value 100 in the sixth element of the matrix, this is done as in the figure.

x [5] = 100;

Example 2

#include <iostream>
using namespace std;
int main()
{
    int x[5];
    x[4] = 15;
    cout << x[4] << endl;
    return 0;
}

We note that we put the value of the last index of the array as 15, and we were able to reach it through the index, and it is ranked 4.

When you run this code, the result will be

example 3

#include <iostream>
using namespace std;
int main()
{
    int x[5];
    x[0] = 20;
    x[1] = 5;
    x[4] = x[0]+x[1];
    cout << x[4] << endl;
    return 0;
}

In the example, we told him to add the index value 0, which is 20, with the index value 1, and we stored its data in index 4.

And we told the print command to print the index value of 4 for us, whose value is the sum of indexes 0 and 1.

When you run the code, we will get the result

25

Example 4

#include <iostream>
using namespace std;
int main()
{
    int x[5] = { 5,6,8,9,1 };
    cout << x[2] << endl;
    return 0;
}

We created an integer array of length 5 with five elements, and then we asked in the print that we want to print the index value number 2.

When you run the code, the result is:

8

Example 5

#include <iostream>
using namespace std;
int main()
{
    int x[5] = {2};
    cout << x[3] << endl;
    return 0;
}

In this example, the number 0 was printed for us, but we only set the value 2, and the idea of ​​this is that the value 2 is the value of the first index only, and the rest of the values ​​are zeros.

0

We printed 0 because we requested the third index print.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com