Arrays are one of the most important data structures in C++ and are used to store collections of data. In this article, we will look at the basics of how to create and initialize arrays, access elements of arrays, modify elements of arrays, and use two-dimensional arrays. We will also look at some common array methods and provide several coding exercises to test your understanding of arrays in C++.
What is an Array?
An array is a data structure that stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. For example, if you want to store marks of 100 students, you can create an array for it. In C++, each element in an array is accessed by its index. The first element in an array has an index of 0, the second element has an index of 1, and so on.
Creating and Initializing Arrays
Arrays are declared in C++ using the following syntax:
type arrayName[size];
Here, type is the data type of the array elements and arrayName is the name of the array. The size parameter specifies the number of elements that can be stored in the array. The size of an array must be specified at the time it is declared. The following code declares an array of 10 integers:
int myArray[10];
Once the array is declared, we can initialize it with values. Arrays can be initialized using the same syntax as a normal variable:
int myArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
In this example, the first 10 elements of myArray are initialized to 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Note that if the size of the array is not specified, the compiler will automatically determine the size of the array based on the number of elements in the initialization list.
Accessing Array Elements
Once we have an array initialized, we can access individual elements of the array using the array index. The index of an array is the position of an element in the array, starting from 0. For example, the first element of the array myArray is accessed using the index 0:
int firstElement = myArray[0]; // firstElement is equal to 1
We can also access elements of an array using a loop. The following code uses a for loop to print all elements of the array:
for(int i = 0; i < 10; i++) {
std::cout << myArray[i] << std::endl;
}
Manipulating Array Elements
We can also modify elements of an array using the same syntax as accessing elements. The following code changes the value of the first element of the array:
myArray[0] = 100; // first element of myArray is now equal to 100
This will set the third element in the marks array to 100.
Dynamic Arrays in C++
Dynamic arrays in C++ are arrays that can change size at runtime. This is useful when you don’t know the size of the array at compile time. The syntax for creating a dynamic array is similar to that of a regular array, except that instead of specifying the size of the array, you use a pointer:
type * arrayName;
For example, to create an array of integers that can change size at runtime, you would use the following code:
int * marks;
This will create a pointer to an array of integers. You can then use the new keyword to allocate memory for the array:
marks = new int[arraySize];
This will allocate memory for an array of the specified size. You can then use the [] operator to access and manipulate the elements of the array.
Common Array Methods
C++ offers several methods for manipulating arrays. The most commonly used methods are:
- push_back() – Adds an element to the end of an array.
- pop_back() – Removes the last element of an array.
- insert() – Inserts an element at a specified position in an array.
- erase() – Removes an element from a specified position in an array.
- sort() – Sorts the elements of an array in ascending or descending order.
Multidimensional Arrays in C++
Two-dimensional arrays are arrays that store multiple elements in a grid-like structure. To create a two-dimensional array, we use the following syntax:
type arrayName[size1][size2];
Here, size1 and size2 specify the number of rows and columns in the array, respectively. We can also initialize a two-dimensional array with values:
int myArray[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
We can access elements of a two-dimensional array using two indices:
int element = myArray[0][2]; // element is equal to 3
Conclusion
In this article, we have looked at the basics of how to create and initialize arrays, access elements of arrays, modify elements of arrays, and use two-dimensional arrays in C++. We have also looked at some common array methods and provided several coding exercises to test your understanding of arrays in C++.
Exercises
Write a program to create an array of 10 integers and print its elements.
#include <iostream>
using namespace std;
int main()
{
// Create an array of 10 elements
int arr[10];
// Store values in the array
for (int i = 0; i < 10; i++)
arr[i] = i;
// Print the elements of the array
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
return 0;
}
Write a program to create a dynamic array of 10 integers and print its elements.
#include <iostream>
using namespace std;
int main()
{
// Create a dynamic array of 10 elements
int *arr = new int[10];
// Store values in the array
for (int i = 0; i < 10; i++)
arr[i] = i;
// Print the elements of the array
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
// Deallocate the memory
delete[] arr;
return 0;
}
Write a program to create a two-dimensional array of 10 rows and 5 columns and print its elements.
#include <iostream>
using namespace std;
int main()
{
// Create a two-dimensional array of 10 rows and 5 columns
int arr[10][5];
// Store values in the array
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
arr[i][j] = (i + 1) * (j + 1);
}
// Print the elements of the array
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
cout << arr[i][j] << " ";
cout << endl;
}
return 0;
}
Write a program to create a two-dimensional array of 3 rows and 4 columns and print its elements in reverse order.
#include <iostream>
using namespace std;
int main()
{
// Create a two-dimensional array of 3 rows and 4 columns
int arr[3][4];
// Store values in the array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
arr[i][j] = (i + 1) * (j + 1);
}
// Print the elements of the array in reverse order
for (int i = 2; i >= 0; i--)
{
for (int j = 3; j >= 0; j--)
cout << arr[i][j] << " ";
cout << endl;
}
return 0;
}
Write a program to create a two-dimensional array of 5 rows and 3 columns and print its elements in spiral order.
#include <iostream>
using namespace std;
int main()
{
// Create a two-dimensional array of 5 rows and 3 columns
int arr[5][3];
// Store values in the array
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
arr[i][j] = (i + 1) * (j + 1);
}
// Print the elements of the array in spiral order
int rowStart = 0, rowEnd = 4, colStart = 0, colEnd = 2;
while (rowStart <= rowEnd && colStart <= colEnd)
{
// Print first row from the remaining rows
for (int i = colStart; i <= colEnd; i++)
cout << arr[rowStart][i] << " ";
rowStart++;
// Print last column from the remaining columns
for (int i = rowStart; i <= rowEnd; i++)
cout << arr[i][colEnd] << " ";
colEnd--;
// Print last row from the remaining rows
if (rowStart <= rowEnd)
{
for (int i = colEnd; i >= colStart; i--)
cout << arr[rowEnd][i] << " ";
rowEnd--;
}
// Print first column from the remaining columns
if (colStart <= colEnd)
{
for (int i = rowEnd; i >= rowStart; i--)
cout << arr[i][colStart] << " ";
colStart++;
}
}
return 0;
}