Array Manipulation
In the previous screen, we learned the basics of arrays. Now, let's dive into array manipulation, which involves performing operations such as creating, accessing, and modifying elements in an array.
Creating an Array
To create an array in C++, you define the type of the elements followed by the name of the array and the size of the array in square brackets. For example:
TEXT/X-C++SRC
1int myArray[5];
xxxxxxxxxx
26
using namespace std;
int main() {
// Create an array
int myArray[5];
// Assign values to the array
for (int i = 0; i < 5; i++) {
myArray[i] = i + 1;
}
// Accessing elements of the array
cout << "Element at index 2: " << myArray[2] << endl;
// Modifying elements of the array
myArray[3] = 10;
// Displaying the modified array
for (int i = 0; i < 5; i++) {
cout << myArray[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment