This article illuminates and explores various kinds of arrays with examples in C++.
Declaration of Array in C++
In C++, arrays are declared through the following syntax:
Declaration of an array of 5 elements of integer datatype is given below:
Types of Arrays in C++
The following are the array types in C++:
Depending on what you require, you can utilize the above kinds of arrays in C++. Now see each array type one by one.
1: What is One-Dimensional Array
The 1D array also refer to a name as a single-dimensional array. A one-dimensional array can rather be referred to as a list of items or a linear array. It only comprises one row.
A one-dimensional array is a collection of duplicate data-type objects that are saved in the same location of memory. Each of the elements within an array is determined through its index, which consists of a value in integers indicating its consecutive location in the array.
Simple Program to Implement One-Dimensional Array in C++
using namespace std;
int main()
{ int array[20], a,b;
cout<>a;
cout<<"\nEnter Values one by one... \n";
for(b=0;b<a;++b)
{
cout<<"\nPlease Enter arr["<<b<>array[b];
}
cout<<"\nArray Values you entered are using One-Dimensional Array:\n\n";
for(b=0; b<a; b++)
{
cout<<array[b]<<" ";
}
return 0;
}
In the above code, we take the array size from the user and ask them to enter elements of an array one by one. After this, we display the elements in a one-dimensional array.
Output
Types of One-Dimensional Arrays
There are several types of one-dimensional arrays, the most widely used are Boolean and Character arrays.
1: Boolean Array
These arrays refer to true and false categories, and it is a simple kind of one-dimensional array.
Example Program of Boolean Array in C++
using namespace std;
int main() {
bool array[5] = { false, true, false, true};
for (int i = 0; i < 5; i++) {
cout << array[i] << " ";}
return 0;
}
In the above code, we initialize a boolean type array as an array having true and false values and print the array as 0 referring to false, and 1 representing true.
Output
2: String or Character Arrays
A character list array is another type of one-dimensional array in which we store strings of characters.
Example Program of String/Character Array in C++
#include <string>
using namespace std;
int main() {
string arr[3] = {"Monday", "Tuesday", "Wednesday"};
for (int i = 0; i < 3; i++) {
cout << arr[i] << " ";}
return 0;
}
In the above program, firstly initialize a string type array as arr and show on screen using for loop.
Output
2: Multi-Dimensional Arrays
A multidimensional array comprises a collection with multiple arrays as elements. There are two-dimensional and three-dimensional structures in multi-dimensional. In two-dimensional arrays, we have elements in form of a matrix in which we have indexes in form of rows and columns. In three-dimensional arrays, we have indexes such as x, y, and z.
Simple Program Example of 2D Array in C++
using namespace std;
int main() {
int a[3][2] = {{1, 2}, {4, 5}, {7, 8}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
cout << a[i][j] << " ";
}
cout << endl;}
return 0;
}
In the above code, we initialize values in 3 rows and 2 columns and print it using cout.
Output
Simple Program Example of 3D Array in C++
using namespace std;
int main() {
const int rows = 3;
const int columns = 2;
const int depth = 3;
int arr[rows][columns][depth];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
for (int k = 0; k < depth; k++) {
arr[i][j][k] = i * j * k;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
for (int k = 0; k < depth; k++) {
cout << arr[i][j][k] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
In the above code, we initialize values in 3 rows and 2 columns and 3 depth and print it using cout.
Output
Conclusion
Arrays are a very confined form of data structures and are widely used in C++. It includes various array types that make it possible for programmers to hold and modify data collections in their code. One-dimensional arrays are the most basic type, while two-dimensional and multidimensional arrays are used to represent data in the form of matrices. Choosing the right type of array can help developers create more powerful, flexible programs that can solve a wide range of problems.