Hey Everyone!!!
My name is Atharva and I am a high school student who is passionate about programming, content writing and open-source. I am curious about the world of technology and how it can be used to make a difference in the world.
In this blog, I will be explaining about what I learnt about Arrays in C++.
Let's Begin !!!
Arrays are a fundamental data structure in C++ that allows you to store and access a sequence of values. They are useful for organizing and manipulating data in your programs and are often used in conjunction with looping constructs to perform operations on multiple elements at once.
An array is a collection of variables of the same type, stored contiguously in memory. Each element in the array can be accessed by its index, which is a zero-based integer value that represents the position of the element in the array.
Here's an example of how you can declare and initialize an array in C++:
#include <iostream>
using namespace std;
int main() {
// Declare an array of integers with 5 elements
int myArray[5];
// Initialize the array with values
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
// Access and print the values of the array
cout << "Element 0: " << myArray[0] << endl;
cout << "Element 1: " << myArray[1] << endl;
cout << "Element 2: " << myArray[2] << endl;
cout << "Element 3: " << myArray[3] << endl;
cout << "Element 4: " << myArray[4] << endl;
return 0;
}
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
You can also use the C++11 syntax to declare and initialize an array in a single line, like this:
int myArray[] = {10, 20, 30, 40, 50};
Arrays in C++ are fixed-size, meaning that once you create an array, you cannot change its size. If you need to store a dynamic collection of values, you can use other data structures like vectors or linked lists.
You can loop through the elements of an array using a for loop, like this:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array of integers
int myArray[] = {10, 20, 30, 40, 50};
// Loop through the array and print each element
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << myArray[i] << endl;
}
return 0;
}
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
You can also use the range-based for loop introduced in C++11 to iterate over the elements of an array, like this:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array of integers
int myArray[] = {10, 20, 30, 40, 50};
// Loop through the array and print each element
for (int x : myArray) {
cout << x << endl;
}
return 0
}
A sample question on arrays :
Q. Write a program that reads an array of integers and finds the sum of all the elements in the array.
#include <iostream>
using namespace std;
int main() {
// Declare an array of integers and a variable to store the sum
int Arr[5] = {1, 2, 3, 4, 5, 6};
int sum = 0;
// Loop through the array and add each element to the sum
for (int i = 0; i < 6; i++) {
sum += Arr[i]; //shorthand operator
}
// Print the sum
cout << "The sum is: " << sum << endl;
return 0;
}
Output:
The sum of elements of the array is: 21
Multi-Dimensional Array
In C++, you can create both single-dimensional and multi-dimensional arrays.
A single-dimensional array is a linear collection of elements, accessed by a single index. The arrays declared above were single-dimensional arrays. We will now learn about multi-dimensional arrays in C++.
A multi-dimensional array is an array of arrays, where each element is itself an array. You can create multi-dimensional arrays of any number of dimensions, but the most common are two-dimensional (2D) and three-dimensional (3D) arrays.
2D Array :
To declare a 2D array, you specify the number of rows and columns in the array, like this:
int myArray[3][3];
This creates a 2D array with 3 rows and 3 columns, for a total of 9 elements. You can access the elements of the array using two indices, one for the row and one for the column. For example:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize a 2D array of integers
int myArray[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Access and print the values of the array
cout << "Element [0][0]: " << myArray[0][0] << endl;
cout << "Element [0][1]: " << myArray[0][1] << endl;
cout << "Element [0][2]: " << myArray[0][2] << endl;
cout << "Element [1][0]: " << myArray[1][0] << endl;
cout << "Element [1][1]: " << myArray[1][1] << endl;
cout << "Element [1][2]: " << myArray[1][2] << endl;
cout << "Element [2][0]: " << myArray[2][0] << endl;
cout << "Element [2][1]: " << myArray[2][1] << endl;
cout << "Element [2][2]: " << myArray[2][2] << endl;
return 0;
}
Output:
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9
3D Array:
A 3D array, also known as a multi-dimensional array, is an array of arrays of arrays in C++. It is a data structure that allows you to store and access a set of values in a 3-dimensional space, with each dimension represented by an index.
You can create a 3D array by specifying the number of dimensions and the size of each dimension, like this:
int myArray[3][3][3];
This creates a 3D array with 3 layers, each with 3 rows and 3 columns, for a total of 27 elements. You can access the elements of the array using three indices, one for the layer, one for the row, and one for the column.
Here's an example of how you can declare and initialize a 3D array in C++:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize a 3D array of integers
int myArray[3][3][3] = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}},
{{19, 20, 21}, {22, 23, 24}, {25, 26, 27}}};
// Access and print the values of the array
cout << "Element [0][0][0]: " << myArray[0][0][0] << endl;
cout << "Element [0][0][1]: " << myArray[0][0][1] << endl;
cout << "Element [0][0][2]: " << myArray[0][0][2] << endl;
cout << "Element [0][1][0]: " << myArray[0][1][0] << endl;
cout << "Element [0][1][1]: " << myArray[0][1][1] << endl;
cout << "Element [0][1][2]: " << myArray[0][1][2] << endl;
cout << "Element [0][2][0]: " << myArray[0][2][0] << endl;
cout << "Element [0][2][1]: " << myArray[0][2][1] << endl;
cout << "Element [0][2][2]: " << myArray[0][2][2] << endl;
cout << "Element [1][0][0]: " << myArray[1][0][0] << endl;
cout << "Element [1][0][1]: " << myArray[1][0][1] << endl;
// ...
return 0;
}
Output:
Element [0][0][0]: 1
Element [0][0][1]: 2
Element [0][0][2]: 3
Element [0][1][0]: 4
Element [0][1][1]: 5
Element [0][1][2]: 6
Element [0][2][0]: 7
Element [0][2][1]: 8
Element [0][2][2]: 9
Element [1][0][0]: 10
Element [1][0][1]: 11
You can also use loops to iterate over the elements of a 3
Conclusion
In conclusion, this blog post provided a clear explanation of arrays in C++.
The post covered the basics of arrays, including how to declare and initialize them, as well as how to loop through their elements.
The post also introduced multi-dimensional arrays, including 2D and 3D arrays, and explained how to declare and access their elements.
Overall, this post is a great resource for anyone looking to learn about arrays in C++.
Stay determined, stay focused and keep pushing forward !!!
Liked This Blog?
Do react and comment with your thoughts on the points discussed above.
Make sure to follow me :