Introduction
Declare and initialize an array in c++ is an article that explains how to declare and initialize an array and a multidimensional array in c++.
What is an Array?
The array is a data structure that stores a collection of objects in an organized manner. The values in an array are named their elements.
The elements can be any data, such as integers, strings, or objects. All elements of an array are of the same type. Every element of an array can be accessed directly.
An integer indexes an array. The index of an array commences from 0 and counts up to one less than the total number of elements in the array. For example, an array with ten elements will have indices from 0 to 9.
Why do we need arrays?
Arrays are used to accumulate lists of data. They are commonly used in computer programming and can be found in many programming languages. Arrays have their functions that perform operations on the array as a whole, such as sorting and searching.
Everyday use for arrays is storing information about people, for example, a list of names or phone numbers.
Arrays are used for storing many values of the same type. They are helpful when you need to store or pass an extensive list of things.
Arrays can be used to store data that is not ordered, like a deck of cards or an unordered list of names, or they can be used to store data that is ordered, like a deck of cards sorted by suit and value.
Declaring an array in C++
Declaring an array in C++ is a common task for programmers. The array declaration syntax is very straightforward, but there are some important concepts to remember while creating the declaration.The first thing to identify is that the Array type must be specified.
The second thing to remember is how many elements will be included in the array, which can be determined by specifying a number or a length. Lastly, it’s important to note that arrays can only contain objects of the same type.Element type array name[Constant number of elements];int score[3];int business_days[5];
Initialize an array in C++
It is best practice to initialize an array with a given size. The Initialization helps the programmer know how much space to allocate for the array. The following example indicates how to initialize an array.int Exam_scores[7] {12, 15, 19, 20, 17, 18, 11};The Array has 7 elements:
- Element 12 has an index of 0
- Element 15 has an index of 1
- Element 19 has an index of 2
- Element 20 has an index of 3
- Element 17 has an index of 4
- Element 18 has an index of 5
- Element 11 has an index of 6
Another way to make the Initialization for arrays in C++:
int Exam_scores[days_in_week] {0};Initialize all elements to 0.int Temperatures [days_in_week] {5, 12, 7};Intialize to 5, 12, 7 and remaining to 0.
Accessing array elements in c++
We can access the element in an array by using its index. We can do this by using the square brackets and putting the index inside them.
The programmer uses the following expression to access the Array element.*array _name[element_index]*Temperatures[3]
- The first temperature at index 0 is
- The second temperature at index 1 is
- The third temperature at index 2 is
- The fourth temperature at index 3 is
- The fifth temperature at index 4 is
- The sixth temperature at index 5 is
- The seventh temperature at index 6 is
The code of the program
#include <iostream>using std::cout;using std::cin;using std::endl;int days_in_week = 7;int main(){ int temperatures [days_in_week] {5, 12, 7};cout <<endl; cout << “The first temperature at index 0 is ” << temperatures[0] << endl;cout << “The second temperature at index 1 is ” << temperatures[1] << endl;cout << “The third temperature at index 2 is ” << temperatures[2] << endl;cout << “The fourth temperature at index 3 is ” << temperatures[3] << endl;cout << “The fifth temperature at index 4 is ” << temperatures[4] << endl;cout << “The sixth temperature at index 5 is ” << temperatures[5] << endl;cout << “The seventh temperature at index 6 is ” << temperatures[6] << endl;cout << ” The console display the following results”<<endl;return 0;}
Storing in a C++ array
Arrays are used to reserve a collection of data, which can be accessed using an index. We use the same syntax to store in a C++ array. We continue using the previous example and get the following code with the following console screen.
The code of the program
#include <iostream>using std::cout;using std::cin;using std::endl;int days_in_week = 7;int main(){ int temperatures [days_in_week] {5, 12, 7};cout <<endl; cin>> temperatures[0];cin>> temperatures[1];cin>> temperatures[2];cin>> temperatures[3];cin>> temperatures[4];cin>> temperatures[5];cin>> temperatures[6];cout << “The first temperature at index 0 is ” << temperatures[0] << endl;cout << “The second temperature at index 1 is ” << temperatures[1] << endl;cout << “The third temperature at index 2 is ” << temperatures[2] << endl;cout << “The fourth temperature at index 3 is ” << temperatures[3] << endl;cout << “The fifth temperature at index 4 is ” << temperatures[4] << endl;cout << “The sixth temperature at index 5 is ” << temperatures[5] << endl;cout << “The seventh temperature at index 6 is ” << temperatures[6] << endl;cout << ” The console display the following results”<<endl;return 0;}
Multidimensional Arrays
Multidimensional arrays are one of the considerable important data structures in computer science.They are also one of the most complex data structures to implement.A multidimensional array is an array that is indexed by two or more indices or keys.
It can be thought of as a table where each row is a different dimension, and each column is a different index.
The values stored in the table depend on both indices and their corresponding dimensions, which makes it challenging to implement efficiently in computer programs.
Declaring Multidimensional arrays
multidimensional arrays can be as the following steps
- Choose an element type
- Choose an array name
- Choose the dimension size 1
- Choose the dimension size 2
C++
Multidimensional arrays
The result will be shown in the following line.element_type array_name[dimension1][dimension2]int [5][3];
The result will be shown in the following line.element_type array_name[dimension1][dimension2]int [5][3];
Initializing multidimensional arrays
multidimensional arrays
int temperatures [5][3]{{8, 6, 5}{12, 8, 47}{3, 22, 25}{1, 7, 41}{6, 6, 9}};
Accessing an element in a multidimensional array
Use the following pieces of codecin>>temperatures [4][2];cin>>temperatures [1][0];cin>>temperatures [2][2];
The code of the program
#include <iostream>using std::cout;using std::cin;using std::endl;int main(){ int temperatures [5][3]{{8, 6, 5},{12, 8, 47},{3, 22, 25},{1, 7, 41},{6, 6, 9}};cout <<endl; cin>>temperatures [4][2];cin>>temperatures [1][0];cin>>temperatures [2][2];cout << ” temperature element 1 is ” <<temperatures [4][2] << endl;cout << ” temperature element 2 is ” <<temperatures [1][0] << endl;cout << ” temperature element 3 is ” << temperatures [2][2] << endl;cout << ” The console display the following results”<<endl;return 0;}
Conclusion
The array is a data structure that stores a collection of values. The values can be either numbers or strings.
The size of the array is fixed when it is created. The individual elements in the array are referenced by an index number, which starts at zero and goes up to one less than the array size minus one.
The first element in an array has index number 0, not 1.The Array in C++ can be multidimensional.