This tutorial aims to provide an in-depth understanding of how to work with arrays in C++. We will cover single-dimensional and multi-dimensional arrays, how to declare, initialize, and manipulate them.
By the end of this tutorial, you will be able to:
- Understand what arrays are and why they are useful
- Declare and initialize single and multi-dimensional arrays
- Access and modify elements within an array
- Understand the basics of memory allocation with arrays
Basic knowledge of C++ syntax and data types is recommended.
An array is a data structure that allows you to store multiple values of the same type in a single variable. This is especially useful when you need to store many values, but don't want to create a separate variable for each one.
To declare an array in C++, you specify the type of its elements, followed by the name of the array and the array size in square brackets.
int myArray[5];
To initialize an array, you can assign values to it at the time of declaration:
int myArray[5] = {10, 20, 30, 40, 50};
To access an array element, you use the array name followed by the index of the element in square brackets. Remember that array indices start at 0, not 1.
cout << myArray[0]; // Outputs: 10
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array
int scores[5] = {89, 95, 76, 85, 92};
// Access and print each element
for(int i = 0; i < 5; i++) {
cout << "Score " << (i+1) << ": " << scores[i] << endl;
}
return 0;
}
Expected Output:
Score 1: 89
Score 2: 95
Score 3: 76
Score 4: 85
Score 5: 92
#include <iostream>
using namespace std;
int main() {
// Declare and initialize a 2D array
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Access and print each element
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
cout << "Element at [" << i << "][" << j << "]: " << matrix[i][j] << endl;
}
}
return 0;
}
Expected Output:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6
In this tutorial, we have learned what arrays are, how to declare, initialize, access, and manipulate them. We also discussed the importance of memory allocation with arrays. The next steps would be to dive deeper into more complex data structures and algorithms, which often make use of arrays.
Exercise 1:
Declare an array of size 10, initialize it with numbers from 1 to 10, and print them in reverse order.
Exercise 2:
Declare a 3x3 two-dimensional array and initialize it with numbers from 1 to 9. Then print the matrix.
Solutions
Solution 1:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize the array
int myArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Print in reverse order
for(int i = 9; i >= 0; i--) {
cout << myArray[i] << endl;
}
return 0;
}
Solution 2:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize the 2D array
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Print the matrix
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}